views:

473

answers:

4

Hi,

I'm having a big graph created with jfreechart. This chart is too big for the screen, so I would like to put it in a scrollpane. However, when using the scrollbar, the complete graph is redrawn everytime, which makes it extremely slow. Is there a solution for this?

thanks,

Bart

A: 

Glancing at the source reveals that the ChartPanel constructor takes an optional boolean parameter, which instructs it to use an image buffer when repainting. I'd guess that would help.

public ChartPanel(JFreeChart chart, boolean useBuffer);
Jonathan Feinberg
Nope, doesn't work. The constructor which only takes one parameter already sets the 'useBuffer' to true.
Fortega
public static final boolean DEFAULT_BUFFER_USED = true;
Fortega
A: 

With JFreeChart, the complete chart will always be redrawn when an event is fired from the underlying dataset, irrespective of whether the chart is embedded within a JScrollPane or not.

This is particularly noticeable if your chart contains a large number of datapoints and is updated frequently. There are some "kludges" you can attempt you make your UI more responsive; e.g.

  • If you underlying dataset is updated (e.g. SeriesDataset) you could delay firing a SeriesChangeEvent if the chart is not currently being displayed.
  • Similarly you could throttle multiple updates by only periodically firing SeriesChangeEvent every N seconds rather than on every event, the obvious downside being that your UI is less "real-time".
Adamski
+1  A: 

Buffer it yourself. Render the JFreeChart in a panel that is not on a visible panel and use that as your buffer to the panel that is in the scrollpane. Then you can control the repaint events and how often you sync the two panels.

Kelly French
A: 

I actually think that the problem is not because of the panel itself, but of some other events fired somewhere else. I do actually use ChartPanel inside jscrollpanes without that behaviour. My application works smoothly.

Agreeing with Adamsky, my advice is to stop looking the problem in the chart rendering but in your dataset implementation.

Oso
It is not a problem with my dataset. The dataset is never changed anymore after creation. I must say I'm having a huge graph which is a StackedBarChart, with thousands of data items...On smaller graphs, I don't have this problem...
Fortega
not trying to solve your problem but giving options... would it make sense convert it to an image and then displaying it?
Oso