views:

587

answers:

3

For my app's graph (line plots) it does not make sense to format the axis labels to tenths. It did not look like there was a way to change this without providing custom labels.

I was able to add custom axis labels based on the sample code in this answer, but the labels do not have tick marks.

Is this an issue (I didn't see anything here) or am I missing something?

+3  A: 

If you want numeric labels with a format different than the default, create an NSNumberFormatter object, set it to whatever format you need, and assign it to the labelFormatter property on the axis.

Check out the CPTimeFormatter class if you need to format the labels as dates and/or times.

Eric Skroch
A: 

This worked for me!

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setMaximumFractionDigits:0];
y.labelFormatter = formatter;

KS222
A: 

Derive a class from NSNumberFormatter (e.g. MyFormatter) and override stringForObjectValue:

- (NSString *)stringForObjectValue:(NSDecimalNumber *)coordinateValue {
    return @"MyLabel";
}

Then set the labelFormatter property of your axis to an instance of MyFormatter, e.g.:

MyFormatter *formatter = [[MyFormatter alloc] init];
x.labelFormatter = formatter;
[formatter release];

The result:

Plot example

Imre Kelényi
I did what you explained but stringForObjectValue is not called...
Luc