1

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.

7
  • What values do you have and what is the range of the axis?
    – user319799
    Feb 24, 2015 at 12:57
  • My values range between about 48 and 74 and at the moment the y-axis is 0-78 Feb 24, 2015 at 13:04
  • Check the following values (just e.g. print them): plot.getDataRange (yAxis), yAxis.getRangeType (), yAxis.getAutoRangeIncludesZero (), yAxis.getFixedAutoRange (), yAxis.getAutoRangeMinimumSize (), yAxis.getAutoRangeStickyZero ().
    – user319799
    Feb 24, 2015 at 13:29
  • plot.getRange(yAxis) = 0.0 and 70.45 (don't know why it starts at 0, none of the data in the dataset is 0) yAxis.getRangeType() = RangeType.FULL yAxis.getAutoRangeIncludesZero() = false yAxis.getFixedAutoRange() = 0.0 yAxis.getAutoRangeMinimumSize() = 1.0E-8 yAxis.getAutoRangeStickyZero() = true Feb 24, 2015 at 13:54
  • Well, if plot range returns zero as lower bound, no wonder the axis includes zero in its autorange. Are you absolutely sure there are actually no zero values in the dataset (maybe somehow "hidden")?
    – user319799
    Feb 24, 2015 at 13:57

1 Answer 1

2

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.