Highchart JSON饼图显示切片未定义

时间:2013-10-12 17:29:20

标签: php jquery mysql json highcharts

这段代码有什么问题只是向我展示了Slice 0%。 这是json的回复,因为我正在使用西里尔文。

[{"\u041b\u043e\u043a\u0430\u043b\u043d\u0438":"1495"},{"\u041c\u0430\u043a\u0435\u0434\u043e\u043d\u0438\u0458\u0430":"1089"},{"\u0411\u0430\u043b\u043a\u0430\u043d":"784"},{"\u0421\u0432\u0435\u0442":"941"},{"\u0421\u043f\u043e\u0440\u0442":"1127"}]

我试图这样:

Cat1 = 1495
Cat2 = 1089

但是简单并没有在饼图上显示任何内容。

charts.js

$(document).ready(function() {
   var options = {
    chart: {
        renderTo: 'chart2',
        plotBackgroundColor: null,
  plotBorderWidth: null,
  plotShadow: false,
  backgroundColor: 'none'
},
title: {
    text: 'Test'
},
tooltip: {
    formatter: function() {
        return '<b style="font-size: 15px;">'+ this.point.name +'</b>: '+ this.y +'';
  }
},
plotOptions: {
    pie: {
        allowPointSelect: true,
        cursor: 'pointer',
        sliced: true,
        dataLabels: {
            enabled: true,
            color: '#000000',
      connectorColor: '#000000',
      formatter: function() {
        return '<b style="font-size: 16px;">'+ this.point.name +'</b>: ' + this.percentage.toFixed(0) + '%';
      }
    }
  }
},
series: [{
    type: 'pie',
    name: 'Test',
    data: []
}]
}
 $.getJSON("ajax.php", function(json) {
options.series[0].data = json;
chart = new Highcharts.Chart(options);
  });
 });

ajax.php

$result = mysql_query(" bla bla "); 
$rows = array();
while($row = mysql_array($result)){
  $rows[] = array($row['cat_name'] => $row['total_items']);
}
header('Content-Type: application/json');
print json_encode($rows);

1 个答案:

答案 0 :(得分:2)

您需要确保数据格式正确,例如:

var data = [];
for( var i = 0; i < json.length; i++ ) { 
    var j = json[i]; 
    for(var k in j) { 
        data.push({ 
            name: k, 
            y: parseFloat(j[k])
        }); 
    } 
} 
options.series[0].data = data;
chart = new Highcharts.Chart(options);
相关问题