Reputation: 57
I am making a desktop application based on java swing for my final year project. I have created different types of charts which is connected from my database.
Now would like to make this dashboard more interactive and when click on the each bar i want to drill down further more to open another chart or maybe a frame or table for detailed information.
Could anyone please help me how can i do to click a bar on the chart which opens a new frame or any new window or chart ?
Below is my screenshot of the application and also code of one of my charts.
Thank you to all
SCREENSHOT OF THE APPLICATION
http://www.tiikoni.com/tis/view/?id=3b425ff
http://www.tiikoni.com/tis/view/?id=4336ceb
//For the Bar Chart
private void lineChart() {
// *************** ADDING BAR CHART FROM DATABASE *****************************
try {
String sql = "select Region, Male, Female from ObeseLondon limit 14";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(MySQL.Connectdb(), sql);
JFreeChart chart = ChartFactory.createBarChart("", "Town", "No. Of Obese People", dataset, PlotOrientation.HORIZONTAL, true, true, true);
chart.setBackgroundPaint(Color.white);
BarRenderer render = null;
//CategoryPlot plot = null;
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.getRenderer().setSeriesPaint(0, Color.green);
plot.getRenderer().setSeriesPaint(1, Color.yellow);
render = new BarRenderer();
org.jfree.chart.ChartFrame chartframe = new org.jfree.chart.ChartFrame("Query Chart", chart);
//chartframe.setVisible(true);
//chartframe.setSize(200,500);
panelBarChart.setLayout(new java.awt.BorderLayout());
ChartPanel chartPanel = new ChartPanel(chart);
panelBarChart.add(chartPanel);
panelBarChart.validate();
//****** Trying Button Click Action for bar chart ********
/*
chart.addChangeListener(chartPanel);
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseMoved(ChartMouseEvent e) {
}
@Override
public void chartMouseClicked(ChartMouseEvent e) {
new JOptionPane().showMessageDialog(null, "You have clicked the bar chart", "Hello", JOptionPane.OK_OPTION);
}
});
*/
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
Upvotes: 1
Views: 2940
Reputation: 7126
You need to add a ChartMouseListener
to your chartPanel
.
chartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent event) {
ChartEntity entity = event.getEntity();
System.out.println(entity);
}
@Override
public void chartMouseMoved(ChartMouseEvent event) {
}
});
The ChartEntity
will be a CategoryItemEntity
that you can use to access the rowKey
, columnKey
and dataset
. Then you can open a a dialog or tab to display the data found.
Upvotes: 2