How do you change the chart values after it has been display

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Jim McGlaughlin
Posts: 26
Joined: Tue Jul 25, 2006 9:16 pm
Location: Newburyport, MA USA
Contact:

How do you change the chart values after it has been display

Post by Jim McGlaughlin » Mon Aug 07, 2006 10:32 pm

I have a chart displaying correctly the first time.

The chart is created in an action button - GraphNow

The action method of the button


================= code start ================

dataCollection = new TimeSeriesCollection();
TimeSeries[] graphValues = new TimeSeries[ curListIndex] ;

for ( int line = 0; line < curListIndex; line++ )
{
graphValues[ line ] = new TimeSeries( (String)ipList.get(line), Minute.class );

....
int points = workingList.size();
for ( int point=0; point < points; point++ )
{
GraphData gd = (GraphData) workingList.get( point );
Date d = new Date( gd.x * (long)60000) ;
graphValues[ line ].add( new Minute(d) , (double)gd.y );
}
dataCollection.addSeries( graphValues[line] );
}


DateAxis domain = new DateAxis( "Time" );
NumberAxis range = new NumberAxis( yAxisLabel );
XYPlot plot = new XYPlot(dataCollection, domain, range, renderer);

.... plot and renderer calls skipped.

JFreeChart chart = new JFreeChart(
chartHeading, JFreeChart.DEFAULT_TITLE_FONT, plot, true
);
chart.setBackgroundPaint(Color.white);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPopupMenu(null);

getContentPane().add(chartPanel);

================= code end ================

The above displays the chart the first time it is called - after that the displayed valued do not change.

The user can change the time scale or type of values being graphed.

The result is the local TimeSeries values are updated and the
local TimeSeriersCollection are updated - actually they are both
new instances.

Then a new JFreeChart is created using the 'new' data.

BUT, the chart displayed to the user is not changed.

In the user guide I could not find anything about clearing the old dataset.

Should I be making the JFreeChart have the scope of my applet instead
of new'ing it each time the user clicks on GraphNow?

Any guidance is appreciated.

Thanks.

Jim McGlaughlin
Posts: 26
Joined: Tue Jul 25, 2006 9:16 pm
Location: Newburyport, MA USA
Contact:

Result of additional test

Post by Jim McGlaughlin » Mon Aug 07, 2006 11:02 pm

I changed the code so the graph did not initially display.

Now, when you click on the GraphNow button the chart does not display.

I think the problem involved setting the graph panel via the setContentPane.

I need a way to set a JPanel to be a ChartPanel after it is created - maybe?

JFreeChart chart = new JFreeChart(
chartHeading, JFreeChart.DEFAULT_TITLE_FONT, plot, true
);
ChartPanel chartPanel = new ChartPanel(chart);
getContentPane().add(chartPanel);

I've created a JPanel

private javax.swing.JPanel graphPanel;

but don't know how to assign the ChartPanel to the JPanel,
graphPanel.add( chartPanel ) does not work.

SeanMcCrohan
Posts: 18
Joined: Thu May 11, 2006 11:22 pm

Post by SeanMcCrohan » Mon Aug 07, 2006 11:17 pm

First, DON'T re-instantiate the chart to update it. You can end up with a memory leak that way - because of the wide variety of listeners between charts and chartPanels and so on, the old chart objects are likely to still have references and not be properly disposed. I wrestled with that one for a while...

I doubt this is the best answer, but if you do a removeAll() from your dataset and then re-add the (updated) series, the chart should notice the changes and update.

Jim McGlaughlin
Posts: 26
Joined: Tue Jul 25, 2006 9:16 pm
Location: Newburyport, MA USA
Contact:

Post by Jim McGlaughlin » Tue Aug 08, 2006 1:39 pm

Thanks for your suggestion. I will look for a way to instantiate the jFreeChart stuff at applet startup and then fill in the rest when it is known. I didn't realize the potential leak problems. I am hoping to avoid getting bitmaps of the graphs and use the 'real' thing.

windmill
Posts: 1
Joined: Fri Aug 11, 2006 12:54 am

How to chage the chart style after it was created

Post by windmill » Tue Aug 15, 2006 2:23 am

I have a similar question. After I created a chart, for example, bar chart with labels and legend, and displayed. How shall I display it without labels and legend upon an resize event. The event part is fine with me, I wonder how to draw/display the chart after this event.

Thanks in advance.

Locked