由于SO(This one specifically)和一般的intrawebs上有很多不同的资源,我已经设法用mySQL创建了一个数据透视表。
您可以在此处查看工作示例(有限数据):SQL Fiddle
现在我希望通过PHP获取数据并将其转换为json字符串,以便将其作为ajax响应发送到HTML页面,我试图通过谷歌可视化显示图表(柱形图)就像这样:https://code.google.com/apis/ajax/playground/?type=visualization#column_chart
我的问题是:如何将数据透视表转换为图表的正确格式以便理解它?
来自我的php文件(sph.php)的echo(ed)结果最终结构如下:
[{"ps_job_serial_num":"888888","105":null,"104":"4.00","400":null,"101":"5.00","102":"3.00","204":"6.00","103":"2.00","399":"1.00","300":"1.00","205":"7.00","203":"2.00","404":null,"405":null,"202":null,"301":null,"106":null,"304":null,"401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"999999","105":null,"104":"2.00","400":null,"101":"1.00","102":"0.00","204":null,"103":null,"399":"3.00","300":"2.00","205":"3.00","203":null,"404":null,"405":null,"202":null,"301":null,"106":null,"304":null,"401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"111111","105":"1.00","104":"3.00","400":null,"101":"5.00","102":"4.00","204":"10.00","103":"7.00","399":"1.00","300":"2.00","205":null,"203":null,"404":null,"405":null,"202":null,"301":null,"106":null,"304":"1.00","401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"222222","105":null,"104":"1.00","400":null,"101":"1.00","102":"1.00","204":"3.00","103":"1.00","399":null,"300":null,"205":null,"203":null,"404":"3.00","405":null,"202":null,"301":null,"106":null,"304":null,"401":null,"201":null,"402":null,"403":null,"303":null},
{"ps_job_serial_num":"333333","105":"2.00","104":"8.00","400":null,"101":"8.00","102":"9.00","204":"10.00","103":"8.00","399":"2.00","300":"5.00","205":"8.00","203":"8.00","404":null,"405":"7.00","202":"8.00","301":null,"106":"1.00","304":null,"401":null,"201":"6.00","402":null,"403":"6.00","303":null},
{"ps_job_serial_num":"444444","105":"2.00","104":"5.00","400":null,"101":"8.00","102":"9.00","204":"10.00","103":"8.00","399":"4.00","300":"3.00","205":"8.00","203":"5.50","404":"2.00","405":"8.00","202":"8.00","301":"2.00","106":"4.00","304":null,"401":"10.00","201":"10.00","402":"7.00","403":"7.00","303":"2.00"},
etc
但我真的希望数据格式如下:
([
['ps_job_serial_num', '101', '102', '103', '104', '105', '106'],
['888888', 500, 381, 381, 110, 665, 157],
['999999', 750, 396, 381, 115, 594, 173],
['111111', 120, 406, 381, 115, 571, 167],
['222222', 100, 460, 381, 116, 619, 185],
['333333', 430, 401, 381, 120, 642, 195],
['444444', 120, 679, 381, 128, 624, 198]
]);
如何让我的数据看起来像它需要看?
这是我的完整PHP代码:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
$query = "CALL new_procedure()";
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {$results_array[] = $row;}
$jsontable = json_encode($results_array);
echo $jsontable;
?>
以下是完整的HTML页面:
<head>
<title>My realtime chart</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<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: "sph.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.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, {width: 1200, height: 600});
}
</script>
</head>
<body>
<div id="chart_div" style="width: 1200px; height: 600px;"></div>
</body>
</html>
HTML页面上显示的错误是“表没有列”...
更新#1
在进行@asgallant(下面)建议的更改之后,从PHP返回的数据现在看起来像这样:
[
["ps_job_serial_num",105,104,400,101,102,204,103,399,300,205,203,404,405,202,301,106,304,401,201,402,403,303],
["888888",null,4,null,5,3,6,2,1,1,7,2,null,null,null,null,null,null,null,null,null,null,null],
["999999",null,2,null,1,0,null,null,3,2,3,null,null,null,null,null,null,null,null,null,null,null,null],
["111111",1,3,null,5,4,10,7,1,2,null,null,null,null,null,null,null,1,null,null,null,null,null],
["222222",null,1,null,1,1,3,1,null,null,null,null,3,null,null,null,null,null,null,null,null,null,null],
[333333,2,8,null,8,9,10,8,2,5,8,8,null,7,8,null,1,null,null,6,null,6,null],
[444444,null,3,null,2,2,8,3,1,1,null,null,2,6,null,null,null,null,null,null,null,null,null],
[555555,null,2,null,2,2,8,3,2,1,null,null,2,3,null,null,null,null,null,null,null,null,null],
[666666,null,2,null,2,2,7,3,1,2,null,null,2,8,null,null,null,null,null,null,null,null,null],
[777777,null,2,null,2,2,8,3,1,1,null,null,2,7,null,null,null,null,2,null,null,null,null]
]
哪个看起来正确。但是我注意到在第5行之后开始,ps_job_serial_number周围不再有引号。我想知道这是不是让它变得更好?
Chrome控制台的错误消息是:
Uncaught Error: Not an array
(anonymous function)
drawChart
更新#2 将查询更改为仅返回在ps_job_serial_num周围有引号的行(如果导致错误则进行故障排除)。它没有任何区别。错误保持不变:
Uncaught Error: Not an array
(anonymous function)
drawChart
答案 0 :(得分:0)
在PHP中试试这个:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
$query = "CALL new_procedure()";
$result = $mysqli->query($query);
$results_array = Array(Array());
$fillKeys = true;
while ($row = $result->fetch_assoc()) {
$temp = Array();
foreach ($row as $key => val) {
if ($fillKeys) {
$results_array[0][] = $key;
}
$temp[] = $val;
}
$results_array[] = $temp;
$fillKeys = false;
}
$jsontable = json_encode($results_array, JSON_NUMERIC_CHECK);
echo $jsontable;
?>
并在您的javascript中,更改DataTable构造以使用#arrayToDataTable
方法(如果您想将数据放入此特定格式,则需要此方法 - 如果您想使用标准构造函数,则必须知道每列的数据类型是什么):
[编辑 - 添加JSON.parse
以下]
var data = google.visualization.arrayToDataTable(JSON.parse(jsonData));