简体   繁体   English

用PHP绘制饼图

[英]Flot Pie Chart with PHP

I tried to add load a Pie Chart dynamically using Flot Chart and PHP/MySQL. 我试图使用Flot Chart和PHP / MySQL动态添加加载饼图。

This is my javascript code 这是我的JavaScript代码

 <script id="source" language="javascript" type="text/javascript">


  function fetchData() {
   $.ajax({
      url:      "test.php",
      method:   "GET",
      dataType: "json",
      success:  function(series) {
         var data = [  series ];
         $.plot($("#graph1"), data, {
            pie: { 
                show: true,
                showLabel: true
            },
            legend: {
                show: false 
            }
        });
      }
   });

   setTimeout(fetchData, 1000);
}


$(function () {
    $("#btn").click(function(){
    fetchData();                       
  });   

});

This is my PHP Code 这是我的PHP代码

<?php 
    include("db.php");
    $return_arr = array();
    $sql = mysql_query("SELECT item, COUNT(target) FROM counter WHERE type='video' and date BETWEEN '2011-02-21' and '2011-02-26' GROUP BY target ORDER BY id ASC");
    while($obj = mysql_fetch_object($sql)){
        $return_arr[] = $obj;   
    }
    echo json_encode($return_arr);
?>

This is my button 这是我的按钮

<input type="submit" value="click" id="btn"/>

This is the array that I get when I click the button 这是单击按钮时得到的数组

[{"item":"Final 2010","COUNT(target)":"2"},{"item":"Semi Final 2009","COUNT(target)":"3"}]

When I click the button, it gives me this error: 当我单击按钮时,它给了我这个错误:

An invalid or illegal string was specified" code: "12
[Break On This Error] false 

and Even the pie chart is not loading. 甚至饼图也没有加载。 Can anybody tell me where did I made the wrong. 谁能告诉我我哪里做错了。

When I check in firebug, the array is showing like this 当我检查萤火虫时,数组显示如下

[{"item":"Final 2010","COUNT(target)":"2"},{"item":"Semi Final 2009","COUNT(target)":"3"}]

But when I print it, it shows as [Object object][Object object], I think that would be the problem, anyone know how to fix it 但是当我打印它时,它显示为[Object object] [Object object],我认为这就是问题所在,任何人都知道如何修复它

Thanks 谢谢

I think you need to eval that data (series) 我认为您需要评估该数据(系列)

var data    = eval("(" + series + ")");

That should do it, I think 我认为应该这样做

EDIT 编辑

Added a simple replication on jsfiddle : http://jsfiddle.net/TVAWL/1/ 在jsfiddle上添加了一个简单的复制: http : //jsfiddle.net/TVAWL/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM