How do I force a buffered ChartPanel to redraw ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Rachel M
Posts: 6
Joined: Mon Sep 26, 2005 8:06 pm

How do I force a buffered ChartPanel to redraw ?

Post by Rachel M » Fri Sep 01, 2006 4:58 pm

Hi,
I switched from using a non-buffered to a buffered ChartPanel and it seems like changes I make to the chart randomly get propagated or get thrown out.

Just changing the constructor useBuffer parameter causes all of these changes. Each time I replot something different happens (I put an example of what happens at the bottom)

I've tried to fire every change event I can find but nothing works. Here is the method I added to my subclass of ChartPanel :

Code: Select all

    public void updateChart(){        
        getChart().setNotify(true);  // force a redraw 
        
        //update title
        getChart().titleChanged(
                new TitleChangeEvent(getChart().getTitle()));
        
        //update x axis
        getChart().getXYPlot().axisChanged(
                new AxisChangeEvent(getChart().getXYPlot().getDomainAxis()));
        
        //update y axis
        getChart().getXYPlot().axisChanged(
                new AxisChangeEvent(getChart().getXYPlot().getRangeAxis()));
        
        //general chart change?
        chartChanged(new ChartChangeEvent(this));
        
        //try dataset change
        getChart().getXYPlot().datasetChanged(new DatasetChangeEvent(
                getChart().getXYPlot(), getChart().getXYPlot().getDataset()));
        
        getChart().fireChartChanged();
        
        getChart().plotChanged(new PlotChangeEvent(getChart().getXYPlot()));
    }
................................................................................

Example:
Replot 1: Axes are not updated, title is updated, legend is incorrect
Replot 2: X axis and title are correct, Y axis is incorrect, title and legend are correct
Replot 3: Everything OK
Replot 4: Title isnt wrong, Y axis is OK but the gridlines have disappeared
Replot 5: everything OK

Also the legend sometimes disappears and reappears
..........................................

Thanks so much for any help!
-Rachel

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Fri Sep 01, 2006 5:19 pm

Are you sure you are firing the event/modifying the chart on the AWTEventDispatch thread?

Rachel M
Posts: 6
Joined: Mon Sep 26, 2005 8:06 pm

Post by Rachel M » Fri Sep 01, 2006 5:52 pm

Thanks for replying.

I'm only directly using one thread... the user makes a selection about what they want to plot, and I read it in and make the appropriate changes to my chart with calls to setAxis, setTitle, etc, and then fire off those AxisChangeEvents, etc, on the same thread.

Do I need to create another thread to fire off the events? I just assumed that the ChartPanel took care of that

Thanks so much, I am far from a JFreeChart expert.

By the way, using JFreeChart 1.0.2 with JDK 1.4

Rachel M
Posts: 6
Joined: Mon Sep 26, 2005 8:06 pm

Post by Rachel M » Fri Sep 01, 2006 11:49 pm

OK seems like out of all the things I tried (including just a repaint() ) the only thing that really worked was doing setNotify(false) before any plot-rearranging and then setNotify(true) afterwards.

Thanks!

bluesh34
Posts: 2
Joined: Tue Oct 08, 2013 4:25 pm
antibot: No, of course not.

Re: How do I force a buffered ChartPanel to redraw ?

Post by bluesh34 » Tue Oct 08, 2013 4:34 pm

Not sure if you still need to know this, since you asked the question some 7 years ago, but I think this could be the answer:

I think you need to instantiate a separate renderer for each dataset. This is how i did it (First I've included just the code that I think answers your question and then I've included the whole of my code segment for completeness. Not everything in the whole code segment is relevant, but i thought I'd put it there in case it helps someone. Having 2 separate renderers does seem to make it automatically rescale the graph to include both plots. Before I did this some of the second plot wouldn't be visible if it wasn't in the range of the first plot.):

XYSplineRenderer SR0 = new XYSplineRenderer(1000);

XYSplineRenderer SR1 = new XYSplineRenderer(1000);

chart.getXYPlot().setRenderer(0, SR0);

chart.getXYPlot().setRenderer(1, SR1);

chart.getXYPlot().getRendererForDataset(chart.getXYPlot().getDataset(0))
.setSeriesPaint(0, Color.BLUE);

chart.getXYPlot().getRendererForDataset(chart.getXYPlot().getDataset(1))
.setSeriesPaint(0, Color.RED);

Hope that's useful to you and/or someone else.

(whole code segment)

JFreeChart chart = ChartFactory.createXYLineChart(
"Plot of true and approx solns of y' = -lambda*y",
"time",
"y",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);

ChartPanel chartPanel = new ChartPanel(chart);

chart.getXYPlot().setDataset(1, datasetApprx);

XYSplineRenderer SR0 = new XYSplineRenderer(1000);

XYSplineRenderer SR1 = new XYSplineRenderer(1000);

chart.getXYPlot().setRenderer(0, SR0);

chart.getXYPlot().setRenderer(1, SR1);

chart.getXYPlot().getRendererForDataset(chart.getXYPlot().getDataset(0))
.setSeriesPaint(0, Color.BLUE);

chart.getXYPlot().getRendererForDataset(chart.getXYPlot().getDataset(1))
.setSeriesPaint(0, Color.RED);

Locked