知识点: 1: 初、10:十、20:廿(niàn)、30:卅(sà)、40:卌(xì)
Charts只有 Swift 版本,所以如果想在 OC 中使用必须要使用桥接文件
Charts 最新版本中对于百分数显示的方式做了改变.通过源码可以看出如下:
1,通过setValueFormatter设置格式,之前版本直接可以设置为 NSNumberFormatter 类型
/// Sets a custom IValueFormatter for all DataSets this data object contains.
open func setValueFormatter(_ formatter: IValueFormatter?)
{
guard let formatter = formatter
else { return }
for set in dataSets
{
set.valueFormatter = formatter
}
}
2,最新版本做了调整,需要使用IValueFormatter类型,但是这个 Swift 的类型,在 OC 中要使用IValueFormatter,可以看出该类型为接口,看简单的英文注释可知,需要自定义类,实现该接口.
源码如下:
//
// IValueFormatter.swift
// Charts
//
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
// A port of MPAndroidChart for iOS
// Licensed under Apache License 2.0
//
// https://github.com/danielgindi/Charts
//
import Foundation
/// Interface that allows custom formatting of all values inside the chart before they are being drawn to the screen.
///
/// Simply create your own formatting class and let it implement ValueFormatter.
///
/// Then override the getFormattedValue(...) method and return whatever you want.
@objc(IChartValueFormatter)
public protocol IValueFormatter : NSObjectProtocol
{
/// Called when a value (from labels inside the chart) is formatted before being drawn.
///
/// For performance reasons, avoid excessive calculations and memory allocations inside this method.
///
/// - returns: The formatted label ready for being drawn
///
/// - parameter value: The value to be formatted
///
/// - parameter axis: The entry the value belongs to - in e.g. BarChart, this is of class BarEntry
///
/// - parameter dataSetIndex: The index of the DataSet the entry in focus belongs to
///
/// - parameter viewPortHandler: provides information about the current chart state (scale, translation, ...)
///
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String
}
3,自定义类
.h
//
// MyDataFormatter.h
// ChartDemo
//
// Created by user on 2017/9/26.
// Copyright © 2017年 MK. All rights reserved.
//
#import
#import "ChartDemo-Bridging-Header.h"
@interface MyDataFormatter : NSObject
@property (weak, nonatomic) id delegate;
@property (strong, nonatomic) NSNumberFormatter * formatter;
- (instancetype) initWithNumber :(NSNumberFormatter *)formatter;
@end
.m
//
// MyDataFormatter.m
// ChartDemo
//
// Created by user on 2017/9/26.
// Copyright © 2017年 MK. All rights reserved.
//
#import "MyDataFormatter.h"
@interface MyDataFormatter()
@end
@implementation MyDataFormatter
- (instancetype)initWithNumber:(NSNumberFormatter *)formatter{
if (self = [super init]) {
self.formatter = formatter;
}
return self;
}
@end
4,再相应的 controller 中设置数据的格式即可.
部分代码
//
// ViewController.m
// ChartDemo
//
// Created by user on 2017/9/21.
// Copyright © 2017年 MK. All rights reserved.
//
#import "ViewController.h"
#import "MyDataFormatter.h"
@interface ViewController ()
@property (strong, nonatomic) PieChartView * pieView;
@property (strong, nonatomic) PieChartData * pieData;
@property (strong, nonatomic) MyDataFormatter * dataFormatter;
@end
@implementation ViewController
- (MyDataFormatter *)dataFormatter{
if (!_dataFormatter) {
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterPercentStyle;
formatter.maximumFractionDigits = 2;//小数位数
formatter.multiplier = @1.0f;
_dataFormatter = [[MyDataFormatter alloc] initWithNumber:formatter];
_dataFormatter.delegate = self;
}
return _dataFormatter;
}
...
//关键:设置数据格式
PieChartData * data = [[PieChartData alloc] initWithDataSet:dataSet];
[data setValueFormatter:self.dataFormatter.delegate];//设置显示数据格式
//代理方法
- (NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler{
return [self.dataFormatter.formatter stringForObjectValue:@(value) ];
}