Java Swing Clickable JFree图表,单击每个条形图或饼图以打开新框架或新图表

时间:2015-03-06 12:46:29

标签: java swing mouseevent jfreechart desktop

我正在为我的最后一年项目制作一个基于java swing的桌面应用程序。我创建了从我的数据库连接的不同类型的图表。

现在想让这个仪表板更具交互性,当点击每个栏时,我想进一步向下钻取打开另一个图表,或者可能是框架或表格以获取详细信息。

有谁可以帮助我如何点击图表上打开新框架或任何新窗口或图表的栏?

下面是我的应用程序截图以及我的某个图表的代码。

谢谢大家

申请的放映

http://www.tiikoni.com/tis/view/?id=3b425ff

enter image description here

http://www.tiikoni.com/tis/view/?id=4336ceb

enter image description here

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

1 个答案:

答案 0 :(得分:2)

您需要向ChartMouseListener添加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) {
    }
});

ChartEntity将是CategoryItemEntity,可用于访问rowKeycolumnKeydataset。然后,您可以打开一个对话框或选项卡以显示找到的数据。