That was a tricky one.
You construct your ChartPanel with the JFreeChart object returned from the first invocation of createChart(). However, in your overridden paintComponent(...) method, you are using that JFreeChart object which is referenced by the "chart" variable of your SwitchAB class. This is not an issue as long as these objects are the same (not "equal" but "the same").
When you first click, you are calling createChart() again (which, btw, is unnecessary. You only need to exchange the datasets. There is no need to construct anything "new") and construct a new JFreeChart which is now referenced by the "chart" variable of your SwitchAB class, and the ChartPanel is now drawing a different chart than before. However, the chart panel method which is carrying out the zoom is not accessing the new chart objectm but the old one that was used to construct the ChartPanel, and is changing the zoom of the old chart. You do not see this, because the old chart is never drawn.
Here is a better approach:
Code: Select all
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.*;
import javax.swing.*;
import java.io.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.title.*;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.ui.RectangleEdge;
public class ChangeDatasetDemo implements ChartMouseListener{
private DefaultXYDataset dataset1;
private DefaultXYDataset dataset2;
private XYPlot plot;
private JFreeChart chart;
private boolean isA = true;
public ChangeDatasetDemo(){
dataset1 = new DefaultXYDataset();
dataset1.addSeries("Small Values",new double[][]{{1, 2, 3,4}, {5, 1, 7, 3}});
dataset2 = new DefaultXYDataset();
dataset2.addSeries("Large Values",new double[][]{{100, 200, 300, 400}, {100, 800, 300, 600}});
ValueAxis xAxis = new NumberAxis("x");
ValueAxis yAxis = new NumberAxis("y");
XYItemRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesShape(0, new Ellipse2D.Double(-5, -5, 10, 10));
plot = new XYPlot(dataset1, xAxis, yAxis, renderer);
chart = new JFreeChart("XYPlot Demo", new Font("Tahoma", 2, 18), plot, true);
JFrame frame = new JFrame("XY Plot Demo");
ChartPanel cPanel = new ChartPanel(chart);
cPanel.addChartMouseListener(this);
frame.getContentPane().add(cPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void chartMouseClicked(ChartMouseEvent e){
isA = !isA;
if(isA){
plot.setDataset(dataset1);
chart.getTitle().setText("A");
}
else{
plot.setDataset(dataset2);
chart.getTitle().setText("B");
}
}
public void chartMouseMoved(ChartMouseEvent e){
}
public static void main(String[] args) {
ChangeDatasetDemo demo = new ChangeDatasetDemo();
}
}