2016-03-02 88 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

相關問題