关于ggplot绘制饼图与PHP中的SQL服务器的数据

时间:2013-12-23 13:27:25

标签: php sql json jqplot

您好我是第一次使用jqplot。我需要通过从数据库中提取数据来显示饼图。我写了查询,我在转换json表中的数据时遇到了问题。我无法生成图表。请在这方面帮助我。这是我的代码。

PHP代码

<?php require_once('Connections/finalkms.php'); 

mysql_select_db($database_finalkms, $finalkms);
$query_getdatagraoh = "SELECT Compliancestatus,value FROM COUNT_VALUE WHERE Zone='PZ' and country='AU' and `Compliancestatus` is not null";
$getdatagraoh = mysql_query($query_getdatagraoh, $finalkms) or die(mysql_error());
$row_getdatagraoh = mysql_fetch_assoc($getdatagraoh);
$totalRows_getdatagraoh = mysql_num_rows($getdatagraoh);
while($row = mysql_fetch_assoc($getdatagraoh)){ 
    $plotgraphs[] = array(
        $row['Compliancestatus'],
        $row['value']
    );
}
header('ContentType: application/json; charset=utf-8');
$data= json_encode($plotgraphs);
?>

java脚本

<script>
$(document).ready(function(){
 var data1 = <?php echo json_encode($plotgraphs); ?>;
 alert(data1);
  var plot1 = jQuery.jqplot ('chart1', [data1], 
    { 
      seriesDefaults: {
        // Make this a pie chart.
        renderer: jQuery.jqplot.PieRenderer, 
        rendererOptions: {
          // Put data labels on the pie slices.
          // By default, labels show the percentage of the slice.
          showDataLabels: true
        }
      }, 
      legend: { show:true, location: 'e' }
    }
  );
});

</script>

HTML

<div id="chart1"></div>

1 个答案:

答案 0 :(得分:0)

如果'Compliancestatus'字段不是数字,则必须使用插件“CategoryAxisRenderer”

xaxis:{
renderer: $.jqplot.CategoryAxisRenderer
}

不要忘记导入它!

<script type="text/javascript" src="./plugins/jqplot.categoryAxisRenderer.min.js"></script>