Ios 设置轴标签核心图的长数字格式

Ios 设置轴标签核心图的长数字格式,ios,core-plot,nsnumberformatter,Ios,Core Plot,Nsnumberformatter,我正在尝试以“k”的形式为轴标签格式化长数字。例如如果我有10000,我希望它显示为10k 我已经试着按照答案去做了。但我无法获得格式化的数字。我还需要做什么?请引导我。谢谢 编辑: if(!rightY) rightY = [[CPTXYAxis alloc]init]; rightY.labelingPolicy = CPTAxisLabelingPolicyAutomatic; rightY.orthogonalCoordinateDecimal = CPTDecimalFromF

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

我已经试着按照答案去做了。但我无法获得格式化的数字。我还需要做什么?请引导我。谢谢

编辑:

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
HumanReadableFormatter.h

@interface HumanReadableFormatter : NSNumberFormatter

@end
@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
我已经修改了上面链接中的代码以满足我的要求

#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
#导入“HumanReadableFormatter.h”
@HumanReadableFormatter的实现
静态常量char sUnits[]={'\0',K',M',G',T',P',E',Z',Y'};
静态int sMaxUnits=sUnits的大小-1;
-(NSString*)stringForObjectValue:(id)obj
{
整数乘数=1000;
int指数=0;
双字节=[(NSNumber*)obj doubleValue];
而((字节>=乘法器)&&(指数
图形代码:

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
if(!rightY)
rightY=[[CPTXYAxis alloc]init];
rightY.labelingPolicy=CPTAxisLabelingPolicyAutomatic;
rightY.OrthogonalCoordinatedCIMAL=CPTDecimalFromFloat(totIntervalShown);
rightY.axisConstraints=[CPTCConstraints ConstraintTwipperOffset:-40];
rightY.coordination=CPT坐标;
rightY.majorTickLength=0;
rightY.minorTickLength=0;
rightY.tickDirection=CPTSignNone;
rightY.plotSpace=条形图空间;
NSNumberFormatter*numFormatter;
if(lowerLock<1000)//如果值<1000,通常将其格式化
{
numFormatter=[[NSNumberFormatter alloc]init];
[numFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numFormatter.maximumFractionDigits=2;
numFormatter.minimumFractionDigits=2;
}
else//for>1000,格式为人类可读形式
{
numFormatter=[[HumanReadableFormatter alloc]init];
//我在这里还需要做什么?
}
rightY.labelFormatter=numFormatter;
//格式如图右侧所示

嵌套循环:

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

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

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

HumanReadableFormatter.h

@interface HumanReadableFormatter : NSNumberFormatter

@end
@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

谢谢Eric的回复。我试图实现您所说的,但我的代码卡在一些嵌套函数调用中,如图所示。我注意到它卡在formatter method
stringForObjectValue
中的return语句中。您正在递归调用格式化程序来格式化它自己。确定指数后,请使用其他格式设置程序对数字进行格式设置。将其存储在formatter类中的实例变量中,这样您就不必每次格式化数字时都创建一个新的实例变量。查看
CPTCalendarFormatter
CPTTimeFormatter
示例代码。谢谢Eric。你成就了我的一天:)先生,你成就了我的一天。嗨@NightFury,你是如何用间隔和绘图绘制右侧Y轴的。请帮我一下