gpt4 book ai didi

php - 谷歌图表json问题

转载 作者:行者123 更新时间:2023-11-29 13:23:42 24 4
gpt4 key购买 nike

帮我解决这个问题,

$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;

我得到以下类型的json代码

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

我使用以下谷歌图表代码

<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>

最后,当我在浏览器中加载页面时,仅在图表中显示值 1199 值,任何示例都会有帮助!谢谢。

最佳答案

为什么数据需要如此复杂的结构。您可以从 PHP 脚本返回以下 json:

data = [
['label','value'],
['No Acc',1199],
['Profit',261],
['Loss',938]
];

关于php - 谷歌图表json问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com