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

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

问题描述:

我是一个新手php/js/mysql程序员。通过AJAX/JSON的Highcharts饼图:但切片错误和Json格式不正确?

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

两个问题:

1)饼图具有这些尾随 “切片:0%” 无处不耀斑。不知道这些来自哪里,它的意思,也不知道如何解决。

2)Json对我来说是新的。 JSON数据馈送似乎正在通过(萤火虫看到它),但格式看起来像这样。我试图把它归结为名称和百分比数字。像这样['页',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文件是在这里:

<?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); 
?> 
+0

我对你的代码有点困惑。它每秒钟都会向剧情添加一个新的数据点。这对饼图来说会是一件奇怪的事情?你的意思是每秒重绘一次饼图(又似乎是奇怪的)?或者你只是想绘制一次图表? – Mark

+0

嗨马克。只需画一次。 – gregm

+0

这里是柱状图的例子可以帮助你。 http://sgeek.org/create-chart-using-mysql-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调用后调用一次情节创作创造的情节。我会在$ .ajax调用的成功回调中执行此操作。

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

非常感谢Mark。还有一个问题。当我测试php脚本时,使用“php -f hc1.php”,它返回一个空集[]。没有生成json字符串。有什么遗漏吗?谢谢! – gregm

+0

嗯,我可能有一个错误,我不经常写PHP代码(我不能真正测试我写的)。当我键入它时,$ row [“count()”]困扰我(我很惊讶你原来的JSON会把它当作关键名字)。我会修改你的sql查询为“select contenttype,count(*)as num”,然后用num替换“count()”。如果这不起作用,你将不得不逐步调试。 – Mark