Question

The following code creates a Sparklines pie chart from a JSON string:

//Display Visitor Screen Size Stats

$.getJSON('models/ucp/traffic/traffic_display_bos.php',
{   
    type: 'ss',
    server: server,
    api: api,
    ip: ip,
},
function(data)
{
    //alert(data.screens);
    $('#traffic_bos_ss').sparkline(data.views,
    {
        type: "pie",
        height: "100%",
        tooltipFormat: "{{offset:offset}} - {{value}}",     
        tooltipValueLookups:
        {
            'offset': data.screens; 
        }
    });
});

The pie chart is successfully created, however, I'm having trouble with assigning the correct labels to the offset. The JSON string that is called is as follows:

{"screens":"{ 0: '1220x1080', 1: '1620x1080', 2: '1920x1080' }", "views":[2, 2, 61]}

I want it so that the screens of the JSON is inserted in the offset (data.screens). It should become:

tooltipValueLookups:
{
    'offset': { 0: '1220x1080', 1: '1620x1080', 2: '1920x1080' }    
}

How can this be accomplished?

Was it helpful?

Solution

Your JSON string:

{"screens":"{ 0: '1220x1080', 1: '1620x1080', 2: '1920x1080' }", "views":[2, 2, 61]}

is incorrect, as "screens" is a single string value (i.e. it has the value "{ 0: '1220x1080', 1: '1620x1080', 2: '1920x1080' }"). You want to remove the quotes so that it is an object with 3 value pairs ie.

{"screens": { "0": '1220x1080', "1": '1620x1080', "2": '1920x1080' }, "views":[2, 2, 61]}

See the following code which I've put in a fiddle for an example of this working:

var values = {"screens": { 0: '1220x1080', 1: '1620x1080', 2: '1920x1080' }, "views":[2, 2, 61]};

$('#test').sparkline(values.views,
                               {
                                   type: "pie",
                                   height: "100%",
                                   tooltipFormat: '{{offset:offset}}  - {{value}}',     
                                   tooltipValueLookups:
                                    {
                                        'offset': values.screens
                                    }
                               });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top