1

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).

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user1092042
  • 1,297
  • 5
  • 24
  • 44

1 Answers1

3

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • If it defines the values on the axis why do i observe values beyond this range. – user1092042 Apr 05 '12 at 14:48
  • 1
    A `ValueAxis` uses its `TickUnitSource` to auto-range labels, but you can override this using `setRange()`. Adding more `NumberTickUnit` instances changes the granularity; the bounds are determined by the `Dataset`. – trashgod Apr 05 '12 at 19:28
  • I am creating my own tickuits as mentioned in the program above. I have used the setRange too from 20 to 70. This still does not prevent values beyond 70 to show up. – user1092042 Apr 06 '12 at 02:47
  • I'd suggest editing your question to clarify your requirements and to include an [sscce](http://sscce.org/) that exhibits your findings. – trashgod Apr 06 '12 at 03:58
  • I finally got it. I had to add setLimitAble(true) before using setLimitRange(). Thanks for your help. – user1092042 Apr 06 '12 at 06:16
  • Neither is present in [r1941](http://jfreechart.svn.sourceforge.net/viewvc/jfreechart/trunk/source/org/jfree/chart/axis/ValueAxis.java?revision=1941&view=markup). What version are you using? – trashgod Apr 06 '12 at 14:50
  • 1
    afreechart which is based on jfreechart (android based) most of the functions are the same though one or two may be different – user1092042 Apr 07 '12 at 02:58