I want to create three charts inside a JFrame using the JDBCCategoryDataset
class.
How can I give three separate queries? I gave three different queries in the dataset object, but the result was to display three similar charts. In addition, it executes the last query for all three. I have seen the way it is done with DefaultPieDataset
given default (static) values, but I want to retrieve data dynamically from database.
I know I can create several JDBCCategoryDataset
objects? Is there a better way?
package barchart;
import Extra.OpenFile;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.ui.RefineryUtilities;
public class Chart {
public static void main(String[] args) throws IOException {
try {
JDBCCategoryDataset dataset = new JDBCCategoryDataset(
"jdbc:mysql://localhost:3306/jobfinder",
"com.mysql.jdbc.Driver","giannis", "giannis");
JFrame frame = new JFrame("Charts");
frame.setLayout(new GridLayout(2,1));
String query = "";
query = OpenFile.getContent("query"); //Gets the query from a file
// cause it's big.It's a custom class.
dataset.executeQuery(query);
JFreeChart chart =
ChartFactory.createBarChart3D("Job Statistics", "Posts/Replys", "Quantity",
dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setSeriesPaint(1, Color.CYAN);
renderer.setSeriesPaint(0, Color.DARK_GRAY);
renderer.setDrawBarOutline(false);
renderer.setItemMargin(0.0);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(700, 270));
frame.add(chartPanel);
// query = "SELECT occuDscr Jobs,COUNT(pstOccuId) Quantity FROM occupation_field " +
// "INNER JOIN job_post ON occuId = pstOccuId GROUP BY Jobs";
// dataset.executeQuery(query);
// JFreeChart chart1 =
// ChartFactory.createBarChart3D("Job Statistics", "Posts/Replys", "Quantity",
// dataset, PlotOrientation.VERTICAL, true, true, false);
//
// ChartPanel chartPanel1 = new ChartPanel(chart1, false);
// chartPanel.setPreferredSize(new Dimension(500/2, 270/2));
//
// frame.add(chartPanel1);
//
// query = "SELECT occuDscr Jobs,COUNT(usrOccuId) Quantity FROM occupation_field " +
// "INNER JOIN users ON occuId = usrOccuId GROUP BY Jobs";
// dataset.executeQuery(query);
// JFreeChart chart2 =
// ChartFactory.createBarChart3D("Job Statistics", "Posts/Replys", "Quantity",
// dataset, PlotOrientation.VERTICAL, true, true, false);
//
// ChartPanel chartPanel2 = new ChartPanel(chart2, false);
// chartPanel.setPreferredSize(new Dimension(500/2, 270/2));
//
// frame.add(chartPanel2);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
RefineryUtilities.centerFrameOnScreen(frame);
} catch (FileNotFoundException fe) {
System.out.println("Error Occurred " + fe.getMessage());
System.exit(0);
} catch (IOException ie) {
System.out.println("Error Occurred " + ie.getMessage());
System.exit(0);
} catch (ClassNotFoundException ce) {
System.out.println("Error Occurred " + ce.getMessage());
System.exit(0);
} catch (SQLException se) {
System.out.println("Error Occurred " + se.getMessage());
System.exit(0);
}
}
}
Conceptually, the simplest approach is to give each panel it's own query-specific JDBCCategoryDataset
. In this related example , each ThermometerDemo
panel has it's own DefaultValueDataset
.
Alternatively, define a class having a single JDBCCategoryDataset
that retrieves all the data required to compose three CategoryDataset
instances, and expose static factory methods that each return the individual dataset required for each pie chart.
Addendum: The latter approach assumes that there is some useful heuristic to distinguish among subsets of the full result set. As an example, here's a partial implementation for contiguous subsets:
/** @see https://stackoverflow.com/questions/8835974 */
public class MyCategoryDataset implements CategoryDataset{
private JDBCCategoryDataset set;
private List subList;
private MyCategoryDataset(JDBCCategoryDataset set, int first, int last) {
this.set = set;
subList = set.getRowKeys().subList(first, first);
}
public static CategoryDataset createSubset(
JDBCCategoryDataset set, int first, int last) {
return new MyCategoryDataset(set, first, first);
}
@Override
public Comparable getRowKey(int row) {
return (Comparable) subList.get(row);
}
@Override
public int getRowIndex(Comparable key) {
return subList.indexOf(key);
}
@Override
public List getRowKeys() {
return subList;
}
// TODO
//@Override
//public Comparable getColumnKey(int column) {}
//
//@Override
//public int getColumnIndex(Comparable key) {}
//
//@Override
//public List getColumnKeys() {}
//
//@Override
//public Number getValue(Comparable rowKey, Comparable columnKey) {}
//
//@Override
//public int getRowCount() {}
//
//@Override
//public int getColumnCount() {}
//
//@Override
//public Number getValue(int row, int column) {}
//
//@Override
//public void addChangeListener(DatasetChangeListener listener) {}
//
//@Override
//public void removeChangeListener(DatasetChangeListener listener) {}
//
//@Override
//public DatasetGroup getGroup() {}
//
//@Override
//public void setGroup(DatasetGroup group) {}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.