iOS 绘制饼图代码

前端之家收集整理的这篇文章主要介绍了iOS 绘制饼图代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

CGContextAddArc(CGContextRef __nullable c,CGFloat x,CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle,int clockwise)
CGFloat degree=(count/allCount)*(360.f-2*self.dataSource.count);
staticinlinefloat radians(double degrees) { return degrees *  M_PI/ 180.f;
}
- (void)drawRect:(CGRect)rect {
  CGContextRef context=UIGraphicsGetCurrentContext();
  //设置半径
  CGFloat radius=130.f;
  if (self.circularRingRadius==0) {
    self.circularRingRadius=54.f;
  }
  CGFloat intRadius=radius-self.circularRingRadius;
  //设置圆心的坐标
  CGFloat centerX=self.bounds.size.width/2.f;
  CGFloat centerY=self.bounds.size.height/2.f;
  //设置起始角度
  CGFloat pieStart=90.f;
  //设置旋转方向
  int clockwise=0; //1: 顺时针 ; 0:逆时针
  //画扇形
  if(allCount == 0){  //无资产
    CGContextSetFillColorWithColor(context,[HEXCOLOR(0xefeff4) CGColor]);
    CGContextMoveToPoint(context,centerX,centerY);
    CGContextAddArc(context,centerY,radius,radians(0),radians(360),clockwise);
    CGContextClosePath(context);
    CGContextFillPath(context);
  }else{
    for (int i=0; i<self.degreeArray.count; i++) {
      CGFloat end=pieStart+[self.degreeArray[i] doubleValue];
      if (self.isShowSeperate) {
        if(i%2==0){  //分割线
          UIColor *fillColor=self.colorArray[(int)(i/2)];
          CGContextSetFillColorWithColor(context,[fillColor CGColor]);
        }else{
          CGContextSetFillColorWithColor(context,[HEXCOLOR(0xefeff4) CGColor]);
        }
      }else{
        CGContextSetFillColorWithColor(context,[self.colorArray[i] CGColor]);
      }
      NSLog(@"%f",radians(end));
      CGContextMoveToPoint(context,centerY);
      CGContextAddArc(context,radians(pieStart),radians(end),clockwise);
      CGContextClosePath(context);
      CGContextFillPath(context);
      pieStart+=[self.degreeArray[i] doubleValue];
    }
  }
  //画内圆
  CGContextSetFillColorWithColor(context,[[UIColor whiteColor] CGColor]);
  CGContextMoveToPoint(context,centerY);
  CGContextAddArc(context,intRadius,radians(360.f),0);
  CGContextClosePath(context);
  CGContextFillPath(context);
}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的C&C++相关文章