单击条形图或条形图时,打开新的框架或图表
我有一个条形图,它从mysql数据库中获取数据集,而我一直坚持使用 chartMouseClicked
。正如我在上一个问题中了解到的那样,当前在单击x轴或y轴条上时会打印代码。但是如何设置当我单击x或y轴上的特定条并打开新的框架或图表时。
I have a bar chart which gets the dataset from the mysql database, and I am stuck on using the chartMouseClicked
. Currently the code prints when clicked on x or y axis bars, as I learned in a previous question, but how do I set that when I click a particular bar on x or y axis and open a new frame or chart. Also an information box would be fine.
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();
chartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent event) {
ChartEntity entity = event.getEntity();
System.out.println(entity);
}
@Override
public void chartMouseMoved(ChartMouseEvent event) {
}
在您的计算机中打开 JOptionPane
处理程序,如下面添加到 BarChartDemo1
。该窗格显示一个标签面板,如图所示在此相关的示例中,但是嵌套的 ChartPanel
也可以工作。
Open a JOptionPane
in your handler, as shown below in a ChartMouseListener
added to BarChartDemo1
. The pane displays a panel of labels, as shown in this related example, but a nested ChartPanel
would work as well.
@Override
public void chartMouseClicked(ChartMouseEvent event) {
CategoryItemEntity entity = (CategoryItemEntity) event.getEntity();
JPanel panel = new JPanel(new GridLayout(0, 1));
Comparable row = entity.getRowKey();
Comparable col = entity.getColumnKey();
panel.add(new JLabel(String.valueOf(row)));
panel.add(new JLabel(String.valueOf(col)));
panel.add(new JLabel(String.valueOf(entity.getDataset().getValue(row, col))));
JOptionPane.showMessageDialog(rootPane, panel);
}