Toggle the top of the range of the JFreeChart axis

I have a chart that displays the number of completed tasks versus time. On the Y axis, 0 and a fixed value are always turned on, which will be the maximum number of tasks. Over time, the series line rises to its maximum value. I can do it all.

What I want to do is let the user switch between the Y axis going from 0 to MAX and 0 to the auto range value. Thus, they can only be increased according to the data and should not have the upper half of the graph empty if 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); 

This line allows me to display the entire range of values, but I can’t get the range back to the automatic value, which sets the upper limit only higher than the largest value in the series (the way it will be displayed if you do not set the range at all).

+4
source share
1 answer

As appropriate, you can keep the default value of Range :

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

and then 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); 

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

+2
source

Source: https://habr.com/ru/post/1382522/


All Articles