使用Jpgraph通过获取Sql表值来绘制分组的条形图。这是我的表格,应该从中绘制图表。 (并非所有数据都显示)
----------------------------------------
| student |
----------------------------------------
| user_ID | category |
----------------------------------------
| MPhil/FT/2011/021 | Full Time |
| MPhil/PT/2013/011 | Part Time |
| MPhil/PT/2012/015 | Part Time |
| MPhil/FT/2012/024 | Full Time |
| MPhil/FT/2013/026 | Full Time |
| MPhil/PT/2011/030 | Part Time |
----------------------------------------
从此处绘制分组条形图,其中x轴显示user_ID中的2011年,2012年,2013年等,以及类别类型“全职”或“零件时间”。例如,对于年份为2011的user_ID,应分别为“全时”计数和“部分时间”计数绘制两个条形。然后是2012年等 如何为此编写SQL查询?我可以用'LIKE'来获得这一年吗? 要使用全时和零件时间来绘制简单的条形图,SQL查询是;
"SELECT count(*) AS num_rows, category FROM student WHERE category ='Full Time' or category= 'Part Time' GROUP BY category"
如何更改此查询以绘制分组条形图,如上所述?如何获取user_ID和Full Time中的年份,兼职按年份计算? (还有更多年,不仅是2011,2012,2013)
答案 0 :(得分:0)
找到解决方案。写了两个查询得到否。每年与全职相关的学生,与每年兼职相关的学生没有。
$sql = mysql_query("SELECT count(*) AS num_rows,LEFT(Right(user_ID,8),4) FROM student WHERE category ='Full Time' GROUP BY LEFT(Right(user_ID,8),4)") or die(mysql_error());
while($row = mysql_fetch_array($sql))
{
$data1[] = $row['num_rows'];
$leg[] = $row['LEFT(Right(user_ID,8),4)'];
}
$sql2 = mysql_query("SELECT count(*) AS num_rows,LEFT(Right(user_ID,8),4) FROM student WHERE category ='Part Time' GROUP BY LEFT(Right(user_ID,8),4)") or die(mysql_error());
while($row2 = mysql_fetch_array($sql2))
{
$data2[] = $row2['num_rows'];
$leg[] = $row2['LEFT(Right(user_ID,8),4)'];
}
所以这里是分组条形图代码的一半
$bplot1 = new BarPlot($data1);
$bplot1->SetFillColor("red");
$bplot2 = new BarPlot($data2);
$bplot2->SetFillColor("blue");