Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

java - Bar chart with respect to Database

Here is my table(in mysql) :

mysql> select device from user_management;
+--------+
| device |
+--------+
| APPLE  |
| HTC    |
| HTC    |
| NOKIA  |
| APPLE  |
| APPLE  |
+--------+
6 rows in set (0.00 sec)

Code I have is

<%
String query1 = "select device,count(device) from user_management where  device='"+APPLE+"'";
JDBCCategoryDataset dataset = new JDBCCategoryDataset("jdbc:mysql://localhost:8080/apps","com.mysql.jdbc.Driver","root","root");
dataset.executeQuery(query1);
System.out.println("query1");
JFreeChart chart = ChartFactory.createBarChart3D("Device  Statictics","Device","Count",dataset,PlotOrientation.VERTICAL,true,true,false);
try
{
ChartUtilities.saveChartAsJPEG(new File("D:/dvc.png"),chart,500,400);
}
catch(IOException e)
{
System.out.println(".....there is a problem in your chart. ");
}
%>

And on the basis of device name(APPLE,NOKIA,SAMSUNG) I want to create a bar chart which will show "no. of device VS device". I want to show this in a jsp page.

Any inputs from yourside will be highly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use GROUP BY in your query.

SELECT device, count(device) FROM user_management GROUP BY device;

Use the query to construct a suitable JDBC dataset from org.jfree.data.jdbc, and use the dataset to create your chart.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...