带有两个系列图表的Google图表(SQL/PHP)
我试图建立与API谷歌图表图表,我很迷茫。。。我在PhpMyAdmin上有一个数据库,其中有两列:numberPeople和date 所以,我想在图表上显示N年的人数和N-1年的其他行 但我不明白。 你能帮我吗?我把我所做的东西放在附录里带有两个系列图表的Google图表(SQL/PHP),php,sql,database,charts,google-visualization,Php,Sql,Database,Charts,Google Visualization,我试图建立与API谷歌图表图表,我很迷茫。。。我在PhpMyAdmin上有一个数据库,其中有两列:numberPeople和date 所以,我想在图表上显示N年的人数和N-1年的其他行 但我不明白。 你能帮我吗?我把我所做的东西放在附录里 //加载可视化API和piechart包。 load('current',{packages:['corechart','bar']}); //将回调设置为在加载Google Visualization API时运行。 google.charts.setO
//加载可视化API和piechart包。
load('current',{packages:['corechart','bar']});
//将回调设置为在加载Google Visualization API时运行。
google.charts.setOnLoadCallback(drawChart);
函数绘图图()
{
//使用从服务器加载的JSON数据创建我们的数据表。
var data=new google.visualization.DataTable();
变量选项={
标题:“NoBre de客户Par Jou'”
宽度:1000,
身高:500,
};
//别忘了检查你的部门ID
var chart=new google.visualization.LineChart(document.getElementById('LineChart_material');
图表绘制(数据、选项);
}
由于您有两个不同的查询,因此需要为每个查询分别加载行,但您仍然需要填充这两列 只需在查询中未使用的列中使用
null
,因此
// old date
while ($r = $anneeN1->fetch()) {
$temp = array();
$temp[] = array('v' => $r['date']);
$temp[] = array('v' => null);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$rows[] = array('c' => $temp);
}
// now date
while ($r = $anneeN->fetch()) {
$temp = array();
$temp[] = array('v' => $r['date']);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$temp[] = array('v' => null);
$rows[] = array('c' => $temp);
}
注意:可能需要先加载旧的,所以日期顺序正确
编辑
为了比较一年比一年,首先,我们需要使用真实的日期,而不是字符串
将第一列从'string'
更改为'date'
array('label' => 'Date', 'type' => 'date'),
这里
$table = array();
$table['cols'] = array(
array('label' => 'Date', 'type' => 'date'),
array('label' => 'Nbr de personne en '.$date_now, 'type' => 'number'),
array('label' => 'Nbr de personne en '.$date_old, 'type' => 'number')
);
接下来,x轴上的日期必须在同一年,因此,我们需要将旧日期更改为当前年份。
但是,我们可以使用格式化的值在工具提示中显示实际日期
'v'
=值,'f'
=格式化值
我们可以使用google的json日期字符串传递真实日期-->“日期(y,m,d,h,n,s)”
因此,在“旧”例程中,我们使用当前年份创建日期,并在格式化值中提供实际日期,
然后以同样的方式格式化“现在”日期
// old date
while ($r = $anneeN1->fetch()) {
// value - old date converted to now year
$rowDate = "Date(".$date_now.", ".((int) date_format($r['date'], 'm') - 1).", ".date_format($r['date'], 'd').", ".date_format($r['date'], 'H').", ".date_format($r['date'], 'i').", ".date_format($r['date'], 's').")";
// formatted value - real value
$rowStr = $date_old."-".date_format($r['date'], 'm')."-".date_format($r['date'], 'd');
$temp = array();
$temp[] = array('v' => $rowDate, 'f' => $rowStr); // <-- use formatted value for real date
$temp[] = array('v' => null);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$rows[] = array('c' => $temp);
}
// now date
while ($r = $anneeN->fetch()) {
$rowDate = "Date(".$date_now.", ".((int) date_format($r['date'], 'm') - 1).", ".date_format($r['date'], 'd').", ".date_format($r['date'], 'H').", ".date_format($r['date'], 'i').", ".date_format($r['date'], 's').")";
$rowStr = $date_now."-".date_format($r['date'], 'm')."-".date_format($r['date'], 'd');
$temp = array();
$temp[] = array('v' => $rowDate, 'f' => $rowStr);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$temp[] = array('v' => null);
$rows[] = array('c' => $temp);
}
编辑2
需要将日期字符串从查询转换为实际日期,请参见-->$realDate
// old date
while ($r = $anneeN1->fetch()) {
$realDate = date_create($r['date']);
// value - old date converted to now year
$rowDate = "Date(".$date_now.", ".((int) date_format($realDate, 'm') - 1).", ".date_format($realDate, 'd').", ".date_format($realDate, 'H').", ".date_format($realDate, 'i').", ".date_format($realDate, 's').")";
// formatted value - real value
$rowStr = $date_old."-".date_format($realDate, 'm')."-".date_format($realDate, 'd');
$temp = array();
$temp[] = array('v' => $rowDate, 'f' => $rowStr);
$temp[] = array('v' => null);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$rows[] = array('c' => $temp);
}
// now date
while ($r = $anneeN->fetch()) {
$realDate = date_create($r['date']);
$rowDate = "Date(".$date_now.", ".((int) date_format($realDate, 'm') - 1).", ".date_format($realDate, 'd').", ".date_format($realDate, 'H').", ".date_format($realDate, 'i').", ".date_format($realDate, 's').")";
$rowStr = $date_now."-".date_format($realDate, 'm')."-".date_format($realDate, 'd');
$temp = array();
$temp[] = array('v' => $rowDate, 'f' => $rowStr);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$temp[] = array('v' => null);
$rows[] = array('c' => $temp);
}
哦,我的上帝,你是耶稣!回答得好!我只有最后一个问题!我想比较这两年,看看这两条曲线,一条在另一条的顶部,看看这一年的数字是增加了还是减少了你明白我的意思吗?我有改变的方法,看看。。。你能告诉我什么对我的项目更好吗?(我已经编辑了我的问题)我希望今年同一时期的数据年-1,比较并查看演变…:/哦,谢谢你,但我有一个错误,上面的图片编辑。+1,谢谢你的回答!是的,这是工作!我可以在谷歌图表上私下发送更多问题吗?
// old date
while ($r = $anneeN1->fetch()) {
$realDate = date_create($r['date']);
// value - old date converted to now year
$rowDate = "Date(".$date_now.", ".((int) date_format($realDate, 'm') - 1).", ".date_format($realDate, 'd').", ".date_format($realDate, 'H').", ".date_format($realDate, 'i').", ".date_format($realDate, 's').")";
// formatted value - real value
$rowStr = $date_old."-".date_format($realDate, 'm')."-".date_format($realDate, 'd');
$temp = array();
$temp[] = array('v' => $rowDate, 'f' => $rowStr);
$temp[] = array('v' => null);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$rows[] = array('c' => $temp);
}
// now date
while ($r = $anneeN->fetch()) {
$realDate = date_create($r['date']);
$rowDate = "Date(".$date_now.", ".((int) date_format($realDate, 'm') - 1).", ".date_format($realDate, 'd').", ".date_format($realDate, 'H').", ".date_format($realDate, 'i').", ".date_format($realDate, 's').")";
$rowStr = $date_now."-".date_format($realDate, 'm')."-".date_format($realDate, 'd');
$temp = array();
$temp[] = array('v' => $rowDate, 'f' => $rowStr);
$temp[] = array('v' => (int) $r['nombrePersonne']);
$temp[] = array('v' => null);
$rows[] = array('c' => $temp);
}