格式化轴标签核心图的长数字 [英] Format long numbers for axis labels core plot

查看:69
本文介绍了格式化轴标签核心图的长数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为轴标签设置 k形式的长数字格式。例如;如果我有 10000 ,我希望将其显示为 10000

I am trying to format long numbers in form of "k" for my axis labels. For example; if I have 10000, I would like it to be displayed as 10k.

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

I have already tried to follow this answer. But I could not manage to get formatted numbers. What I need to do additionally? Please guide me. Thanks.

编辑:

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

嵌套循环:

推荐答案

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

I am posting solution, in case anyone needs it in future. Thanks to @Eric for helping me out.

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行代码:

To use in graph, you need 2 lines of code:

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

这篇关于格式化轴标签核心图的长数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆