iOS_danielgindi/Charts饼状图pieChartView用法

#import "PieChartsView.h"
#import "ChartsDemo-Bridging-Header.h"
#import "ChartsDemo-swift.h"
#define kSelfWidth self.frame.size.width
#define kSelfHeight self.frame.size.height

@implementation PieChartsView {
    PieChartView *_pieChartView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self uiConfig];
    }
    return self;
}

- (void)uiConfig {
    _pieChartView = [[PieChartView alloc] init];
    _pieChartView.backgroundColor = [UIColor cyanColor];
    _pieChartView.frame = CGRectMake(0, 0, kSelfWidth, kSelfHeight);
    _pieChartView.holeRadiusPercent = 0.4; // 中间空心半径比
    _pieChartView.transparentCircleRadiusPercent = 0;
    _pieChartView.chartDescription.enabled = NO;
    [_pieChartView setExtraOffsetsWithLeft:5.f top:10.f right:5.f bottom:5.f];
    
    ChartLegend *l = _pieChartView.legend;
    l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
    l.verticalAlignment = ChartLegendVerticalAlignmentTop;
    l.orientation = ChartLegendOrientationVertical;
    l.formSize = 10;
    l.font = [UIFont systemFontOfSize:10];
    [self addSubview:_pieChartView];
    
    // 开始设值
    NSDictionary *statistics = @{
                                 @"初步接洽":[NSNumber numberWithLong:27],
                                 @"需求确定":[NSNumber numberWithLong:11],
                                 @"方案报价":[NSNumber numberWithLong:8],
                                 @"谈判审核":[NSNumber numberWithLong:3],
                                 };
    NSMutableArray *values = [NSMutableArray array];
    
    //累加总数, 为了算百分比
    double total = 0;
    for (NSNumber *num in statistics.allValues) {
        total += [num doubleValue];
    }
    
    // 循环字典数组 创建data
    for (NSString *str in statistics.allKeys) {
        NSNumber *value = statistics[str];
        if ([value intValue] != 0) {
            [values addObject:[[PieChartDataEntry alloc] initWithValue:[value doubleValue]/total*100 label:str]];
        }
    }
    
    // 循环data数组 创建Set
    if (values.count > 0) {
        PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:values label:@""];
        dataSet.sliceSpace = 2.0;
        
        //设置饼块颜色数组
        dataSet.colors = @[[UIColor blueColor], [UIColor brownColor], [UIColor redColor], [UIColor orangeColor]];
        
        PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
        
        NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
        pFormatter.numberStyle = NSNumberFormatterPercentStyle;
        pFormatter.maximumFractionDigits = 1;
        pFormatter.multiplier = @1.f;
        [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:pFormatter]];
        [data setValueFont:[UIFont systemFontOfSize:8]];
        [data setValueTextColor:UIColor.whiteColor];
        
        _pieChartView.data = data;
        [_pieChartView highlightValues:nil];
    }
}


@end

github demo

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫小言mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值