2016-03-02 90 views
13

到目前为止,我有一个填充圆圈,就是这样。我试图制作一张饼图,表示满意和不满意客户的数量并呈现。我对CG非常陌生,想知道有人可以拿出足够的代码给我一个想法或引导我。使用Core Graphics制作饼图

我是否应该让底部圆圈表示满意的客户数量,然后在其上添加另一个圆圈以显示不满意的客户?我以正确的方式接近它吗?

这是我的代码到目前为止。

override func drawRect(rect: CGRect) { 

    // Get current context 
    let context = UIGraphicsGetCurrentContext() 

    // Set color 
    CGContextSetStrokeColorWithColor(context,UIColor(red: 0.2, green: 0.4, blue: 1, alpha: 1.0).CGColor) 

    let rectangle = CGRectMake((frame.size.width/3) - 50, frame.size.height/2 + 40,220,220) 
    CGContextAddEllipseInRect(context,rectangle) 

    CGContextSetFillColorWithColor(context, UIColor(red: 0.2, green: 0.4, blue: 1, alpha: 1.0).CGColor) 
    CGContextFillPath(context) 
    CGContextStrokePath(context) 

} 

编辑

而且,现在我开始认识到,我可能需要支付我的圈子与基于关闭总不满意的顾客的弧。如何根据人数增加或减少覆盖弧的大小?

任何帮助将非常感激!

+0

可能有以下线程可以帮助你:饼图,曲线图,在-SWIFT(http://stackoverflow.com/questions/28768550/pie -chart积合SWIFT)。 – dfri

+0

我仔细研究了这个答案,他的代码不会产生比空圈更多的东西。但是,谢谢。 – Mihado

+1

@Ah我没有查看具体细节,因此“可能”:)除非你真的想自己实现它,否则你可以看看(或者受到启发)[iOS的PieChart(...)图表](https://github.com/danielgindi/ios-charts)(参见[本教程](http://www.appcoda.com/ios-charts-api-tutorial/))或例如[夫特-饼图(https://github.com/zemirco/swift-piechart)。 – dfri

回答

36

您需要使用CGContextAddArc()函数(Swift 3中的CGContext.addArc())。这将使您可以通过为饼图的每个分段绘制一条弧来为饼图创建多个分段。

像这样的东西应该做的伎俩:

import UIKit 

struct Segment { 

    // the color of a given segment 
    var color: UIColor 

    // the value of a given segment – will be used to automatically calculate a ratio 
    var value: CGFloat 
} 

class PieChartView: UIView { 

    /// An array of structs representing the segments of the pie chart 
    var segments = [Segment]() { 
     didSet { 
      setNeedsDisplay() // re-draw view when the values get set 
     } 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     isOpaque = false // when overriding drawRect, you must specify this to maintain transparency. 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func draw(_ rect: CGRect) { 

     // get current context 
     let ctx = UIGraphicsGetCurrentContext() 

     // radius is the half the frame's width or height (whichever is smallest) 
     let radius = min(frame.size.width, frame.size.height) * 0.5 

     // center of the view 
     let viewCenter = CGPoint(x: bounds.size.width * 0.5, y: bounds.size.height * 0.5) 

     // enumerate the total value of the segments by using reduce to sum them 
     let valueCount = segments.reduce(0, {$0 + $1.value}) 

     // the starting angle is -90 degrees (top of the circle, as the context is flipped). By default, 0 is the right hand side of the circle, with the positive angle being in an anti-clockwise direction (same as a unit circle in maths). 
     var startAngle = -CGFloat.pi * 0.5 

     for segment in segments { // loop through the values array 

      // set fill color to the segment color 
      ctx?.setFillColor(segment.color.cgColor) 

      // update the end angle of the segment 
      let endAngle = startAngle + 2 * .pi * (segment.value/valueCount) 

      // move to the center of the pie chart 
      ctx?.move(to: viewCenter) 

      // add arc from the center for each segment (anticlockwise is specified for the arc, but as the view flips the context, it will produce a clockwise arc) 
      ctx?.addArc(center: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false) 

      // fill segment 
      ctx?.fillPath() 

      // update starting angle of the next segment to the ending angle of this segment 
      startAngle = endAngle 
     } 
    } 
} 

您可以输入您的饼图数据的Segment结构,其中每个Segment表示段的颜色和值的数组。

该值可以是任何浮点数,并会自动降低到饼图中要使用的比率。因此,举例来说,如果你希望你的饼图来表示不满意对客户满意度的数数,你可以直接在值传递使用的

例:

let pieChartView = PieChartView() 
pieChartView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 400) 
pieChartView.segments = [ 
    Segment(color: .red, value: 57), 
    Segment(color: .blue, value: 30), 
    Segment(color: .green, value: 25), 
    Segment(color: .yellow, value: 40) 
] 
view.addSubview(pieChartView) 

输出:

enter image description here


全部项目(含部分额外的功能):https://github.com/originaluser2/Pie-Chart-View

+0

太棒了,我会在下班回家后检查一下,看看我是否还有其他问题。非常感谢! – Mihado

+0

@Mihado高兴地帮助:)在使用UIViews时,在Core Graphics中弧线有时会令人困惑,因为y轴被翻转,因此指定顺时针弧会产生逆时针弧,并且角度被反转等。回答您可能遇到的任何问题。 – Hamish

+0

这是完美的。我不知道该如何谢谢你。我会尽快给你赏金。如果您有兴趣,我很乐意向您展示我正在建设的东西@ originaluser2 – Mihado

相关问题