从数据库中提取记录后,我创建了一个数组。数组是这样的:
// $xyz array
Array
(
[f30] => Array
(
[December 2012] => 71
[November 2012] => 70
[October 2012] => 66
)
[f32] => Array
(
[December 2012] => 85
[November 2012] => 83
[October 2012] => 81
)
)
如果它是一个单线图,我可以毫不费力地绘制它。但是,当我尝试绘制多折线图时,我遇到了很多问题。下面是我试图绘制数据的代码:
PHP
$table = array();
$table['cols'] = array(
array('label' => 'Month', 'type' => 'string')
);
$rows = array();
foreach($xyz as $form=>$hhh)
{
$table['cols'][] = array('label'=>$form, 'type'=>'number');
foreach($hhh as $mnth=>$pt)
{
$temp = array();
$temp[] = array('v' => (string) $mnth);
$temp[] = array('v' => (int) $pt);
$rows[] = array('c'=>$temp);
}
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
JS
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: 'Some title',
is3D: 'true',
width: 800,
height: 600
};
var chart = new google.visualization.LineChart(document.getElementById('divID'));
chart.draw(data, options);
}
</script>
我通过更改和数组的位置尝试了各种排列和组合$temp
,但我似乎无法在单个图中绘制两条线。我可能弄乱了数组的位置。$rows
$table['rows']
我设法得到的最接近的是下面的:
但我的预期输出类似于
f30
andf31
代替Sales
and Expenses
, October
,代替, , , November
_December
2004
2005
2006
2007