Last active
November 1, 2020 03:51
JDBCTest—JFreeChart examples using JDBCCategoryDataset, JDBCPieDataset and JDBCXYDataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Dimension; | |
import java.awt.EventQueue; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.sql.Timestamp; | |
import java.util.Calendar; | |
import java.util.Random; | |
import javax.swing.JFrame; | |
import org.jfree.chart.ChartFactory; | |
import org.jfree.chart.ChartPanel; | |
import org.jfree.chart.JFreeChart; | |
import org.jfree.chart.axis.CategoryAxis; | |
import org.jfree.chart.axis.CategoryLabelPositions; | |
import org.jfree.chart.plot.CategoryPlot; | |
import org.jfree.chart.plot.PlotOrientation; | |
import org.jfree.data.jdbc.JDBCCategoryDataset; | |
/** | |
* @see https://stackoverflow.com/a/63930834/230513 | |
* @see https://stackoverflow.com/a/24762078/230513 | |
*/ | |
public class JDBCCategoryTest { | |
private static final int N = 16; | |
private static final Random r = new Random(); | |
private void display() { | |
JFrame f = new JFrame("JDBCCategoryTest"); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
JDBCCategoryDataset jds = createDataset(); | |
JFreeChart chart = ChartFactory.createLineChart("Test", "Time", "PV", | |
jds,PlotOrientation.VERTICAL, true, true, false); | |
CategoryPlot plot = (CategoryPlot) chart.getPlot(); | |
CategoryAxis domain = plot.getDomainAxis(); | |
domain.setCategoryLabelPositions(CategoryLabelPositions.UP_45); | |
f.add(new ChartPanel(chart){ | |
@Override | |
public Dimension getPreferredSize() { | |
return new Dimension(640, 480); | |
} | |
}); | |
f.pack(); | |
f.setLocationRelativeTo(null); | |
f.setVisible(true); | |
} | |
private JDBCCategoryDataset createDataset() { | |
try { | |
Connection conn = DriverManager.getConnection( | |
"jdbc:h2:mem:test", "", ""); | |
Statement st = conn.createStatement(); | |
st.execute("create table data(when timestamp, pv float)"); | |
PreparedStatement ps = conn.prepareStatement( | |
"insert into data values (?, ?)"); | |
Calendar c = Calendar.getInstance(); | |
for (int i = 0; i < N; i++) { | |
ps.setTimestamp(1, new Timestamp(c.getTimeInMillis())); | |
ps.setFloat(2, (float)r.nextGaussian() + 2); | |
ps.execute(); | |
c.add(Calendar.SECOND, r.nextInt(60 * 60)); | |
} | |
JDBCCategoryDataset jds = new JDBCCategoryDataset(conn); | |
jds.executeQuery("select when, pv from data"); | |
return jds; | |
} catch (SQLException ex) { | |
ex.printStackTrace(System.err); | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new JDBCCategoryTest().display(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Dimension; | |
import java.awt.EventQueue; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import javax.swing.JFrame; | |
import org.jfree.chart.ChartFactory; | |
import org.jfree.chart.ChartPanel; | |
import org.jfree.chart.JFreeChart; | |
import org.jfree.data.jdbc.JDBCPieDataset; | |
/** | |
* @see https://stackoverflow.com/q/64463340/230513 | |
* @see https://stackoverflow.com/a/63930834/230513 | |
* @see https://stackoverflow.com/a/24762078/230513 | |
*/ | |
public class JDBCPieTest { | |
private static final int N = 3; | |
private void display() { | |
JFrame f = new JFrame("JDBCPieTest"); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
JDBCPieDataset dataset = createDataset(); | |
JFreeChart chart = ChartFactory.createPieChart("Test", dataset); | |
f.add(new ChartPanel(chart) { | |
@Override | |
public Dimension getPreferredSize() { | |
return new Dimension(640, 480); | |
} | |
}); | |
f.pack(); | |
f.setLocationRelativeTo(null); | |
f.setVisible(true); | |
} | |
private JDBCPieDataset createDataset() { | |
try { | |
Connection conn = DriverManager.getConnection( | |
"jdbc:h2:mem:test", "", ""); | |
Statement st = conn.createStatement(); | |
st.execute("create table data(label varchar, val int)"); | |
PreparedStatement ps = conn.prepareStatement( | |
"insert into data values (?, ?)"); | |
addRow(ps, "Apple", 23); | |
addRow(ps, "Banana", 25); | |
addRow(ps, "Cherry", 15); | |
addRow(ps, "Peach", 22); | |
addRow(ps, "Pumpkin", 15); | |
JDBCPieDataset jpd = new JDBCPieDataset(conn); | |
jpd.executeQuery("select label, val from data"); | |
return jpd; | |
} catch (SQLException ex) { | |
ex.printStackTrace(System.err); | |
} | |
return null; | |
} | |
private void addRow(PreparedStatement ps, String label, int value) throws SQLException { | |
ps.setString(1, label); | |
ps.setInt(2, value); | |
ps.execute(); | |
} | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new JDBCPieTest().display(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.EventQueue; | |
import java.sql.Connection; | |
import java.sql.Date; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.Calendar; | |
import java.util.Random; | |
import javax.swing.JFrame; | |
import org.jfree.chart.ChartFactory; | |
import org.jfree.chart.ChartPanel; | |
import org.jfree.chart.JFreeChart; | |
import org.jfree.chart.axis.DateAxis; | |
import org.jfree.chart.plot.PlotOrientation; | |
import org.jfree.chart.plot.XYPlot; | |
import org.jfree.data.jdbc.JDBCXYDataset; | |
/** | |
* @see https://stackoverflow.com/a/24762078/230513 | |
* @see http://stackoverflow.com/a/24592754/230513 | |
*/ | |
public class JDBCXYTest { | |
private static final int N = 30; | |
private static final Random r = new Random(); | |
private void display() { | |
JFrame f = new JFrame("JDBCXYTest"); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
JDBCXYDataset jds = createDataset(); | |
JFreeChart chart = ChartFactory.createScatterPlot("Inventory", | |
"Date", "Count", jds, PlotOrientation.VERTICAL, true, true, false); | |
XYPlot plot = (XYPlot) chart.getPlot(); | |
plot.setDomainAxis(new DateAxis("Date")); | |
f.add(new ChartPanel(chart)); | |
f.pack(); | |
f.setLocationRelativeTo(null); | |
f.setVisible(true); | |
for (int i = 0; i < jds.getItemCount(); i++) { | |
System.out.println(new Date(jds.getX(0, i).longValue())); | |
} | |
} | |
private JDBCXYDataset createDataset() { | |
try { | |
Connection conn = DriverManager.getConnection( | |
"jdbc:h2:mem:test", "", ""); | |
Statement st = conn.createStatement(); | |
st.execute("create table inventory(when date, n1 integer, n2 integer)"); | |
PreparedStatement ps = conn.prepareStatement( | |
"insert into inventory values (?, ?, ?)"); | |
Calendar c = Calendar.getInstance(); | |
for (int i = 0; i < N; i++) { | |
ps.setDate(1, new Date(c.getTimeInMillis())); | |
ps.setInt(2, N / 3 + r.nextInt(N / 3)); | |
ps.setInt(3, N / 2 + r.nextInt(N / 3)); | |
ps.execute(); | |
c.add(Calendar.MONTH, 1); | |
} | |
JDBCXYDataset jds = new JDBCXYDataset(conn); | |
jds.executeQuery("select when, n1, n2 from inventory"); | |
return jds; | |
} catch (SQLException ex) { | |
ex.printStackTrace(System.err); | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new JDBCXYTest().display(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment