How can I have tick marks in Core Plot with custom labels?
Asked Answered
H

4

11

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?

Homophile answered 7/1, 2010 at 3:0 Comment(1)
Hello, How did you manage to get some ticks with your custom labels ? Same problem here, custom labels ok but no ticks. Thanks, LucOuthaul
W
9

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.

Weldonwelfare answered 7/1, 2010 at 4:3 Comment(0)
A
4

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];
Ardys answered 19/6, 2010 at 21:41 Comment(0)
M
1

This worked for me!

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setMaximumFractionDigits:0];  
y.labelFormatter = formatter; 
Milliner answered 28/5, 2010 at 13:54 Comment(0)
S
0

You can also swizzle the method in category like this:

#import "NSNumberFormatter+BigNumber.h"
#import <objc/runtime.h>

static Method origStringFromNumberMethod = nil;

@implementation NSNumberFormatter (BigNumber)

-(NSString *)stringFromBigNumber:(NSNumber*)number{
    int result = 0;
    int level = 1;
    NSString *format = @"";
    if([number integerValue] >= 1000000000) {
        level = 1000000000;
        format = @"b";
    }
    if([number integerValue] >= 1000000) {
        level = 1000000;
        format = @"m";
    }
    if([number integerValue] >= 1000){
        level = 1000;
        format = @"k";
    }
    result = [number integerValue]/level;

    NSString *kValue = [NSString stringWithFormat:@"%d%@",result,format];

    return kValue;
}

+ (void)initialize {
    origStringFromNumberMethod = class_getClassMethod(self, @selector(stringFromNumber:));
    method_exchangeImplementations(origStringFromNumberMethod,
                                   class_getClassMethod(self, @selector(stringFromBigNumber:)));
}

@end
Subtotal answered 30/4, 2014 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.