저는 현재 프로젝트에서 파이 차트를 그릴 필요가 있습니다. 타사 라이브러리를 사용하는 대신 코어 그래픽을 사용하여 그려 봅니다. 다음은 파이 차트를 그리는 코드입니다.어떻게 신속하게 원형 차트보기를 그립니까?
let circlePath = UIBezierPath(arcCenter: CGPoint(x: self.frame.width/2 + r/8, y: self.frame.height/2 + r/8), radius: r, startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2 * Double(percent1/100)), clockwise: true)
let circlePath2 = UIBezierPath(arcCenter: CGPoint(x: self.frame.width/2 + r/8, y: self.frame.height/2 + r/8), radius: r, startAngle: CGFloat(M_PI * 2 * Double(percent1/100)), endAngle: CGFloat(0), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
let shapeLayer2 = CAShapeLayer()
shapeLayer2.path = circlePath2.cgPath
//change the fill color
shapeLayer.fillColor = UIColor.red.cgColor
shapeLayer2.fillColor = UIColor.blue.cgColor
//you can change the stroke color
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer2.strokeColor = UIColor.blue.cgColor
//you can change the line width
shapeLayer.lineWidth = 3.0
self.layer.addSublayer(shapeLayer)
self.layer.addSublayer(shapeLayer2)
그러나 중심을 기준으로하지 않고 원을 선형으로 그려야하므로 원하는 효과를 내지 못합니다.
[awesome-ios-chart] (https://github.com/ameizi/awesome-ios-chart) 중 하나를 사용하여 – zombie
양쪽 중심 좌표에서'+ r/8'을 삭제하십시오. – rmaddy