我在数组中有来自ajax响应的数据,这里是:

"attd": [
  {
    "y": 1,
    "name": "Attendance",
    "sliced": true,
    "selected": true
  },
  {
    "y": 1,
    "name": "SPJ in town",
    "sliced": true,
    "selected": true
  }
]


我想将此结果传递到highchart,这是我的代码:

success: function(rs) {
   var attdChart = $(".attdChart");
   attdChart.unbind();
   var jsonData = JSON.parse(rs);
   if (jsonData.success) {
      var data = jsonData.attd;
      var data_array = [];
      $.each(data, function(key, value){
          data_array.push(value);
      });
      $('#containerPiechart').highcharts({
          chart: {
           plotBackgroundColor: null,
           plotBorderWidth: null,
           plotShadow: false,
           type: 'pie',
           height: 200,
           marginRight: 60
          },
          title: {
           text: ''
          },
          tooltip: {
           pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
          },
          plotOptions: {
            pie: {
              allowPointSelect: true,
              cursor: 'pointer',
              dataLabels: {
                enabled: false,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                   color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
              },
              showInLegend: true
            }
          },


legend: {
            align: 'right',
            verticalAlign: 'top',
            layout: 'vertical',
            x: 0,
            y: 0
          },


    series: data_array
       });
}


我尝试使用console.log,结果如下:
javascript - Ajax响应中的数据无法加载到HighChart中-LMLPHP

它显示结果。图表显示,我以为series: data_array中的错误是由于我在其中输入了硬代码而引起的。但是导致代码:series: data_array,没有图表显示。请帮助我...

最佳答案

这是我的饼图示例代码,我该如何做,

var options1={

        chart:{
            renderTo: 'pie_chart',
            type: 'pie',
            options3d:{
                        enabled: true,
                        alpha: 45,
                        beta: 0
            }

        },
        title: {
            text: 'Title'
        },
         xAxis: {
         categories: []
         },
        yAxis: {

            title: {
                text: 'Time Fixed',

            },
            labels: {
                overflow: 'justify'
            },
            tooltip:{
                formatter: function() {
                 return this.series.name +': '+ this.y;
                 }
            }

        },

        plotOptions: {
              pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    depth: 35,
                    dataLabels: {
                        enabled: true
                    },

                    showInLegend: true
                },
                series: {

                    animation:{ duration: 1000}
                }
        },
        legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 50,
                floating: true,
                borderWidth: 1,
                backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
                shadow: true
        },
        credits: {
            enabled: false
        },
        series: []

    }
    //  chart = new Highcharts.Chart(options1);
    $.getJSON("your_ajax_call_file.php", function(json){
         $.each(json, function(key, value) {
            var series = {}; // <-------------------- moved and changed to object
            series.name = key;
            series.data = value;
            options1.series.push(series); // <-------- pushing series object
        });
        var chart = new Highcharts.Chart(options1);
    });


刚刚尝试了这种方法,它肯定会对您有所帮助。请记住,只是将系列作为数组放入var options1中。

09-19 10:16