2

我通过引用 json 数据在 highcharts 中绘制多个系列。json数据配置如下:

{"data": {"words with friends": [[1365034819.212261, 107], [1366071619.212278, 224]], "snacksss": [[1364775619.212285, 786], [1366849219.212291, 445]], "ticktackloot": [[1365207619.212297, 85], [1367799619.212303, 803]], "bike race": [[1364343619.212309, 293], [1367886019.212315, 572]]}}

其中每个字典中的第一个也是唯一的元素是列表列表。每个列表列表都包含一个带有时间戳和任意值的列表。每个字典的键代表一个系列。问题是该系列仅绘制每个列表列表中的最后一个列表。时间戳在 x 轴上,在 y 轴上是任意值。我最终得到 224、445、803 和 572 的 y 点。我希望它们都显示出来。代码如下:

var chart;  // global
function requestData() 
{
    $.ajax(
    {
        url: 'test.json',
        success: function(response) 
        {
            // SINCE response IS TURNED INTO AN OBJECT LITERAL
            // ASSOCIATIVE ARRAY, WE CAN ACCESS data
            for (var key in response.data)
            {
                //extract every timestamp and multiply it by 1000
                //because JS processes timestamps in microseconds.
                //Reinsert in array
                for(var counter=0;counter<response.data[key].length;counter++)
                {
                    insert_array=[]
                    //insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
                    insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])

                    if(counter==response.data[key].length-1)
                    {
                        var seriesData=insert_array;

                        var seriesName = key;

                        console.log("   processing " + seriesName + "=" + seriesData);
                        var series = 
                        {
                            name: seriesName,
                            data: seriesData,
                            lineWidth: 15
                        };

                        chart.addSeries(series);
                    }
                }
            }
        },
        cache: false
    });
}

$(document).ready(function()
{
    console.log("preparing document...");
    chart = new Highcharts.Chart(
    {
        chart: 
        {
            renderTo: 'container1',
            type: 'bar',
            events: 
            {
                load: requestData
            }
        },
        title: 
        {
            text: 'Top Offers'
        },
        xAxis: 
        {
            type: 'datetime',
            title:
            {
                text: 'Offers'
            }
        },
        yAxis: 
        {
            title:
            {
                text: 'Rewards'
            }    
        },
        series: []
    });   
 });
4

1 回答 1

1

尝试像这样更改循环内的 cody:

//extract every timestamp and multiply it by 1000 because JS processes timestamps in microseconds. Reinsert in array
insert_array=[]

for(var counter=0;counter<response.data[key].length;counter++)
    {                      
        //insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
        insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
    ...

这样,insert_array变量将在每个列表中定义一次并正确填充;使用您的实际代码,该变量在每个循环中都会被清除,因此它只会携带最后一个值。

于 2013-05-16T19:47:39.460 回答