为轴标签核心图绘制长数字

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

我已经试图遵循这个答案。 但我无法得到格式化的数字。 我还需要做些什么? 请指导我 谢谢。

编辑:

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 

在这里输入图像说明

嵌套循环:

在这里输入图像说明

我发布解决scheme,以防将来有人需要它。 感谢@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; 

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