作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在为用户在仪表板上创建一个饼图。我尝试过以下代码:
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getData.php",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, {width: 400, height: 240});
}
</script>
getData.php
$sent = mysqli_query($con,"SELECT name FROM table_name WHERE user_id='$user_id'");
$num_rows_sent = mysqli_num_rows($sent);
$viewed = mysqli_query($con,"SELECT viewed FROM table_name WHERE user_id='$user_id' AND viewed= 1 ");
$num_rows_viewed= mysqli_num_rows($viewed);
$accepted = mysqli_query($con,"SELECT accepted FROM table_name WHERE user_id='$user_id' AND accepted= 1 ");
$num_rows_accepted= mysqli_num_rows($accepted);
$declined = mysqli_query($con,"SELECT declined FROM table_name WHERE user_id='$user_id' AND declined= 1 ");
$num_rows_declined= mysqli_num_rows($declined);
$suggest = mysqli_query($con,"SELECT In_Discussion FROM table_name WHERE user_id='$user_id' AND In_Discussion= 1 ");
$num_rows_suggest= mysqli_num_rows($suggest);
$results = array(
"cols" => array(
array("label" => "sent", "type" => "string"),
array("label" => "viewed", "type" => "number"),
array("label" => "accepted", "type" => "number"),
array("label" => "declined", "type" => "number"),
array("label" => "In_Discussion", "type" => "number"),
),
"rows" => array()
);
$results["rows"][] = array("c" => array(
array("v" => $num_rows_sent,"f"=>"sent" ),
array("v" => $num_rows_viewed,"f"=>"viewed"),
array("v" => $num_rows_accepted,"f"=>"accepted"),
array("v" => $num_rows_declined,"f"=>"declined"),
array("v" => $num_rows_suggest,"f"=>"In Discussion"),
));
$json = json_encode($results,JSON_NUMERIC_CHECK);
echo $json;
我得到的 JSON 像 dis:
{
"cols":[
{"label":"sent","type":"string"},
{"label":"viewed","type":"number"},
{"label":"accepted","type":"number"},
{"label":"declined","type":"number"},{"label":"In_Discussion","type":"number"}
],
"rows":[
{ "c":
[
{"v":4,"f":"sent"},{"v":4,"f":"viewed"},{"v":1,"f":"accepted"},{"v":0,"f":"declined"},{"v":2,"f":"In Discussion"}
]
}
]
}
I am getting this chart as output
如何利用上面的数据输出想要的图表?
最佳答案
Google 可视化 DataTables 实际上只是一个二维数组。您需要的只是一个像这样的数组结构:
activity | count
--------------------------
sent | 4
viewed | 4
accepted | 1
declined | 0
... etc
因此 PHP 脚本传递的 JSON 应如下所示:
var jsonData = {
"cols":[
{"label":"activity","type":"string"},
{"label":"count","type":"number"}
],
"rows":[
{ "c": [ {"v":'sent' },{"v" : 4 }]},
{ "c": [ {"v":'viewed' },{"v" : 4 }]},
{ "c": [ {"v":'accepted' },{"v" : 1 }]},
{ "c": [ {"v":'declined' },{"v" : 0 }]},
{ "c": [ {"v":'In Discussion' },{"v" : 2 }]}
]
}
因此,在 PHP 脚本中,创建如下 JSON:
$cols = array(
array('label' => 'activity', 'type' => 'string'),
array('label' => 'count', 'type' => 'number')
);
$rows = array();
$rows[] = array('c' => array(array('v' => 'sent'), array('v' => $num_rows_sent )));
$rows[] = array('c' => array(array('v' => 'viewed'), array('v' => $num_rows_viewed )));
$rows[] = array('c' => array(array('v' => 'accepted'), array('v' => $num_rows_accepted )));
$rows[] = array('c' => array(array('v' => 'declined'), array('v' => $num_rows_declined )));
$rows[] = array('c' => array(array('v' => 'In Discussion'), array('v' => $num_rows_suggest )));
$results = array(
'cols' => $cols,
'rows' => $rows
);
echo json_encode($results);
图表将如下所示 -> http://jsfiddle.net/JP4ZJ/
关于php - 使用 MYSQL 数据库中的数据绘制 google 饼图时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225730/
有人可以帮我理解为什么我的饼图百分比计算不正确吗?看截图: 根据我的计算,如 RHS 上所示,支出百分比应为 24.73%。传递给 Highcharts 的值如下:- 花费:204827099.36-
我正在制作圆环饼图。 我设置数据的颜色并获取钻取(它是保存外部切片的数据和配置的对象)切片的颜色为同一组颜色。我想设置要在向下钻取对象中使用的不同颜色数组。请参阅附图(外层切片有两种颜色:橙色和透明)
我是一名优秀的程序员,十分优秀!