Using query string data


If you're passing data around via the query string that's going to be displayed on your charts then RGraph has a set of functions that will make this much easier for you.

These functions take the name of the query string parameter that you want and return the value to you in the desired format which can be a number, a string, a JSON object or a JavaScript array depending on your data and which function you use.

The available functions

The available functions are:


This function will return a single number to you. The returned value is a Number type (remember the query string starts off as a string). An example of using it is:
    // URL: example.html?myNumber=-67.9
    var num = RGraph.GET.number('myNumber');


This function will return a string (text) to you. Naturally, the type of the variable will be String. An example of using it is:
    // URL: example.html?myString=someRandomText
    var str = RGraph.GET.string('myString');
Note that you may need to encode the URL when you generate it if it has non-URL-safe characters in it.


This function will return an array of data to you. You can use it to get whole datasets from the query string. An example of using it is:
    //    URL: example.html?myArray=5,8,4,6,3,5,8
    // or URL: example.html?myArray=[5,8,4,6,3,5,8]
    // or URL: example.html?myArray=5|8|4|6|3|5|8
    // Get a normal, comma-separated list (with or without enclosing square brackets)
    var arr = RGraph.GET.array('myArray');
    // Or using the second parameter to specify the separator character
    var arr = RGraph.GET.array('myArray', '|');


This is undoubtedly the most useful of all these functions. It fetches an entire JSON object (a regular JavaScript object) from the URL. The advantage of passing an object around is that you can pass multiple bits of information at the same time - for example, both the data and also the labels. An example of using it is:
    // URL: example.html?myObject={"data":[5,4,3],"labels":["John","Luis","Bob"]}
    // Get the object from the URL
    var json = RGraph.GET.json('myObject');
    // And then in your chart configuration you can use and json.labels
    // like in the example code below
Note 1

The JSON parsing function in JavaScript is strict in it's interpretation of JSON - so your strings must be enclosed by double-quotes - not single-quotes. So this would work:

But this would not (single-quotes are not allowed):
Note 2

Also note that you should try to avoid spaces in JSON where they're not needed. This avoids you seeing lots of %20 encodings in the URL.

Note 3

If you're hoping to get pages indexed by Google, they've previously advised that you shouldn't use certain characters in URLs (for example: { } , [ ]). So this means that you shouldn't use JSON in these URLs.

An example

This is an example of using the RGraph.GET.json function (because it's the most versatile). It uses a URL like this:

    json = RGraph.GET.json('json');

    if(json) {
        new RGraph.Bar({
            id: 'cvs',
            options: {
                xaxisLabels: json.labels,
                xaxis: false,
                yaxis: false,
                backgroundGridVlines: false,
                backgroundGridBorder: false
            {maxWidth: null, width: 700, height: 300, options:{textSize: 16, marginInner: 20}},
            {maxWidth: 750, width: 400, height: 200, options:{textSize: 11, marginInner: 5}}