user1092042
user1092042

Reputation: 1295

JFreechart tick units

I am using the following code which i got off the web

TickUnits units = new TickUnits();
DecimalFormat df1 = new DecimalFormat("$0");
DecimalFormat df2 = new DecimalFormat("$0.00");

// we can add the units in any order, the TickUnits collection will
// sort them...
units.add(new NumberTickUnit(100, df1, 0));
units.add(new NumberTickUnit(50, df1, 0));
units.add(new NumberTickUnit(20, df1, 0));
units.add(new NumberTickUnit(10, df1, 0));
units.add(new NumberTickUnit(5, df1, 0));
units.add(new NumberTickUnit(2, df1, 0));
units.add(new NumberTickUnit(1, df1, 0));

As I am very new, I don't understand what this piece of code actually does. I Thought that it sets the values on the y axis, but that does not seem to be the case as there are values exceeding 100 dollars on the y axis. What exactly does this piece of code do. (Also what exactly is tick units. Does it define the labels on the axes or am I mistaken here).

Upvotes: 1

Views: 6251

Answers (1)

trashgod
trashgod

Reputation: 205785

Yes, TickUnits define the format of Axis labels, but a complete explication is beyond the scope of Stackoverflow. The developer guide is dispositive, but it may prove instructive to examine the methods that return a TickUnitSource for a particular Axis subclass. For example, the source code for createIntegerTickUnits() in NumberAxis, along with NumberTickUnitSource

The example cited is flawed in that it ignores the user's locale.

Addendum: If you want localized currency format, override the default Number formatter.

XYPlot plot = chart.getXYPlot();
NumberAxis range = (NumberAxis) plot.getRangeAxis();
range.setNumberFormatOverride(NumberFormat.getCurrencyInstance());

Disclaimer: Not affiliated with Object Refinery Limited; just a satisfied customer and very minor contributor.

Upvotes: 3

Related Questions