DeathCoder
DeathCoder

Reputation: 165

Well displaying the Y-axis on jFreechart

Please how can I change the Y-axis to have data displayed with the same number of digits.

This is what I have on my graph

and this is the code that produces that part of the graph

final CategoryPlot plot = (CategoryPlot)result.getPlot ();
plot.setBackgroundPaint(Color.BLACK);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange (1.2, 1.3);
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits ());
rangeAxis.setTickUnit (new NumberTickUnit(0.005));

As you can see, I fix the begin at 1.2 and the end at 1.3 and I define the TickUnit at 0.005. So normaly (I think) I should have something gradualy like : 1.2 1.205 1.210 ... 1.305


This is what I would like to obtain

How can I modify the code to obtain the Y-axis with gradues values ? Thanks

I think I have found this solution

DecimalFormat df = new DecimalFormat("0.000");
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange (1.200, 1.300);
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits ());
rangeAxis.setTickUnit (new NumberTickUnit(0.005, df, 0));

it seems to work fine. Thanks

Upvotes: 2

Views: 2812

Answers (1)

GenericJon
GenericJon

Reputation: 8986

The chart axis will accept a java.text.NumberFormat instance, which it will use to format the labels. To do this, try adding these lines to your code:

DecimalFormat newFormat = new DecimalFormat("0.000");
rangeAxis.setNumberFormatOverride(newFormat);

Upvotes: 4

Related Questions