【发布时间】:2016-07-19 17:58:13
【问题描述】:
我有三个数组:
$array1、$array2 和 $array3。每个都有结构为(计数不同):
[{"year":"2016","month":"5","lab_id":"1","count":"19"},{"year":"2016","month":"6","lab_id":"1","count":"1"},{"year":"2016","month":"7","lab_id":"1","count":"8"}]
[{"year":"2016","month":"5","lab_id":"1","count":"26"},{"year":"2016","month":"6","lab_id":"1","count":"34"},{"year":"2016","month":"7","lab_id":"1","count":"30"}]
我正在尝试使用以下代码在 Google 图表中绘制它:
<?php
$rows = array();
$flag = true;
$table = array();
$table['cols'] = array(
array('label' => 'Month', 'type' => 'string'),
array('label' => '<15 min', 'type' => 'number'),
array('label' => '>15 & <60', 'type' => 'number'),
array('label' => '>60', 'type' => 'number'),
);
$rows = array();
$allArray = array();
array_push($allArray,$array1);
array_push($allArray,$array2);
array_push($allArray,$array3);
foreach($allArray as $dataArray) {
$temp = array();
foreach($dataArray as $item) {
$temp[] = array('v' => "{$item["month"]}"."/"."{$item["year"]}");
$temp[] = array('v' => (int) $item["count"]);
}
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>
还有图形脚本:
<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() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: 'User Transaction Statistics',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div"></div>
但我没有得到预期的图表。它应该有日期(5/2016、6/2016、7/2016)作为标签和对应于<15 min、>15 & <60和>60的三个折线图。
但相反,我得到了这个:
【问题讨论】:
标签: php charts google-visualization