在Highcharts中使用JSON数据

时间:2014-05-14 23:27:42

标签: javascript json highcharts

我有一些JSON数据:

data1=[
{"date": 1396828800, "impressions": 49145385},
{"date": 1396915200, "impressions": 46704447},
{"date": 1397001600, "impressions": 47181000},
{"date": 1397088000, "impressions": 47337965},
{"date": 1397174400, "impressions": 51129266},
{"date": 1397260800, "impressions": 60547397},
{"date": 1397347200, "impressions": 62217077},
{"date": 1397433600, "impressions": 49145385},
{"date": 1397520000, "impressions": 46704447},
{"date": 1397606400, "impressions": 47181000},
{"date": 1397692800, "impressions": 47337965},
{"date": 1397779200, "impressions": 51129266},
{"date": 1397865600, "impressions": 60547397}
];

我希望在Highcharts中显示它。这是我正在处理的模板:http://jsfiddle.net/hohenheim/j8cTE/9/

如何将这些数据输入高级图表?

2 个答案:

答案 0 :(得分:0)

替换

series: 
        [{
            data: [
                    [Date.UTC(2014, 0, 01), 1000],
                    [Date.UTC(2014, 0, 02), 1000],
                    [Date.UTC(2014, 0, 03), 1000],
                    [Date.UTC(2014, 0, 04), 1000]]    
        },
        { 
            data: [
                    [Date.UTC(2014, 0, 01), 1000],
                    [Date.UTC(2014, 0, 02), 1000],
                    [Date.UTC(2014, 0, 03), 1000],
                    [Date.UTC(2014, 0, 04), 1000]]
        }
        ] 

series: [{data: data}]

此外,您需要重新格式化数据,使其看起来像在那里显示的那样:

["date", 49145385]

而不是

{"date": 1396828800, "impressions": 49145385}

答案 1 :(得分:0)

您需要对数据进行一些处理,以使其符合highcharts想要的格式。

for (var i=0;i< data1.length;i++) {
    data1[i] = [data1[i].date, data1[i].impressions];
}

http://jsfiddle.net/j8cTE/10/

然后将其用作系列:

    series: 
    [{
        data: data1   
    }]