格式化轴标签核心图的长数字

Nig*_*ury 2 core-plot nsnumberformatter ios

我正在尝试为我的轴标签格式化"k"形式的长数字.例如; 如果我有10000,我希望它显示为10k.

我已经尝试过这个答案.但我无法获得格式化的数字.我还需要做什么?请指导我.谢谢.

编辑:

HumanReadableFormatter.h

@interface HumanReadableFormatter : NSNumberFormatter

@end
Run Code Online (Sandbox Code Playgroud)

HumanReadableFormatter.m

我修改了上面链接中的代码以满足我的要求.

#import "HumanReadableFormatter.h"

@implementation HumanReadableFormatter

static const char sUnits[] = { '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
static int sMaxUnits = sizeof sUnits - 1;

    -(NSString *) stringForObjectValue:(id)obj
{
    int multiplier =  1000;
    int exponent = 0;

    double bytes = [(NSNumber *)obj doubleValue];

    while ((bytes >= multiplier) && (exponent < sMaxUnits)) {
        bytes /= multiplier;
        exponent++;
    }
    return [NSString stringWithFormat:@"%@ %c", [super stringFromNumber: [NSNumber numberWithDouble: bytes]], sUnits[exponent]];

}

@end
Run Code Online (Sandbox Code Playgroud)

图形代码:

if(!rightY)
    rightY = [[CPTXYAxis alloc]init];
rightY.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
rightY.orthogonalCoordinateDecimal = CPTDecimalFromFloat(totIntervalShown);
rightY.axisConstraints = [CPTConstraints constraintWithUpperOffset:-40];
rightY.coordinate = CPTCoordinateY;
rightY.majorTickLength = 0;
rightY.minorTickLength = 0;
rightY.tickDirection = CPTSignNone;
rightY.plotSpace  = barPlotSpace;

NSNumberFormatter *numFormatter;
if(lowerLock < 1000) //if value is < 1000, normally format it
{
numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
numFormatter.maximumFractionDigits = 2;
numFormatter.minimumFractionDigits = 2;
}
else   //for > 1000, format in human readable form
{
    numFormatter = [[HumanReadableFormatter alloc] init];
//what i need to do here more?
}
rightY.labelFormatter = numFormatter;

//formatted as shown on right side on graph
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

嵌套循环:

在此输入图像描述

Nig*_*ury 5

我正在发布解决方案,以防将来有人需要它.感谢@Eric帮助我.

HumanReadableFormatter.h

@interface HumanReadableFormatter : NSNumberFormatter
{
    NSNumberFormatter *numberFormatter;
}

@end
Run Code Online (Sandbox Code Playgroud)

HumanReadableFormatter.m

@implementation HumanReadableFormatter

static const char sUnits[] = { '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
static int sMaxUnits = sizeof sUnits - 1;

-(id)init
{
    if(self = [super init])
    {
        numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
        numberFormatter.maximumFractionDigits = 1;
        numberFormatter.minimumFractionDigits = 1;

    }
    return self;
}

-(NSString *) stringForObjectValue:(id)obj
{
    int multiplier =  1000;
    int exponent = 0;

    double bytes = [(NSNumber *)obj doubleValue];

    while ((bytes >= multiplier) && (exponent < sMaxUnits)) {
        bytes /= multiplier;
        exponent++;
    }
     NSString *convertedStr = [NSString stringWithFormat:@"%@ %c", [numberFormatter stringFromNumber: [NSNumber numberWithDouble: bytes]], sUnits[exponent]];

    return convertedStr;

}

@end
Run Code Online (Sandbox Code Playgroud)

要在图表中使用,您需要2行代码:

HumanReadableFormatter *formatter = [[HumanReadableFormatter alloc] init];
YAxis.labelFormatter = formatter;
Run Code Online (Sandbox Code Playgroud)