iOS 中饼状图的自定义绘制
前几天有一个需求是制作一个统计工资的饼状图,但是和一般的饼状图不同的是要求该饼状图中心需要有两条文字,功能需求就是这样,先上一张效果图:
因为咱们的饼状图本身只是一个View ,在调用的时候一定是在一个ViewController 里面的,所以我们需要传值,这里我选择用代理:
@protocol PDChartViewDataSource;@interface PDChartView : UIView@property(nonatomic, assign) id<PDChartViewDataSource> dataSource;
接下来的任务就是要确定一下我们需要通过代理向Controller中传入什么东西。
1. slice 的片数
2.每个slice 所占的比例
3.slice的颜色
4. 内部中心圆的半径(没有中间View需求的可以不传)
所以就有了以下一段代码
/**chart中slice的个数*/
- (NSInteger)numberOfSlicesInPDChartView;
/**chart中单个slice所占的比重*/
- (CGFloat)valueForChartViewSliceAtIndex:(NSUInteger)index;
/**chart中单个slice的color*/
- (UIColor *)colorForChartViewSliceAtIndex:(NSUInteger)index;
/**chart内部中心圆的半径*/
- (CGFloat)centerCircleRadius:(PDChartView *)charView width:(CGFloat)innerWidth;
这样我们在 h 文件中操作的东西就够了
m文件中首先我们要重写初始化方法,并在该方法中把中间的View 和饼状图的轮廓大小约束出来 ,为下一步绘制饼状图内部做准备
- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {self.backgroundColor = [UIColor clearColor];midView = [[UIView alloc] init];midView.translatesAutoresizingMaskIntoConstraints = NO;[self addSubview:midView];numLabel = [[UILabel alloc] init];numLabel.translatesAutoresizingMaskIntoConstraints = NO;numLabel.font = [UIFont systemFontOfSize:12];[numLabel setTextColor:[UIColor darkGrayColor]];numLabel.textAlignment = NSTextAlignmentCenter;[midView addSubview:numLabel];totalLabel = [[UILabel alloc] init];totalLabel.translatesAutoresizingMaskIntoConstraints = NO;totalLabel.font = [UIFont systemFontOfSize:12];[totalLabel setTextColor:[UIColor darkGrayColor]];totalLabel.textAlignment = NSTextAlignmentCenter;[midView addSubview:totalLabel];[midView mas_makeConstraints:^(MASConstraintMaker *make) {make.centerX.mas_equalTo(self.mas_centerX);make.centerY.mas_equalTo(self.mas_centerY);}];NSArray *vs = @[@"H:|[numLabel]|",@"H:|[totalLabel]|",@"V:|-[numLabel]-(5)-[totalLabel]-|",];for (int i = 0; i <vs.count; i++) {[midView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vs[i]options:0metrics:nilviews:NSDictionaryOfVariableBindings(midView,numLabel,totalLabel)]];}}return self;
}
实现代理方法
#pragma mark - draw circle
- (void)drawWithContext:(CGContextRef)context circleCenterX:(CGFloat)x circleCenterY:(CGFloat)y radius:(CGFloat)circleRadius start:(CGFloat)start end:(CGFloat)end innerWidth:(CGFloat)width color:(UIColor *)color {CGContextAddArc(context, x, y, circleRadius, start, end, 0);CGContextSetStrokeColorWithColor(context, color.CGColor);CGContextSetLineWidth(context, width);CGContextStrokePath(context);
}#pragma mark - draw line
- (void)drawLineWithContext:(CGContextRef)context width:(CGFloat)width startPointX:(CGFloat)x startPointY:(CGFloat)y endPointX:(CGFloat)endX endPointY:(CGFloat)endY color:(UIColor *)color {CGContextSetLineWidth(context, width);CGContextMoveToPoint(context, x, y);CGContextAddLineToPoint(context, endX, endY);CGContextSetStrokeColorWithColor(context, color.CGColor);CGContextStrokePath(context);
}#pragma - sideLabel
- (void)sideLabel:(CGRect)frame val:(int)val {UILabel *label = [[UILabel alloc] initWithFrame:frame];label.font = [UIFont systemFontOfSize:(13)];[label setTextColor:[UIColor redColor]];label.text = [NSString stringWithFormat:@"%d%%", val];[self addSubview:label];[labelArray addObject:label];
}
具体的绘制方法在drawRect 中绘制(代码量太大就不粘贴了)
最后就是我们的调用了
//
// ViewController.m
// TestNetWork
//
// Created by Mac on 16/7/25.
// Copyright © 2016年 Mac. All rights reserved.
//#import "ViewController.h"
#import "testViewController.h"
#import "PDChartView.h"
#define KChartColorBlue_R66G169B238 [UIColor colorWithRed:0.259 green:0.663 blue:0.933 alpha:1.000]
#define KChartColorGreen_R78G201B72 [UIColor colorWithRed:0.306 green:0.788 blue:0.282 alpha:1.000]
#define KChartColorOrange_R250G157B58 [UIColor colorWithRed:0.980 green:0.616 blue:0.227 alpha:1.000]
#define KChartColorRed_R240G89B89 [UIColor colorWithRed:0.941 green:0.349 blue:0.349 alpha:1.000]
#define KChartColorGray_R226G226B226 [UIColor colorWithWhite:0.886 alpha:1.000]@interface ViewController ()<PDChartViewDataSource>{NSMutableArray *arr;
}
@property(nonatomic,strong) PDChartView *pipeView;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];arr = [NSMutableArray array];[arr addObject:@"50"];[arr addObject:@"80"];[arr addObject:@"160"];[arr addObject:@"30"];[arr addObject:@"16"];[self initPipeView];
}
- (void)initPipeView {self.pipeView = [[PDChartView alloc] initWithFrame:CGRectMake(50, 100,250, 250)];self.pipeView.dataSource = self;[self.view addSubview:self.pipeView];self.pipeView.contentMode = UIViewContentModeRedraw;
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}#pragma mark datasource
- (NSInteger)numberOfSlicesInPDChartView {return 5;
}- (UIColor *)colorForChartViewSliceAtIndex:(NSUInteger)index {UIColor *color;switch (index) {case 0:color = KChartColorBlue_R66G169B238;break;case 1:color = KChartColorGreen_R78G201B72;break;case 2:color = KChartColorOrange_R250G157B58;break;case 3:color = KChartColorRed_R240G89B89;break;case 4:color = KChartColorGray_R226G226B226;break;}return color;
}- (CGFloat)valueForChartViewSliceAtIndex:(NSUInteger)index {NSString *numStr = arr[index];return [numStr doubleValue];
}- (CGFloat)centerCircleRadius:(PDChartView *)charView width:(CGFloat)innerWidth {return 40;
}@end
这里不细说了,无非就是调用代理方法等等基本操作,到这里我们就完成了饼状图的绘制。。
下载源码
iOS 中饼状图的自定义绘制相关推荐
- android 自定义饼图半径不定,【Android】仿支付宝账单统计饼状图的自定义view
仿支付宝统计饼状图的自定义view,如下图: 项目地址:https://github.com/bigeyechou/CustomViewCollection 目录:customviewcollecti ...
- echart的关系图高亮_echart中饼状图的高亮显示。
1 2 3 4 5 6 7 8 9 10 #main{ 11 width:50vw;height:60vh;margin-left:2vw 12 } 13 14 15 16 17 18 19 20 / ...
- Echarts中饼状图的使用
先上效果图: 1.首先去官网下载echarts.min.js,并引入项目 2.为 ECharts 准备一个具备大小(宽高)的 容器 3.js构造饼图,js代码如下: var myChart = ech ...
- Matplotlib绘制漂亮的饼状图|python绘制漂亮的饼状图
python绘图系列文章目录 往期python绘图合集: python绘制简单的折线图 python读取excel中数据并绘制多子图多组图在一张画布上 python绘制带误差棒的柱状图 python绘 ...
- H5画布 canvas(一)canvas简介、绘制圆形矩形、案例饼状图绘制
目录 1. canvas 简介 2. canvas 标签介绍 3. canvas 上下文 Context 4. 案例:在 canvas 画布中绘制表格 5. canvas 的 beginPath 状态 ...
- H5画布 canvas(一)canvas简介、绘制圆形/矩形、案例饼状图绘制
目录 1. canvas 简介 2. canvas 标签介绍 3. canvas 上下文 Context 4. 案例:在 canvas 画布中绘制表格 5. canvas 的 beginPath 状态 ...
- ppt饼状图如何合计数据
在 PowerPoint 中绘制饼状图时,可以使用 "数据系列" 功能来合计数据.具体步骤如下: 选择需要绘制饼状图的数据,然后点击 "图表" 按钮. 在弹出的 ...
- android自定义View: 饼状图绘制(四)
本系列自定义View全部采用kt 系统mac android studio: 4.1.3 kotlin version1.5.0 gradle: gradle-6.5-bin.zip 本篇效果: 画矩 ...
- vue中使用echart绘制柱状图、折现图、饼状图
echarts有几个主要的字段,title.legend.xAxis. yAxis.tooltip.series. title 图表的标题 legend 用来标志图表数据,data字段的数组需要对应每 ...
最新文章
- Fastp对fastq质控
- 这年头连人用脑机接口信号都能无线传输了,瘫痪者可在家轻松上网看视频
- 如何在私有链部署智能合约
- oracle中check约束性别,关于Oracle Check类型约束的导入与启用
- JAVA synchronized关键字锁机制(中)
- junit单元测试不通过报documentationPluginsBootstrapper相关异常
- 全国OA系统下载 (转)
- dell服务器无线网卡,dell笔记本内置无线网卡找不到怎么处理
- 【积分变换】积分变换常用公式定理与方法
- 电信屏蔽了80端口,利用80端口映射解决web网站应用发布问题
- 【数说人物】170个热搜刷屏网络 起底康巴汉子丁真爆红路径
- 基于Python的指数基金量化投资 - 指数的净资产收益率计算
- Mysq连接l数据库有效文章
- 奇安信前三季营收31.9亿:同比增19% 扣非后净亏13.6亿
- Fedora13装nividia驱动
- 域名指向主机IP地址,通过域名:8080才能访问网站,去掉后面的8080;或者其他的端口号,直接使用域名访问网站
- 洛谷-P1007-魔法少女
- mysql 内连接、左连接会出现笛卡尔积?
- 设置页面默认为繁体字
- python三行情书代码_“三行情书”——给你三行代码的爱恋~