Hi,
I asked:
How do I get a chart to redraw itself? I want it to
redraw after I've updated some date.
The Response was:
If your chart is displayed in a ChartPanel, it should automatically redraw itself when you update the dataset. If it doesn't, then there is a bug somewhere.
Yes, my chart is displayed in a chartpanel, I have included the code to show how I create the dataset below. So, what your telling me is that after I change any data in this method (code below), the charts will be drawn?
Do I just make the dataset method capable of accepting values?
I have done this and when the values of the data is changed, the chart is not redrawing itself.
Is there some other method that needs to be envoked in order for the chart to redraw itself?
code________________________________________________________
public ExhibitA(String title){
super(title);
chart = createCombinedChart();
chart.getPlot().setBackgroundPaint(Color.lightGray);
ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
panel.setPreferredSize(new java.awt.Dimension(900,700));
panel.addChartMouseListener(this);
this.setContentPane(panel);
}//ends ExhibitA method
__________________________________________________
/**
* Creates a combined XYPlot chart.
*/
private JFreeChart createCombinedChart(){
Date date = new Date(102,2,6);
// create subplot 1....................................................................................
XYDataset data1 = this.createDataset1(0);
XYItemRenderer renderer1 = new StandardXYItemRenderer();
TimeSeriesToolTipGenerator toolTip = new TimeSeriesToolTipGenerator("d/M/yy", "0,000.0");
renderer1.setToolTipGenerator(toolTip);
//new VerticalNumberAxis("Units(000s)",Axis.DEFAULT_AXIS_LABEL_FONT,0.0,750.0)
XYPlot subplot1 = new XYPlot(data1, null, new VerticalNumberAxis("Units(000s)"), renderer1);
subplot1.addDomainMarker(new Marker(date.getTime(),Color.red, new java.awt.BasicStroke(2.5f), Color.red, 0.80f));
//......................................................................................................
// create subplot2...
XYDataset data2 = this.createDataset2();
XYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d/M/yy", "0,000.0"));
XYPlot subplot2 = new XYPlot(data2, null,new VerticalNumberAxis("Units(000s)"), renderer2);
subplot2.addDomainMarker(new Marker(date.getTime(),Color.red, new java.awt.BasicStroke(2.5f), Color.red, 0.80f));
// create subplot 3...
XYDataset data3 = this.createDataset3();
XYItemRenderer renderer3 = new StandardXYItemRenderer();
renderer3.setToolTipGenerator(new TimeSeriesToolTipGenerator("d/M/yy", "0,000.0"));
XYPlot subplot3 = new XYPlot(data3, null, new VerticalNumberAxis("Market Share"), renderer3);
subplot3.addDomainMarker(new Marker(date.getTime(),Color.red, new java.awt.BasicStroke(2.5f), Color.red, 0.80f));
// create subplot 4...
XYDataset data4 = this.createDataset4();
XYItemRenderer renderer4 = new VerticalXYBarRenderer(.4); //bar thickness
renderer4.setToolTipGenerator(new TimeSeriesToolTipGenerator("d/M/yy", "0,000.0"));
XYPlot subplot4 = new XYPlot(data4, null,new VerticalNumberAxis("Number of Customers"), renderer4);
subplot4.addDomainMarker(new Marker(date.getTime(),Color.red, new java.awt.BasicStroke(2.5f), Color.red, 0.80f));
// create subplot 5...
XYDataset data5 = this.createDataset5();
XYItemRenderer renderer5 = new VerticalXYBarRenderer(0.4); //bar thickness
renderer5.setToolTipGenerator(new TimeSeriesToolTipGenerator("d/M/yy", "0,000.0"));
XYPlot subplot5 = new XYPlot(data5, null,new VerticalNumberAxis(" Number of Customers "), renderer5);
subplot5.addDomainMarker(new Marker(date.getTime(),Color.red, new java.awt.BasicStroke(2.5f), Color.red, 0.80f));
HorizontalDateAxis monthAxis = new HorizontalDateAxis("March");
// create a parent plot...
CombinedXYPlot plot = new CombinedXYPlot(monthAxis, CombinedXYPlot.VERTICAL);
// add the subplots...
plot.add(subplot1, 2);
plot.add(subplot2, 1);
plot.add(subplot3, 1);
plot.add(subplot4, 1);
plot.add(subplot5, 1);
// return a new chart containing the overlaid plot...
JFreeChart myChart = new JFreeChart("Project 2", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
return myChart;
}
___________________________________________________________
/**
* Creates a sample dataset.
*/
private XYDataset createDataset2(){
// create dataset 2...
BasicTimeSeries series2 = new BasicTimeSeries("325xi", Day.class);
series2.add(new Day(1, SerialDate.MARCH, 2002), 1700.2);
series2.add(new Day(2, SerialDate.MARCH, 2002), 1500.2);
series2.add(new Day(3, SerialDate.MARCH, 2002), 5000.2);
series2.add(new Day(4, SerialDate.MARCH, 2002), 1111.3);
series2.add(new Day(5, SerialDate.MARCH, 2002), 1853.5);
series2.add(new Day(6, SerialDate.MARCH, 2002), 1552.3);
series2.add(new Day(7, SerialDate.MARCH, 2002), 1332.0);
series2.add(new Day(8, SerialDate.MARCH, 2002), 9000.3);
series2.add(new Day(9, SerialDate.MARCH, 2002), 1998.2);
series2.add(new Day(10, SerialDate.MARCH, 2002), 1943.2);
series2.add(new Day(11, SerialDate.MARCH, 2002), 1643.9);
series2.add(new Day(12, SerialDate.MARCH, 2002), 1843.2);
series2.add(new Day(13, SerialDate.MARCH, 2002), 1495.3);
series2.add(new Day(14, SerialDate.MARCH, 2002), 1943.6);
series2.add(new Day(15, SerialDate.MARCH, 2002), 1500.7);
series2.add(new Day(16, SerialDate.MARCH, 2002), 1995.9);
return new TimeSeriesCollection(series2);
}
____________________________________________________________
thanks again,
Hostos
redrawing after changing data values
Re: redrawing after changing data values
If you keep a reference to series2, you can add more data to it at any time. This will cause the following to happen:
(1) The add(...) method in the BasicTimeSeries class will call fireSeriesChanged() which notifies the TimeSeriesCollection that one of its series has been updated, by calling the seriesChanged(...) method.
(2) The seriesChanged(...) method in the TimeSeriesCollection class (inherited from the AbstractSeriesDataset class) will call fireDatasetChanged(), which will notify the plot that the dataset has been changed, by calling the datasetChanged(...) method in the Plot class.
(3) The datasetChanged(...) method in the Plot class then notifies the chart that it has been changed, by calling the plotChanged(...) method in the JFreeChart class.
(4) The JFreeChart class will notify any listeners (including the chart panel) that the chart has been changed by calling the chartChanged(...) method for each listener.
(5) The ChartPanel class will react by redrawing the chart.
It sounds complicated, but it is just a chain of event notifications.
If you write your own dataset class, then you need to make sure that you notify the dataset listeners whenever you change the dataset. If you use the existing dataset classes (like TimeSeriesCollection), then this should be done for you.
Regards,
DG.
(1) The add(...) method in the BasicTimeSeries class will call fireSeriesChanged() which notifies the TimeSeriesCollection that one of its series has been updated, by calling the seriesChanged(...) method.
(2) The seriesChanged(...) method in the TimeSeriesCollection class (inherited from the AbstractSeriesDataset class) will call fireDatasetChanged(), which will notify the plot that the dataset has been changed, by calling the datasetChanged(...) method in the Plot class.
(3) The datasetChanged(...) method in the Plot class then notifies the chart that it has been changed, by calling the plotChanged(...) method in the JFreeChart class.
(4) The JFreeChart class will notify any listeners (including the chart panel) that the chart has been changed by calling the chartChanged(...) method for each listener.
(5) The ChartPanel class will react by redrawing the chart.
It sounds complicated, but it is just a chain of event notifications.
If you write your own dataset class, then you need to make sure that you notify the dataset listeners whenever you change the dataset. If you use the existing dataset classes (like TimeSeriesCollection), then this should be done for you.
Regards,
DG.