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

 ftwinkle 发布于 2023-02-13 11:06

我正在尝试为我的轴标签格式化"k"形式的长数字.例如; 如果我有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

在此输入图像描述

嵌套循环:

在此输入图像描述

1 个回答
  • 我正在发布解决方案,以防将来有人需要它.感谢@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;
    

    2023-02-13 11:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有