简体   繁体   English

切换JFreeChart轴范围的上限

[英]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. 在Y轴上,始终包含0,并且有一个固定值,它将是最大任务数。 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. 我想要做的是允许用户在Y轴从0到MAX和0到自动量程值之间切换。 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

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 . 根据估计的例子org.jfree.chart.demo.TimeSeriesChartDemo1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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