美文网首页
IOS开发之——绘制饼状图

IOS开发之——绘制饼状图

作者: iOS发呆君 | 来源:发表于2021-02-20 14:23 被阅读0次

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

对iOS开发感兴趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎大家加群,大家一起开车

一 概述

  • 按照1:1:2的份额划分圆形
  • 开始角度为0度,旋转角度为份额2π(360度),结束角度为开始角度+旋转角度
  • 给每分饼状图设置不同的颜色
  • 点击图形,饼状图切换颜色

二 饼状图划分

startA=0;
angle=25/100.0*M_PI*2;
endA=startA=angle;

三 功能开发

3.1 代码

pieView

- (void)drawRect:(CGRect)rect {
    // Drawing code
    NSArray *data=@[@25,@25,@50];
    //1.获取上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGPoint center=CGPointMake(125, 125);
    CGFloat radius=120;
    CGFloat startA=0;
    CGFloat angle=0;
    CGFloat endA=0;

    for (NSNumber *number in data) {
        //2.拼接路径
        startA=endA;
        angle=number.intValue/100.0*M_PI*2;
        endA=startA+angle;

        UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path addLineToPoint:center];
        [[UIColor randomColor]set];
        //3.把路径添加到上下文
        CGContextAddPath(ctx, path.CGPath);
        //4.渲染
        CGContextFillPath(ctx);
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self setNeedsDisplay];
}

UIColor+RandomColor(随机颜色工具)

#import "UIColor+RandomColor.h"

@implementation UIColor (RandomColor)
+(UIColor *)randomColor
{
    CGFloat r=arc4random_uniform(256)/255.0;
    CGFloat g=arc4random_uniform(256)/255.0;
    CGFloat b=arc4random_uniform(256)/255.0;
    return  [UIColor colorWithRed:r green:g blue:b alpha:1];

}
@end

3.2 效果图

相关文章

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

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

  • 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饼状图的小测,如下,结果为正常饼状图 绘制前面数据库中的数据来找某一天交易的各类目物品的饼状图 ...

  • matplotlib绘制图表

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

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

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

  • Android 绘制饼状图

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

网友评论

      本文标题:IOS开发之——绘制饼状图

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