Highcharts显示系列名称,但缺少json源中的数据点


Highcharts displays series names but missing data points from json source

我使用带有JSON数据的Highcharts来呈现标准折线图,但在显示系列数据时遇到了一些问题。图表框架和系列名称显示在页面上,所有内容都在控制台中检查出来,所以我知道它已经成功地获取了数据。

当我运行JSONLint时,JSON输出看起来是有效的,所以我有点困惑为什么这个图表不能正确呈现。

这是我的图表JAVASCRIPT代码:

$(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                type: 'line',
                zoomType: "x" 
            },
            xAxis: {
                type: 'datetime',
                tickInterval:  7 * 24 * 3600 * 1000,
                labels: {
                    format: '{value:%b %e, %Y}',
                    rotation: -45
                },
            },
            yAxis: {
                allowDecimals: true,
                title: {
                    text: '$US'
                }
            },
            tooltip: {
                xDateFormat: '%b %e, %Y',
            },
            series: []
        }
        $.getJSON("mdb_ajax.php", function(json) {
            options.series = new Array();
            for(i=0;i< json.length;i++) {
                options.series.push(json[i]);
            }
            var chart = new Highcharts.Chart(options);
        });
    });

以下是相应的PHP文件:

$price_output = $prices->getPrices($filter_option);
    foreach($price_output as $category => $price)
    {
        $arr[] = array('name' => $category, 'data' => $price);
    }
print json_encode($arr);

正如你所看到的,我已经对该对象进行了评审,添加了"name"answers"data"键来帮助Highcharts,但如果没有这些额外的工作,该对象将生成完全有效的JSON(它不会以这种方式显示任何内容)。

以下是JSON DATA:的示例

[
  {
    "name": "CTGY1",
    "data": {
        "1414998000000": "6.2400",
        "1415084400000": "-3.1110",
        "1415170800000": "1.5090",
        "1415257200000": "4.2390",
        "1415343600000": "1.6990",
        "1426140000000": "5.9100"
    },
    {
    "name": "CTGY2",
    "data": {
        "1414998000000": "7.7890",
        "1415084400000": "-0.7610",
        "1415170800000": "1.1600",
        "1415257200000": "5.3300",
        "1415343600000": "1.9290",
        "1415602800000": "-0.8260"
  }
]

我猜在处理提取的JSON数据时,需要对键值进行一些定义,我只是不确定这一点会是什么样子。有什么最好的方法吗?

data应该是一个数组,Y值在您的情况下不应该是字符串:data: [[1415602800000,-0.8260],[],...]

以下是seriesdata可接受的数据结构http://api.highcharts.com/highcharts#series.data

Highcharts API也提供了一些例子:

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/series/data-array-of-arrays/