知识点: 1: 初、10:十、20:廿(niàn)、30:卅(sà)、40:卌(xì)

    OC(廿一)-Charts--饼状图的百分数显示 - 图1

    Charts只有 Swift 版本,所以如果想在 OC 中使用必须要使用桥接文件

    Charts 最新版本中对于百分数显示的方式做了改变.通过源码可以看出如下:

    1,通过setValueFormatter设置格式,之前版本直接可以设置为 NSNumberFormatter 类型

    1. /// Sets a custom IValueFormatter for all DataSets this data object contains.
    2. open func setValueFormatter(_ formatter: IValueFormatter?)
    3. {
    4. guard let formatter = formatter
    5. else { return }
    6. for set in dataSets
    7. {
    8. set.valueFormatter = formatter
    9. }
    10. }

    2,最新版本做了调整,需要使用IValueFormatter类型,但是这个 Swift 的类型,在 OC 中要使用IValueFormatter,可以看出该类型为接口,看简单的英文注释可知,需要自定义类,实现该接口.

    源码如下:

    1. //
    2. // IValueFormatter.swift
    3. // Charts
    4. //
    5. // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
    6. // A port of MPAndroidChart for iOS
    7. // Licensed under Apache License 2.0
    8. //
    9. // https://github.com/danielgindi/Charts
    10. //
    11. import Foundation
    12. /// Interface that allows custom formatting of all values inside the chart before they are being drawn to the screen.
    13. ///
    14. /// Simply create your own formatting class and let it implement ValueFormatter.
    15. ///
    16. /// Then override the getFormattedValue(...) method and return whatever you want.
    17. @objc(IChartValueFormatter)
    18. public protocol IValueFormatter : NSObjectProtocol
    19. {
    20. /// Called when a value (from labels inside the chart) is formatted before being drawn.
    21. ///
    22. /// For performance reasons, avoid excessive calculations and memory allocations inside this method.
    23. ///
    24. /// - returns: The formatted label ready for being drawn
    25. ///
    26. /// - parameter value: The value to be formatted
    27. ///
    28. /// - parameter axis: The entry the value belongs to - in e.g. BarChart, this is of class BarEntry
    29. ///
    30. /// - parameter dataSetIndex: The index of the DataSet the entry in focus belongs to
    31. ///
    32. /// - parameter viewPortHandler: provides information about the current chart state (scale, translation, ...)
    33. ///
    34. func stringForValue(_ value: Double,
    35. entry: ChartDataEntry,
    36. dataSetIndex: Int,
    37. viewPortHandler: ViewPortHandler?) -> String
    38. }

    3,自定义类

    .h

    1. //
    2. // MyDataFormatter.h
    3. // ChartDemo
    4. //
    5. // Created by user on 2017/9/26.
    6. // Copyright © 2017年 MK. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "ChartDemo-Bridging-Header.h"
    10. @interface MyDataFormatter : NSObject
    11. @property (weak, nonatomic) id<IChartValueFormatter> delegate;
    12. @property (strong, nonatomic) NSNumberFormatter * formatter;
    13. - (instancetype) initWithNumber :(NSNumberFormatter *)formatter;
    14. @end

    .m

    1. //
    2. // MyDataFormatter.m
    3. // ChartDemo
    4. //
    5. // Created by user on 2017/9/26.
    6. // Copyright © 2017年 MK. All rights reserved.
    7. //
    8. #import "MyDataFormatter.h"
    9. @interface MyDataFormatter()
    10. @end
    11. @implementation MyDataFormatter
    12. - (instancetype)initWithNumber:(NSNumberFormatter *)formatter{
    13. if (self = [super init]) {
    14. self.formatter = formatter;
    15. }
    16. return self;
    17. }
    18. @end

    4,再相应的 controller 中设置数据的格式即可.

    部分代码

    1. //
    2. // ViewController.m
    3. // ChartDemo
    4. //
    5. // Created by user on 2017/9/21.
    6. // Copyright © 2017年 MK. All rights reserved.
    7. //
    8. #import "ViewController.h"
    9. #import "MyDataFormatter.h"
    10. @interface ViewController ()<IChartValueFormatter>
    11. @property (strong, nonatomic) PieChartView * pieView;
    12. @property (strong, nonatomic) PieChartData * pieData;
    13. @property (strong, nonatomic) MyDataFormatter * dataFormatter;
    14. @end
    15. @implementation ViewController
    16. - (MyDataFormatter *)dataFormatter{
    17. if (!_dataFormatter) {
    18. NSNumberFormatter *formatter = [NSNumberFormatter new];
    19. formatter.numberStyle = NSNumberFormatterPercentStyle;
    20. formatter.maximumFractionDigits = 2;//小数位数
    21. formatter.multiplier = @1.0f;
    22. _dataFormatter = [[MyDataFormatter alloc] initWithNumber:formatter];
    23. _dataFormatter.delegate = self;
    24. }
    25. return _dataFormatter;
    26. }
    27. ...
    28. //关键:设置数据格式
    29. PieChartData * data = [[PieChartData alloc] initWithDataSet:dataSet];
    30. [data setValueFormatter:self.dataFormatter.delegate];//设置显示数据格式
    31. //代理方法
    32. - (NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler{
    33. return [self.dataFormatter.formatter stringForObjectValue:@(value) ];
    34. }