Matt Williams
Matt Williams

Reputation: 1328

How do I use JFreeChart auto ranges properly?

I was manually creating a range for the y-axis on the graph I was creating, but saw that there were a few properties indicating that JFreeChart can generate them for you.

It already generates a sensible maximum value for the y-axis, but no matter what I try and do, I cannot get the setAutoRangeIncludesZero(boolean) to actually be taken into account when generating the graph.

Here is the relevant code that generates and manipulates the graph:

barChart = ChartFactory.createBarChart("Classifiers' accuracy for " + position + "s", 
           "Missing Value Imputation Method Combination", 
           "Average accuracy (%)", dataset, 
           PlotOrientation.VERTICAL, true, false, false);

plot = (CategoryPlot)barChart.getCategoryPlot();
xAxis = (CategoryAxis)plot.getDomainAxis();
xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

yAxis = (NumberAxis)plot.getRangeAxis();
yAxis.setAutoRangeIncludesZero(false);

barChartImage = new File(position + "-Classification" + ".png");

I have also tried first getting the y-axis as a ValueAxis, using setAutoRange(true) and then casting the y-axis to a NumberAxis and using setAutoRangeIncludesZero(false).

Every time, the y-axis still starts at 0.

Upvotes: 1

Views: 1686

Answers (1)

Matt Williams
Matt Williams

Reputation: 1328

Thanks to @doublep for the answer in a private chat.

I was using a BarChart and it turns out the BarRenderer by default, sets the base of the range to be 0. To override this you just need to get the renderer from the plot object and cast it to type BarRenderer and then call setIncludeBaseInRange(false) and that will prevent the default value of 0 being included in the range.

Upvotes: 2

Related Questions