如何使用java中的jfreechart绘制mysql数据库表中数据的散点图。我使用过 Swing 库。
任何链接都会有帮助。我搜索了谷歌但找不到理解的解决方案。
如果您有代码,请提供给我。
实际上我确实做了条形图并使用 jfreechart 绘制了它。
我用于条形图的代码在这里。这里的display3函数显示条形图。
如何修改它以显示散点图?
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:
精炼代码如下并且有效:
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);
JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Paddy Production", dataset);
//CategoryPlot p = chart.getCategoryPlot();
//XYPlot xyplot = (XYPlot)jfreechart.getPlot();
//http://stackoverflow.com/questions/12417732/jfreechart-with-scroller
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();
// JOptionPane.showMessageDialog(MajorUI.this, err.getMessage());
}
}
输出是:http://i58.tinypic.com/29ynsxh.png http://i58.tinypic.com/29ynsxh.png