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

时间:2013-12-03 13:31:04

标签: ios core-plot nsnumberformatter

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

我已尝试按this回答。但我无法获得格式化的数字。我还需要做什么?请指导我。感谢。

修改

HumanReadableFormatter.h

@interface HumanReadableFormatter : NSNumberFormatter

@end

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

图表代码:

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

enter image description here

嵌套循环:

enter image description here

2 个答案:

答案 0 :(得分:5)

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

<强> HumanReadableFormatter.h

@interface HumanReadableFormatter : NSNumberFormatter
{
    NSNumberFormatter *numberFormatter;
}

@end

<强> 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

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

HumanReadableFormatter *formatter = [[HumanReadableFormatter alloc] init];
YAxis.labelFormatter = formatter;

答案 1 :(得分:2)

由于labelFormatter使用NSFormatter,您的自定义格式化程序应覆盖-stringForObjectValue:,而不是-stringFromNumber:特定于NSNumberFormatter。将参数转换为NSNumber,您可以对方法体使用相同的代码。