单击条形图或条形图时打开新框架或图表

Open new frame or chart when a bar or a bar chart is clicked

我有一个从 mysql 数据库获取数据集的条形图,我一直坚持使用 chartMouseClicked。目前,代码会在单击 x 或 y 轴条时打印,正如我在之前的 中了解到的那样,但是当我单击 x 或 y 轴上的特定条并打开新框架或图表时如何设置它。还有一个信息框就好了。

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. The pane displays a panel of labels, as shown in this related exampleChartMouseListener,但嵌套的 ChartPanel 也可以。

@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);
}