OC(廿一):Charts- 餅狀圖的百分數顯示

知識點: 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 <Foundation/Foundation.h>
#import "ChartDemo-Bridging-Header.h"


@interface MyDataFormatter : NSObject

@property (weak, nonatomic) id<IChartValueFormatter> 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 ()<IChartValueFormatter>

@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) ];
    
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在成長過程中聽到最多的勵志的話就是,人最大的敵人就是自己,戰勝自己就是最大的成功。 這句話從小聽到大,在哪都能聽到...
    一路小風吹閱讀 358評論 0 1
  • 目標:1、口語提升:今天-5月份四級課程結束,基本交流沒問題,聽力相同。 2、topik3月份過六級!...
    君_三少閱讀 336評論 0 1
  • 從即日起, 《一代詩人》期刊, 不定期推出, 世界上的一位詩人的, 一首詩歌佳作! 包括各種詩歌體裁,如:格律詩,...
    湖海文學閱讀 335評論 4 13
  • 導語:人類生來孤獨,一個人生,一個人死。但人類總歸是群居動物,長久的孤獨會逼瘋一個人,所以人們總是希望熱鬧的社交的...
    迷路的貓耳朵閱讀 1,092評論 0 0