1

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

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);
    }      
User1920
  • 57
  • 1
  • 7

1 Answers1

2

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.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • Hi Catalina, Thanks for your response. It works as i can see the actions clicked by mouse on my output but I am not sure how to use this as I am using a mysql database for the dataset and clicking upon one of the bar or legend text i wanted a new frame or a new chart panel to open. Will you be able to help me what do i need to do for this ? Thanks – User1920 Mar 06 '15 at 21:30
  • It depends on what you want to do, but there's some good examples [here](http://stackoverflow.com/q/9554636/261156). You may want to update your question or ask different one. – Catalina Island Mar 07 '15 at 13:24
  • Thanks Catalina, I'll create a new question for it. – User1920 Mar 07 '15 at 22:25