I hava an XYPlot with a NumberAxis as domain axis. I addded an AxisChangeListener to it as I would like to notice changes to the axis' range.
Now adding a dataset to the plot changes the range due to auto-ranging. However the listener is not notified. In contrast, when zooming in/out it is notified.
Is this the expected behaviour? Is there another way to notice changes to the range when new data is added?
Here is a minimal example that demonstrates the problem:
Code: Select all
public class Demo extends JFrame {
public static void main(String[] args) {
Demo demo = new Demo("Demo", "Demo-Chart");
demo.pack();
demo.setVisible(true);
}
public Demo(String applicationTitle, String chartTitle) {
super(applicationTitle);
// Create chart
XYPlot plot = new XYPlot();
plot.setDomainAxis(new NumberAxis());
plot.setRangeAxis(new NumberAxis());
JFreeChart chart = new JFreeChart(chartTitle, plot);
chart.getXYPlot().getDomainAxis().addChangeListener(new AxisChangeListener() {
@Override
public void axisChanged(AxisChangeEvent event) {
System.out.println(((ValueAxis)event.getAxis()).getRange());
}
});
// Add renderer and dataset
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
DefaultXYDataset dataset = new DefaultXYDataset();
plot.setRenderer(0,renderer);
plot.setDataset(0, dataset);
// Populate content pane
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
setContentPane(chartPanel);
// Add data to the dataset to trigger auto-range
dataset.addSeries(0, createData());
}
private double[][] createData() {
double[][] data = new double[2][5];
data[0][0] = 1;
data[0][1] = 2;
data[0][2] = 3;
data[0][3] = 4;
data[0][4] = 5;
data[1][0] = 4;
data[1][1] = 7;
data[1][2] = 2;
data[1][3] = 6;
data[1][4] = 4;
return data;
}
}