dreamice2013 2013-12-06 17:49
浏览 45
已采纳

谷歌图表json问题

Help me to solve this issue ,

$sql=mysql_query("select t1.LOGIN,t1.BALANCE,t2.PROFIT from table2 t2 LEFT JOIN table1 t1 on t2.CLOSE_TIME = '1985-11-01 00:00:00' where (t2.CMD=4 or t2.CMD=6 ) AND t2.LOGIN=t1.LOGIN  ") or die(mysql_query());
$table = array();
$table['cols'] = array(
    array('label' => 'No Acc', 'type' => 'string'),
    array('label' => 'Profit', 'type' => 'number'),
    array('label' => 'Loss', 'type' => 'number')
);
$p=0;
$l=0;
$profit=0;
$loss=0;
$rows = array();
while($r=mysql_fetch_array($sql))
{
        if($r[PROFIT]>0)
        {
        $p=$p+1;
        $profit=$profit+$r[PROFIT];
        }
        else if($r[PROFIT]<=0)
        {
        $l=$l+1;
        $loss=$loss+$r[PROFIT];
        }
}
    $noacc=$p+$l;
    $temp = array();
    $temp[] = array('v' => (int) $noacc);
    $temp[] = array('v' => (int) $p);
     $temp[] = array('v' => (int) $l);
    $table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;

I get the following type of json code

{"cols":[{"label":"No Acc","type":"string"},{"label":"Profit","type":"number"},{"label":"Loss","type":"number"}],"rows":[{"c":[{"v":1199},{"v":261},{"v":938}]}]}

i use the following google chart code

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

function drawChart() 
{
    var jsonData = $.ajax({
        url: "cjson.php",
        dataType:"json",
        async: false
}).responseText;

// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);

var options = {'title':'Ticket Sales',
'width':500,
'height':400};

// Instantiate and draw our chart, passing in some options.
 var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options); 
}
</script>
<?
//  PrintHeader($buf,$username,$userid,$session);

  echo("<div id='chart_div'></div>");
?>
</html>

Finally When I load the page in my browser just shows the value 1199 value only in the chart ,Any example would help! Thank you.

  • 写回答

2条回答 默认 最新

  • dongyaofu0599 2013-12-07 00:58
    关注

    Your problem is that the PieCharts expect data in two columns: pie slice label and pie slice value. You need to alter your PHP to return the appropriate structure:

    $sql = mysql_query("select t1.LOGIN,t1.BALANCE,t2.PROFIT from table2 t2 LEFT JOIN table1 t1 on t2.CLOSE_TIME = '1985-11-01 00:00:00' where (t2.CMD=4 or t2.CMD=6 ) AND t2.LOGIN=t1.LOGIN  ") or die(mysql_query());
    $table = array();
    $table['cols'] = array(
        array('label' => 'Category', 'type' => 'string'),
        array('label' => 'Value', 'type' => 'number')
    );
    $p = 0;
    $l = 0;
    $profit = 0;
    $loss = 0;
    while ($r = mysql_fetch_array($sql)) {
        if ($r[PROFIT] > 0) {
            $p++;
            $profit += $r[PROFIT];
        }
        else if ($r[PROFIT] <= 0) {
            $l++;
            $loss += $r[PROFIT];
        }
    }
    $noacc = $p + $l;
    
    $table['rows'][] = array(array('v' => 'No Acc'), array('v' => $noacc));
    $table['rows'][] = array(array('v' => 'Profit'), array('v' => $p));
    $table['rows'][] = array(array('v' => 'Loss'), array('v' => $l));
    $jsonTable = json_encode($table);
    echo $jsonTable;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)