简体   繁体   中英

Toggle upper bound of JFreeChart axis range

I have a chart that graphs number of tasks completed versus time. On the Y-axis, 0 is always included and there is a fixed value that will be the maximum number of tasks. As time progresses, the line of the series climbs up to the maximum value. I can do all this.

What I want to be able to do is allow the user to switch between the Y-axis going from 0 to MAX and 0 to the auto range value. This way they can be zoomed in on just the data and not have the upper half of the graph empty when they are still far from the maximum value.

JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "Progress", dataset, false, true, false);
XYPlot plot = chart.getXYPlot();
plot.getRangeAxis().setRange(new Range(0, TOTAL), false, true);

That line allows me to show the entire value range, but I can't manage to get the range back to the auto value that sets the upper bound to just higher than the largest value in the series (The way it would render if you don't set the range at all).

As an expedient, you can preserve the default Range :

Range auto = plot.getRangeAxis().getRange();

and later restore it:

this.add(new JButton(new AbstractAction("Restore") {

    @Override
    public void actionPerformed(ActionEvent e) {
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getRangeAxis().setRange(auto);
    }
}), BorderLayout.SOUTH);

Notional example based on org.jfree.chart.demo.TimeSeriesChartDemo1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM