Adapt BarChart according to changing JTable

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Bennyyy
Posts: 2
Joined: Tue Jul 04, 2017 2:47 pm
antibot: No, of course not.

Adapt BarChart according to changing JTable

Post by Bennyyy » Tue Jul 04, 2017 2:57 pm

Hi together!
I've got a task which I've selected for myself.
Background:
I finally achieved to create a GUI that contains a JTable and JSliders for each Column. With these sliders, I am filtering the content of the JTable over an AbstractTableModel.
The next step for me would be to visualize the results.
That means a stacked bar chart, that contains Max and Minimum values for each column in a graphic way.
I tried to find a solution by googling but I didn't manage to find a proper solution somehow. Furthermore, im really really new to the whole topic of Java scripting.

The few things I managed to collect so far:
-As far as I realized it, the JFreeChart - stackedBarchart is basing on an XYDataset which is similiar to the Dataset in JTable. Therefore, I would have to connect the filtered data from JTable to the StackedBarchart, define my columns of interest and calculate the max and min values of the remaining (filtered) table.

Unfortunately, I am perhaps not smart enough to achieve that.
Could you please help me here?
I would be really really grateful.

Regards
Benny

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Adapt BarChart according to changing JTable

Post by John Matthews » Tue Jul 04, 2017 5:28 pm

ChartFactory.createStackedBarChart() expects a CategoryDataset. As you already extend AbstractTableModel, you could arrange to implement CategoryDataset to allow one model to serve both views. Some examples using JSlider may be found in the demo and here.

Bennyyy
Posts: 2
Joined: Tue Jul 04, 2017 2:47 pm
antibot: No, of course not.

Re: Adapt BarChart according to changing JTable

Post by Bennyyy » Tue Jul 04, 2017 7:48 pm

Hi John,
thank you very very much for your fast response.
Your answer brought me closer to my objective.
I now added a method createCategoryDataset() in my AbstractTableModel which calculates the data for the plot. This method has to be called after a changeEvent of the sliders. The updated CategoryDaset has to be given to the Chart.
Is this a good approach?

The next step would be how I can integrate my StackedBarChart into a Panel. I read something about ChartPanel being an extension of JPanel.
The demos I found for the stackedbarchart are referring to an ApplicationFrame. I am struggling how to modify this that the result is a ChartPanel/JPanel and how I can insert (and update(!)) the CategoryDataset after each change.
I guess that i need to define "Class extends JPanel" where I define that the Chartpanel is the task of this class.

Thank you

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Adapt BarChart according to changing JTable

Post by John Matthews » Wed Jul 05, 2017 1:02 am

Bennyyy wrote:I now added a method createCategoryDataset() in my AbstractTableModel which calculates the data for the plot. This method has to be called after a changeEvent of the sliders. The updated CategoryDaset has to be given to the chart. Is this a good approach?
It has the advantage of simplicity. Note that you can update the model using plot.setDataset(), and the listening plot will update itself in response. More generally, letting your TableModel implement CategoryDataset will allow you fire a DatasetChangeEvent in your implementation of setValueAt(), just as you would fire a TableModelEvent to cause the listening table to update itself.

The demo uses ApplicationFrame as a convenience for managing stand-alone examples that can be easily integrated in a sample gallery, but your application will likely have a different design. You may want to look at some ChartPanel examples going forward. Several focus on updating the chart's appearance using Swing controls.

Locked