美文网首页
OC(廿一):Charts- 饼状图的百分数显示

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

作者: IMSong | 来源:发表于2017-09-26 13:24 被阅读205次

知识点: 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) ];
    
}

相关文章

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

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

  • Charts-饼状图

    上篇文章已经讲述了折线图的用法这边文章主要来谈饼状图。其实Charts难的部分主要在于配置,所以同样主要说说他的配...

  • echarts设置饼图,曲线阴影效果

    饼图: series: [{ itemStyle: {normal: {//具体决定了饼状图每一份的颜色显示//...

  • Matplotlib库基础-饼形图

    饼状图用来显示部分在总体中的占比。 基础的饼图 #饼形图fruit = ['苹果','橘子','香蕉','芒果']...

  • iOS CGContextRef

    一、绘制饼状图 饼状图的简单实现代码:

  • React中d3(v4)的饼状图绘制与数据更新

    类似页面如下: 平时在各个页面中可能会遇到类似的饼状图显示,这里采用d3 (v4版) 绘制,就上面的饼状图可以抽象...

  • Echarts

    http://echarts.baidu.com/api.html#echarts 柱状图: 饼状图: 饼状图2:...

  • 饼状图

    最近在重构项目,里面涉及到饼状图,就自己写了个,在这里分享一下:github地址 展示一下效果图: 大致思路如下:...

  • 饼状图

  • 饼状图

网友评论

      本文标题:OC(廿一):Charts- 饼状图的百分数显示

      本文链接:https://www.haomeiwen.com/subject/ggdsextx.html