美文网首页
iOS绘制饼状图

iOS绘制饼状图

作者: 宋魁鹏 | 来源:发表于2019-07-10 15:18 被阅读0次

效果图

效果图

1 创建SKPPieChartView继承于UIView

2 SKPPieChartView.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN


@interface ChartModel : NSObject

@property(nonatomic,assign)float bili;//总值为1
@property(nonatomic,strong)UIColor *color;

@end


@interface SKPPieChartView : UIView

@property(nonatomic,assign)CGFloat startAngle;//开始弧度默认为0度

-(void)drawChartWithChartModelArray:(NSArray<ChartModel*>*)chartModelArray;


@end

NS_ASSUME_NONNULL_END

3 SKPPieChartView.m

#import "SKPPieChartView.h"

@implementation ChartModel

@end


@interface SKPPieChartView ()

@property(nonatomic,strong)NSArray<ChartModel*> *chartModelArray;

@end

@implementation SKPPieChartView


-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius=frame.size.width/2;
        self.layer.masksToBounds=YES;
    }
    return self;
}

-(void)drawChartWithChartModelArray:(NSArray<ChartModel*>*)chartModelArray{
    self.chartModelArray=chartModelArray;
}

- (void)drawRect:(CGRect)rect {
    
    CGFloat radius = rect.size.width/2;//半径
    CGPoint center = CGPointMake(rect.size.width/2, rect.size.width/2);//圆心
    CGFloat startAngle = self.startAngle;//开始弧度
    CGFloat angle = 0;//所占弧度
    CGFloat endAngle = 0;//结束弧度
    for (int i = 0; i<self.chartModelArray.count; i++) {
        ChartModel *model=self.chartModelArray[i];
        angle=model.bili*M_PI*2;
        endAngle=startAngle+angle;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
        [path addLineToPoint:center];
        [model.color set];
        [path fill];
        startAngle=endAngle;
    }
}

@end

4 调用

#import "ViewController.h"

#import "SKPPieChartView.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    ChartModel *model1=[[ChartModel alloc]init];
    model1.color=[UIColor redColor];
    model1.bili=0.4;
    
    ChartModel *model2=[[ChartModel alloc]init];
    model2.color=[UIColor yellowColor];
    model2.bili=0.4;
    
    ChartModel *model3=[[ChartModel alloc]init];
    model3.color=[UIColor blueColor];
    model3.bili=0.1;
    
    SKPPieChartView *pieChartView=[[SKPPieChartView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    pieChartView.backgroundColor=[UIColor lightGrayColor];
    pieChartView.startAngle=M_PI+M_PI_2;
    [self.view addSubview:pieChartView];
    [pieChartView drawChartWithChartModelArray:@[model1,model2,model3]];
    
    
}

@end

相关文章

  • iOS CGContextRef

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

  • iOS绘制饼状图

    效果图 1 创建SKPPieChartView继承于UIView 2 SKPPieChartView.h 3 SK...

  • iOS使用Charts框架绘制—饼状图

    iOS使用Charts框架绘制—饼状图[https://www.jianshu.com/p/45194d861b21]

  • iOS 如何绘制饼状图

    先来见识一下史上最无力的背打 前言 对于图形的绘制,我们可以用CoreGraphics中CGContext或者UI...

  • Swift第三方Charts的简单使用

    Charts是一个强大的图表框架,MPAndroidChart 在 iOS 上的移植。可以绘制线形图,直方图,饼状...

  • charts3

    先进行charts饼状图的小测,如下,结果为正常饼状图 绘制前面数据库中的数据来找某一天交易的各类目物品的饼状图 ...

  • IOS开发之——绘制饼状图

    文章搬运来源:https://blog.csdn.net/Calvin_zhou/article/details/...

  • matplotlib绘制图表

    python中使用matplotlib库可以快速画简单的图表下面介绍下柱状图和饼图绘制1 柱状图绘制 2 饼状图绘...

  • 学习笔记----python绘图pie

    # python 绘制饼状图 #-*- coding:utf-8 –*- import matplotlib.py...

  • Android 绘制饼状图

    工作中有绘制饼状图功能,自己稍微修改了一下前边人的写作方法。 1.具体是一个空心的圆环外层是两种颜色的扇形图的一部...

网友评论

      本文标题:iOS绘制饼状图

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