1

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?

4

1 回答 1

1

您的 JSON 字符串:

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

不正确,因为“screens”是单个字符串值(即它的值是“{ 0: '1220x1080', 1: '1620x1080', 2: '1920x1080' }”)。您想删除引号,使其成为具有 3 个值对的对象,即。

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

有关此工作的示例,请参阅我放入小提琴中的以下代码:

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
                                    }
                               });
于 2013-10-28T02:02:46.910 回答