Displaying range axis tick labels in millions

Discussion about JFreeChart related to stockmarket charts.
Locked
davesnowdon
Posts: 9
Joined: Tue Jul 06, 2010 7:10 pm
antibot: No, of course not.

Displaying range axis tick labels in millions

Post by davesnowdon » Sun Oct 31, 2010 11:32 pm

I'm using JfreeChart 1.0.13 to display a CombinedDomainXYPlot with price data using a line chart on top and a bar chart displaying volume data underneath.

Since the volumes can be quite large (eg 60 million) a lot of space is being used for the range axis labels.

Is there any way to display the tick labels in millions - for example to display 6m instead of 6,000,000?

thanks

Dave

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Displaying range axis tick labels in millions

Post by skunk » Mon Nov 01, 2010 4:45 pm

You could try something like this:

Code: Select all

rangeAxis.setNumberFormatOverride(new DecimalFormat() {
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
        if (number >= 1000000) {
            toAppendTo.append((int)(number / 1000000));
            toAppendTo.append("m");
        }
        else if (number >= 1000) {
            toAppendTo.append((int)(number / 1000));
            toAppendTo.append("k");
        }
        else {
            toAppendTo.append((int)number);
        }
        return toAppendTo;
    }
});

davesnowdon
Posts: 9
Joined: Tue Jul 06, 2010 7:10 pm
antibot: No, of course not.

Re: Displaying range axis tick labels in millions

Post by davesnowdon » Tue Nov 02, 2010 9:57 pm

Works like a charm - many thanks!
Dave

Locked