Redraw/repaint a graph

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sandrider
Posts: 9
Joined: Thu Apr 27, 2006 6:40 am

Redraw/repaint a graph

Post by sandrider » Mon Jul 09, 2007 2:09 pm

Hi,

I'm wondering how can I redraw a chart that is already displaying a chart.

I want the user to push a button and recalculate the chart and have it redrawn. Which would I have to change, the IntervalXYDataset, the XYSeriesCollection, the XYPlot or the XYBarRenderer?

I was thinking that I just have to change the IntervalXYDataset and reinvoke the renderer to update the chart. Am I correct?

Thanks.

Desmond

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Jul 09, 2007 2:25 pm

If you update or replace the dataset, the chart will automatically get repainted (if it is displayed in a ChartPanel).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

sandrider
Posts: 9
Joined: Thu Apr 27, 2006 6:40 am

Still doesn't update

Post by sandrider » Wed Jul 11, 2007 2:50 am

Hi David,

I have updated the dataset but the chart still doesn't update. This is the chart code.

Code: Select all

    private JFreeChart createChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYBarChart(
            null,
            "m/z",
            false,
            null,
            dataset,
            PlotOrientation.VERTICAL,
            false,
            false,
            false
        );
        
        chart.setBackgroundPaint(Color.WHITE);
        
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setOutlineVisible(false);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinesVisible(false);
        
        ValueAxis yAxis = plot.getRangeAxis();
        yAxis.setVisible(false);
        
        renderer = (XYBarRenderer) plot.getRenderer();
        
        renderer.setSeriesPaint(0, darkGreen);
        renderer.setSeriesPaint(1, Color.BLUE);
        renderer.setSeriesPaint(2, Color.RED);
        renderer.setSeriesPaint(3, Color.GRAY);
        
        return chart;
    }
The chart is returned to this

Code: Select all

    private JPanel createPanels() {
        JPanel p = new JPanel();
        p.setBackground(Color.WHITE);
        
        p.add(createOptionsPanel(), BorderLayout.NORTH);
        p.add(createSeqPanel(), BorderLayout.CENTER);
        p.add(createChartPanel(), BorderLayout.SOUTH);
        
        return p;
    }
This is called by init()

Code: Select all

setContentPane(createPanels());
The creation of the dataset is via

Code: Select all

    private IntervalXYDataset createDataset() {
        
        calculateMass();
        
        XYSeries aIonsSeries = new XYSeries("aIons");
        XYSeries bIonsSeries = new XYSeries("bIons");
        XYSeries yIonsSeries = new XYSeries("yIons");
        XYSeries allIonsSeries = new XYSeries("allIons");
        
        XYSeriesCollection collection = new XYSeriesCollection();
        collection.removeAllSeries();

        collection = createSeriesCollection(aIonsSeries, aIonsArray, collection);
        collection = createSeriesCollection(bIonsSeries, bIonsArray, collection);
        collection = createSeriesCollection(yIonsSeries, yIonsArray, collection);
        collection = createSeriesCollection(allIonsSeries, allIonsArray, collection);
        
        return new XYBarDataset(collection, 0);
    }
All the ions array have been updated.

The code for createSeriesCollection()

Code: Select all

    private XYSeriesCollection createSeriesCollection(XYSeries series,
            double[][] ions, XYSeriesCollection collection) {
            
        for (int i = 0; i < ions.length; i++)
            series.add(ions[i][0], ions[i][1]);
        
        collection.addSeries(series);
        
        return collection;
    }
What am I doing wrong?

Thanks.

Desmond

uvoigt
Posts: 168
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Post by uvoigt » Wed Jul 11, 2007 9:19 am

Please check your createChartPanel method. Ensure that your chart is displayed in a ChartPanel and NOT in a JPanel. So it probably looks like

Code: Select all

private ChartPanel createChartPanel() {
    IntervalXYDataset dataset = createDataset();
    JFreeChart chart = createChart(dataset);

    return new ChartPanel(chart);
}

   

sandrider
Posts: 9
Joined: Thu Apr 27, 2006 6:40 am

Nope not creatChartPanel()

Post by sandrider » Thu Jul 12, 2007 4:03 am

Hi uvoigt,

Thanks for the reply.

This is my createChartPanel():

Code: Select all

private ChartPanel createChartPanel() {
    dataset = createDataset();
    ChartPanel cp = new ChartPanel(createChart(dataset));
    cp.setPreferredSize(new Dimension(500, 220));
    cp.setBackground(Color.WHITE);
        
    return cp;
}
The dataset has been declared private

Code: Select all

private static IntervalXYDataset dataset;
This is correct isn't it?

Just to clarify, I just have to update the dataset and the chart should update.

I update the dataset by calling createDataset()

So in my button I have this

Code: Select all

dataset = createDataset()
Thanks.

Desmond

Note: I have updated createChart(), it no longer accepts input

Code: Select all

private JFreeChart createChart() {
    JFreeChart chart = ChartFactory.createXYBarChart(
        null,
        "m/z",
        false,
        null,
        dataset,
        PlotOrientation.VERTICAL,
        false,
        false,
        false
    );
        
    chart.setBackgroundPaint(Color.WHITE);
        
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setOutlineVisible(false);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);
        
    ValueAxis yAxis = plot.getRangeAxis();
    yAxis.setVisible(false);
        
    renderer = (XYBarRenderer) plot.getRenderer();
        
    renderer.setSeriesPaint(0, darkGreen);  // a Ions
    renderer.setSeriesPaint(1, Color.BLUE); // b Ions
    renderer.setSeriesPaint(2, Color.RED);  // y Ions
    renderer.setSeriesPaint(3, Color.GRAY); // full spectrum
    renderer.setSeriesVisible(3, false);
        
    return chart;
}

uvoigt
Posts: 168
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Post by uvoigt » Thu Jul 12, 2007 8:53 am

Hi sanrider,

I think the problem is your button code:
I update the dataset by calling createDataset()

So in my button I have this

Code: Select all

dataset = createDataset()
If you create a new dataset after pressing the button you have to assign the dataset to the plot. Otherwise the old dataset is used by the plot.

Code: Select all

dataset = createDataset();
((XYPlot)chart.getPlot()).setDataset(dataset);
By the way it would probably be a better style to not define the dataset static.

sandrider
Posts: 9
Joined: Thu Apr 27, 2006 6:40 am

Thanks!

Post by sandrider » Thu Jul 12, 2007 2:57 pm

Thanks a lot uvoigt, it works!

Locked