BusyBird-Input-Feed
view release on metacpan or search on metacpan
t/samples/stackoverflow.atom view on Meta::CPAN
<category scheme="http://stackoverflow.com/tags" term="swing" />
<category scheme="http://stackoverflow.com/tags" term="jfreechart" />
<category scheme="http://stackoverflow.com/tags" term="scatter-plot" />
<author>
<name>enjal</name>
<uri>http://stackoverflow.com/users/2657277</uri>
</author>
<link rel="alternate" href="http://stackoverflow.com/questions/24591898/scatter-plot-in-jfreechart-from-database" />
<published>2014-07-06T01:20:55Z</published>
<updated>2014-07-06T05:30:13Z</updated>
<summary type="html">
<p>How can i draw scatter plot of datas in mysql database table using jfreechart in java. I have used swing library.
Any link would be helpful. I searched google but couldnot find a understanding solution.
If you have got code just provide me.
Actually i did do barchart and plot it using jfreechart.
The code i used for my barchart is here. Here display3 function displays the barchart.
How can i modify it to display scatter plot?</p>
<pre><code>public void display3() throws SQLException, ClassNotFoundException{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
String JDBC_DRIVER="com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql://localhost/data2";
Connection conn;
Statement stmt;
String USER = "root";
String PASS = "";
try{
Class.forName(JDBC_DRIVER);
conn=DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql="SELECT * FROM `production` WHERE crop_id = 1 AND location_id = 1";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next()){
//String student = rs.getString("studentname");
String yeartext = rs.getString("year_of_production");
//double value = Double.parseDouble(text);
String productiontext = rs.getString("production_amount");
double production = Double.parseDouble(productiontext);
Integer year = Integer.parseInt(yeartext);
dataset.setValue(production, "production", year);
}
JFreeChart chart = ChartFactory.createBarChart("Bar Graph",// Chart Title
"Year", //horizontal axis label
"Paddy Production", // vertical axis label
dataset, //data
PlotOrientation.VERTICAL, //orientation of chart
true, //include legend
false, // tool tips
true);//urls
CategoryPlot p = chart.getCategoryPlot();
ChartPanel chartPanel = new ChartPanel(chart, false);
jPanel9.setLayout(new BorderLayout());
jPanel9.add(chartPanel, BorderLayout.EAST);
jPanel9.add(chartPanel);
SwingUtilities.updateComponentTreeUI(this);
p.setRangeGridlinePaint(blue);
System.out.println("Database created successfully...");
} catch(SQLException se) {
//Handle errors for JDBC
System.out.println("Connect failed ! ");
se.printStackTrace();
}
}
I finally solved my problem:
</code></pre>
<p>The refine code is below and it works: </p>
<pre><code>public void display3() throws SQLException, ClassNotFoundException{
//DefaultCategoryDataset dataset = new DefaultCategoryDataset();
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("production");
String JDBC_DRIVER="com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql://localhost/data2";
Connection conn;
Statement stmt;
String USER = "root";
String PASS = "";
try{
Class.forName(JDBC_DRIVER);
conn=DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql="SELECT * FROM `production` WHERE crop_id = 1 AND location_id = 1";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next()){
//String student = rs.getString("studentname");
String yeartext = rs.getString("year_of_production");
//double value = Double.parseDouble(text);
String productiontext = rs.getString("production_amount");
double production = Double.parseDouble(productiontext);
double year = Double.parseDouble(yeartext);
series.add(year,production) ;
//dataset.addSeries(series);
}
dataset.addSeries(series);
( run in 1.525 second using v1.01-cache-2.11-cpan-e1769b4cff6 )