AxisChangeEvent when new data changes range.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Worblehat
Posts: 7
Joined: Tue Jun 30, 2015 1:01 pm
antibot: No, of course not.

AxisChangeEvent when new data changes range.

Post by Worblehat » Tue Jun 30, 2015 1:20 pm

Hi,

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;
    }
}

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: AxisChangeEvent when new data changes range.

Post by Naxter » Tue Jun 30, 2015 2:01 pm

Actually the addSeries Method doesnt fire a axisChanged Event. And I do not see an autoAdjust either there.

You can listen to the new Series with a SeriesListener.

Worblehat
Posts: 7
Joined: Tue Jun 30, 2015 1:01 pm
antibot: No, of course not.

Re: AxisChangeEvent when new data changes range.

Post by Worblehat » Wed Jul 01, 2015 2:00 pm

I don't see the auto-adjustment in the addSeries-method either. Nevertheless it definitely happens, I don't know where...

Using a SeriesListener is a good idea for a workaround in my case.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: AxisChangeEvent when new data changes range.

Post by paradoxoff » Wed Jul 01, 2015 3:51 pm

The auto adjustment is happening inside the XYPlot class. The XYPlot is registered as change listener on the datasets and on the axes. If any of the objects fires a change event, the XYPlot updates itself. During this update process, the XYPlot redertermines the value range of any "auto adjustable" axis and sets the range of the axis.
Note that the process will be triggered if one of the datasets are changed and also if one of the axes is changed. In the latter case, an AxisChangeEvent is fired which you catch. In the former case, only a DatasetChagenEvent is fired.

Worblehat
Posts: 7
Joined: Tue Jun 30, 2015 1:01 pm
antibot: No, of course not.

Re: AxisChangeEvent when new data changes range.

Post by Worblehat » Wed Jul 22, 2015 4:32 pm

Thanks for your answers!

I now listen not only to the AxisChangeEvent but also to the DatasetChangeEvents of all data sets in the plot. That seems to work.

Locked