使用对象数组填充高图线

时间:2016-06-16 07:00:14

标签: javascript php highcharts

我正在使用highchart js来创建折线图。我正在使用php进行json响应。 问题是当我从响应填充图表时显示年份但是线未绘制。

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: 'resp_highChart.php',
        dataType: "json",
        contentType: "application/json",
        success: function (response) {
            console.log(response);
            // draw chart
            $('#container').highcharts({
                chart: {
                type: 'line'
            },
            title: {
                text: 'Year Wise Sales Data'
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr']
            },
            yAxis: {
                title: {
                    text: 'Sold Value'
                },
                labels: {
                    formatter: function () {
                        return this.value + '';
                    }
                }
            },
            plotOptions: {
                line: {
                    dataLabels: {
                        enabled: false
                    },
                    enableMouseTracking: true
                }
            },
                series: response
            });

        }


    });

});

我的回复格式如下

[{"name":"2012","data":"[692101643,716334837,776991835,769420169 ]"},{"name":"2013","data":"[860859252,825852169,895524501,892930333 ]"}]

1 个答案:

答案 0 :(得分:1)

这里的问题是你的数据值被读作字符串而不是数组。

我使用您的图表选项和直接插入的JSON数据创建了一个示例小提琴:http://jsfiddle.net/brightmatrix/143bzv1s/

图表可以正常使用,格式如下。请注意,数据阵列周围没有引号。

strsplit

我希望这对你有所帮助!

另外,非常感谢您在问题中包含样本数据。那是非常有帮助。