Php Highchart ajax请求

Php Highchart ajax请求,php,jquery,json,highcharts,Php,Jquery,Json,Highcharts,在网上搜索之后,我决定最后问这个问题 我目前正在使用PHPExcel访问xlsx数据,我也在使用jqueryhighcharts;两者单独使用时都有效。我发现了几种将highcharts与不同的XHR方法结合使用的方法。我想知道的是,下面的代码是否可行。我可以得到结果,从PHP返回的JSON包含10个我可以在控制台中看到的对象。我的问题在于数据。输出将最后一个JSON对象插入xAxis上的第一个插槽,并跳过前9个 $(function () {

在网上搜索之后,我决定最后问这个问题

我目前正在使用PHPExcel访问xlsx数据,我也在使用jqueryhighcharts;两者单独使用时都有效。我发现了几种将highcharts与不同的XHR方法结合使用的方法。我想知道的是,下面的代码是否可行。我可以得到结果,从PHP返回的JSON包含10个我可以在控制台中看到的对象。我的问题在于数据。输出将最后一个JSON对象插入xAxis上的第一个插槽,并跳过前9个

$(function () {                     
    var chart;
    $(document).ready(function() {
        chart1 = new Highcharts.Chart({

            chart: {
                renderTo: 'container',
                zoomType: 'xy'
            },
            title: {
                text: 'Market Size & Trading Report'
            },
            subtitle: {
                text: 'Source: Some Source'
            },
            xAxis: [{
                categories: [],

            }],
            yAxis: [{ // Primary yAxis
                labels: {
                    formatter: function() {
                        return this.value +'m';
                    },
                    style: {
                        color: '#000'
                    }
                },
                title: {
                    text: 'Market Size',
                    style: {
                        color: '#000'
                    }
                }
            }, { // Secondary yAxis
                title: {
                    text: 'Percent',
                      style: {
                        color: '#4572A7'
                    }
                },
            labels: {
                formatter: function() {
                    return this.value +' %';
                },
                style: {
                    color: '#4572A7'
                }
            },
            opposite: true
        }],
        tooltip: {
            formatter: function() {
                return ''+
                    this.x +': '+ this.y +
                    (this.series.name == 'Percent' ? ' %' : 'm');
            }
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 200,
            verticalAlign: 'top',
            y: 50,
            floating: true,
            backgroundColor: '#FFFFFF'
        },
        series: [{
            name: 'Market Size',
            color: '#4572A7',
            type: 'column',
            yAxis: 0,
            data: [6.7,7.0,6.8,6.6,6.4,6.8,6.5,6.3,6.3,6.4]

        }, {
            name: 'P Market Size',
            color: '#89A54E',
            type: 'spline',
            yAxis: 1,
            data: [9, 8.4, 8.3,8.1,8.6,8.8,8.7,8.4,8.9,8.6]
        },{
            name: 'D Market Size',
            color: '#FFCC00',
            type: 'spline',
            yAxis: 1,
            data: [1.7,1.7,1.8,1.8,2.0,1.8,1.7,1.7,1.7,1.71]
        }]
    });


    $.ajax({
                type: "GET",
                dataType: "json",
                url: 'http://devtest.localhost/screen-tests/excelReader/Tests/marketSizeandShareGraph.php',
                success: function (data) {
                var i;
                        $.each(data, function (k, v) {

                                chart1.xAxis[0].setCategories([v.heading]) ;

                        } );
                        }
                        } );

});
}))


你有什么想法吗?

我想你需要一次设置所有类别。大概是这样的:

var myCategories = [];
$.each(data, function (k, v) {
    myCategories.push(v.heading);
}
char1.xAxis[0].setCategories(myCategories);

每次调用setCategories都会导致重新绘制图表,因此最好调用一次。

返回的数据对象是什么样子的?嗨,steve,返回的对象如下,[{“heading”:“30 Dec”},{“heading”:“06 Jan”},{“heading”:“13 Jan”},{“heading”:“20 Jan”},{“heading”:“27 Jan”},{“heading”:“03 Feb”},{“heading”:“10 Jan”}{“heading”:“1月17日”},{“heading”:“2月24日”},{“heading”:“3月3日”}]谢谢,在使用该修复程序后,我在控制台中遇到了一个新错误,char1没有定义,我认为这意味着超出了范围。Hi steve,我发现这是一个拼写错误,它工作得很好,因此为了结束这一问题,我最好创建一个空白数组并使用push()函数?谢谢你的帮助steve:)每次调用setCategories都会重新绘制图表,所以最好只调用一次。