简体   繁体   English

Highcharts图表未加载数据

[英]Highcharts chart is not loading data

My ajax function in my controller is correctly hitting the page, the chart loads but its blank, I cannot get the data to reach the chart. 我控制器中的ajax函数正确地击中了页面,图表已加载,但其空白,我无法获取数据以到达图表。 My code is below. 我的代码如下。 Using highcharts. 使用高图。 Thanks! 谢谢!

Controller: 控制器:

public function ajax_get_chart() {
      $series_data[] = array('series' => array(array('name' => 'John', 'data' => array(5, 7, 4))));
      $series_data[] = array('series' => array(array('name' => 'Tony', 'data' => array(5, 7, 4))));
      die (json_encode($series_data)); 

    }

Javascript: Javascript:

var chart;

$(document).ready(function() {

    $.ajax({
      url: "/chart/ajax_get_chart", // the URL of the controller action method
      dataType: "json",
      success: function(result) 
      {

        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                defaultSeriesType: 'bar'
            },
            title: {
                text: 'Stacked bar chart'
            },
            xAxis: {
                categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Total fruit consumption'
                }
            },

            legend: {
                backgroundColor: '#FFFFFF',
                reversed: true
            },
            tooltip: {
                formatter: function() {
                    return ''+
                        this.series.name +': '+ this.y +'';
                }
            },
            plotOptions: {
                series: result
            }
        });
      }
    });

});

Passing your data to plotOptions.series won't do anything. 将数据传递到plotOptions.series不会做任何事情。

Your result should go inside "series", eg: 您的结果应放在“系列”中,例如:

chart = new Highcharts.Chart({
            chart: {},
            title: {},
            xAxis: {},
            yAxis: {},
            legend: {},
            tooltip: {},
            plotOptions: {},
            series: result
        });

Providing that your "result" has a format similar to: 假设您的“结果”的格式类似于:

result = [{
        name: "One",
        data: [1, 2, 3, 4]
    }, {
        name: "Two",
        data: [1, 2, 3, 4]
    }
];

You must add series instead of plotOptions in your chart config. 您必须在图表配置中添加series而不是plotOptions

Like this: 像这样:

series: [{
    name: 'test',
    data: result
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM