I have created a plot in JFreeChart, consisting of a TimeSeriesCollection
and XYPlot
. The horizontal axis uses a DateAxis
, and the vertical (Y) axis is using a NumberAxis
. If the horizontal axis is using all dates for which I have data points I get a proper Y-axis scale when using:
Y1axis.setAutoRangeIncludesZero(false);
Y1axis.setRangeWithMargins(LeftSeries.getRangeBounds(true));
Now I want to create a plot from only a portion of the available time data. For example: I have data points covering four years, but want to create a plot of only the second year. For this I create a date range (MinDate
is the oldest data point I want to show, MaxDate
the most recent data point I want to show):
dateAxis.setMinimumDate(MinDate);
dateAxis.setMaximumDate(MaxDate);
This scales the horizontal axis as desired. However, if I now use the same code to scale the Y axis (Y1axis.setRangeWithMargins(LeftSeries.getRangeBounds(true))
) I get the entire vertical range, suitable for the entire data set. What I want is a vertical scale which suits the date range MinDate
~MaxDate
.
I have tried a few things to get the vertical axis to scale correctly, but none of my attempts was any good. For example:
double LowValue = Y1axis.getLowerBound();
double HighValue = Y1axis.getUpperBound();
Y1axis.setRange(LowValue, HighValue);
did not work. What code should I use to adjust the scale on the vertical (Y) axis to adapt itself to the horizontal time axis data points which are shown in the plot?
You just have to enable auto range for the Y1axis
axis:
Y1axis.setAutoRangeIncludesZero(false);
Y1axis.setAutoRange(true);
Example:
All data:
Subset of values with auto range disabled:
Subset of values with auto range enabled: