简体   繁体   English

SQL查询以使用Jpgraph绘制分组的条形图

[英]Sql query to draw a grouped bar chart with Jpgraph

Used Jpgraph to draw a grouped bar chart by taking Sql table values. 使用Jpgraph通过获取Sql表值来绘制分组的条形图。 Here is my table from which the graph should be drawn. 这是应该从中绘制图表的表格。 (Not all data are displayed) (并非显示所有数据)

 ----------------------------------------
 |               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  |
 ----------------------------------------

A grouped bar chart be drawn from this where the x-axis shows the year 2011, 2012, 2013 etc in the user_ID and for the category type 'Full Time' or 'Part Time'. 由此绘制的分组条形图,其中x轴在user_ID中显示2011、2012、2013等年份,类别类型为“全职”或“兼职”。 For example, for no.of user_IDs which have year as 2011 two bars should be drawn separate for 'Full Time' count and 'Part Time' count. 例如,对于以年为2011的user_ID编号,应分别为“全职”计数和“兼职”计数绘制两个条形。 And then for year 2012 etc. How to write the SQL query for this? 然后是2012年等。如何为此编写SQL查询? Can i use 'LIKE' to get the year? 我可以使用“喜欢”来获得年份吗? To take Full time and Part Time count to draw a simple bar chart, the SQL query is; 要使用“全职”和“兼职”计数来绘制简单的条形图,SQL查询为:

"SELECT count(*) AS num_rows, category FROM student WHERE category ='Full Time' or category= 'Part Time' GROUP BY category"

How to change this query to draw a grouped bar chart as explained above? 如上所述,如何更改此查询以绘制分组的条形图? How to get the year in user_ID and Full Time, Part time counts according the year? 如何获得user_ID和“全职”中的年份,兼职按年份计数? (There are more years, not only 2011,2012,2013 ) (还有更多的年份,不仅是2011,2012,2013)

Found the solution. 找到了解决方案。 Wrote two queries to get no. 编写两个查询以获得否。 of students related to full time in each year and no of students related to part time in each year. 每年与全职相关的学生人数,而与兼职相关的学生人数则没有。

$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)'];
}

So here is half of the code of grouped bar graph 所以这是分组条形图的一半代码

$bplot1 = new BarPlot($data1);
$bplot1->SetFillColor("red"); 
$bplot2 = new BarPlot($data2);
$bplot2->SetFillColor("blue"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM