2013-10-12 70 views
2

這段代碼有什麼問題只是告訴我Slice 0%。 這是從json respod,因爲我使用西里爾文。Highchart JSON餅圖顯示Slice undefined

[{"\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); 

回答

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);