Redraw/repaint a graph
Redraw/repaint a graph
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
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
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Still doesn't update
Hi David,
I have updated the dataset but the chart still doesn't update. This is the chart code.
The chart is returned to this
This is called by init()
The creation of the dataset is via
All the ions array have been updated.
The code for createSeriesCollection()
What am I doing wrong?
Thanks.
Desmond
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;
}
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;
}
Code: Select all
setContentPane(createPanels());
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);
}
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;
}
Thanks.
Desmond
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);
}
Nope not creatChartPanel()
Hi uvoigt,
Thanks for the reply.
This is my createChartPanel():
The dataset has been declared private
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
Thanks.
Desmond
Note: I have updated createChart(), it no longer accepts input
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;
}
Code: Select all
private static IntervalXYDataset dataset;
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()
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;
}
Hi sanrider,
I think the problem is your button code:
By the way it would probably be a better style to not define the dataset static.
I think the problem is your button code:
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.I update the dataset by calling createDataset()
So in my button I have thisCode: Select all
dataset = createDataset()
Code: Select all
dataset = createDataset();
((XYPlot)chart.getPlot()).setDataset(dataset);