Highcharts饼图通过AJAX/JSON:但切片错误和JSON格式不正确


Highcharts Pie Chart via AJAX/JSON: But Slice Errors and Json Format Not Correct?

我是一个php/js/mysql编程新手。

我试图创建一个饼状图在highcharts使用jquery,其中的数据是通过ajax从echo json_encode在php动态发现(包括从mysql的选择查询)。

两个问题:

1)饼状图中到处都有这些尾随的"Slice: 0%"耀斑。不知道这些是从哪里来的,它意味着什么,也不知道如何修复它。

2) Json对我来说是新的。json数据提要似乎通过了(firebug看到了),但格式是这样的。我试着把它归结为名字和百分比数字。就像这样['Pages', 45.0],但不知道怎么做。这是在json/php中完成还是应该在sql查询本身中完成?

[{"contenttype":"BLOGPOST","count(*)":"2076"},{"contenttype":"COMMENT","count(*)":"2054"},{"contenttype":"MAIL","count(*)":"29448"},{"contenttype":"PAGE","count(*)":"33819"}]

任何帮助都非常感谢

highcharts js文件在这里

//Define the chart variable globally,
var chart;
//Request data from the server, add it to the graph and set a timeout to request again
function requestData() {
$.ajax({
    url: 'hc1.php',
    success: function(point) {
        var series = chart.series[0],
            shift = series.data.length > 20; // shift if the series is longer than 20
        // add the point
        chart.series[0].addPoint(point, true, shift);
        // call it again after one second
        setTimeout(requestData, 1000);    
    },
    cache: false
});
}
$(document).ready(function(){
//Create the test  chart
chart = new Highcharts.Chart({
    chart: {
            renderTo: 'mycontainer2',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            events: {load: requestData}
    },
    title: {text: 'Content Types in Wiki'},
tooltip: {formatter: function() {return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';}
  },
plotOptions: {
     pie: {
        allowPointSelect: true,
        cursor: 'pointer',
        dataLabels: {
           enabled: true,
           //color: Highcharts.theme.textColor || '#000000',
           //connectorColor: Highcharts.theme.textColor || '#000000',
           formatter: function() {
              return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
           }
        }
     }
  },

        series: [{
        type: 'pie',
        name: 'Content',
        data: []
    }]
});        
<?php
// Set the JSON header
header("Content-type: text/json");
// Connect to db 
include('dbconnect.php');
// Count version 1 of content types of interest 
$query = ("select contenttype, count(*)
    from CONTENT
    where version='1' and contenttype='page' or contenttype='comment' or contenttype='blogpost' or contenttype='mail' or contenttype='drafts'
    group by CONTENT.contenttype;");
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// create a php array and echo it as json
//$row = mysql_fetch_assoc($result);
//echo json_encode($row);

$results = array(); while ( $row = mysql_fetch_assoc( $result )) { $results[] = $row; }
echo json_encode($results);
?>

第一个问题,如何将数据转换为highcharts将要接受的格式(即数组的数组,[[name,percent],[nextname,percent]等)?我会在PHP中处理:

<snip>
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$total = 0;
$results = array(); 
while ( $row = mysql_fetch_assoc( $result )) { 
  $results[$row["contenttype"] = $row["count()"];
  $total +=  $row["count()"];
}
$forHigh = array();
foreach ($results as $k => $v) {
    $forHigh[]=array($k,($v/$total) * 100); // get percent and push onto array
}
echo json_encode($forHigh); // this should be an array of array

现在我们的JSON返回结构已经为HighCharts准备好了,我们只需要在JSON调用之后调用plot创建一次来创建plot。我会在$的成功回调中做。ajax调用。

$.ajax({
    url: 'hc1.php', 
    success: function(jsonData) {
        chart = new Highcharts.Chart({
        <snip>
                series: [{
                  type: 'pie',
                  name: 'Content',
                  data: jsonData
                }]
        <snip>
    },
    cache: false
});