Highcharts不显示数据

时间:2017-01-26 22:04:53

标签: javascript highcharts

我正在添加系列'从我的API加载数据后。问题是系列数据没有显示。我有相同的数据格式,就像你刚制作图表时刚刚添加了系列一样。我检查了我的JSON和所有数据,所有这些都很好。

代码:

var overallGraph;

$(document).ready(function() {

    updateGraph();

    function updateGraph() {
        $.post('../api/overall-lookup.php?d=4', function(json) {
            var guildData = [];
            var textCData = [];
            var offiUData = [];

            for(var i = 0; i < 4; i++){
                var data = json['lookup-results'][i];
                guildData.push(data['guilds']);
                textCData.push(data['text_channels']);
                offiUData.push(data['official_users']);
            }

            overallGraph.addSeries({'name': 'guilds', 'data': guildData}, true);
            overallGraph.addSeries({'name': 'text channels', 'data': textCData}, true);
            overallGraph.addSeries({'name': 'official users', 'data': offiUData}, true);
        });
    }

    overallGraph = Highcharts.chart('container', {
        title: {
            text: 'Overall Data For FlareBot',
            x: -20
        },
        xAxis: {
            title : {
                text: 'Date'
            }  
        },
        yAxis: {
            title: {
                text: 'Amount'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        }
    });
});

输出: output

JSON

{
  "code": 300,
  "lookup-results": [
    {
      "date": "2017-01-01",
      "guilds": "759",
      "text_channels": "4320",
      "official_users": "79"
    },
    {
      "date": "2016-12-31",
      "guilds": "756",
      "text_channels": "4409",
      "official_users": "81"
    },
    {
      "date": "2016-12-30",
      "guilds": "717",
      "text_channels": "4225",
      "official_users": "82"
    },
    {
      "date": "2016-12-29",
      "guilds": "700",
      "text_channels": "4137",
      "official_users": "78"
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

数据中有字符串,需要数字,因此必须将字符串转换为数字。

for(var i = 0; i < 4; i++){
        var data = json['lookup-results'][i];
        guildData.push(Number(data['guilds']));
        textCData.push(Number(data['text_channels']));
        offiUData.push(Number(data['official_users']));
    }