Contents

william-weng/wwdonutchartview

- [Simple donut chart.](https://blog.vizdata.tw/2018/02/how-to_26.html)

[Introduction - 簡介](https://swiftpackageindex.com/William-Weng)

[WWDonutChartView]

Installation with Swift Package Manager

dependencies: [
    .package(url: "https://github.com/William-Weng/WWDonutChartView.git", .upToNextMajor(from: "1.2.3"))
]

[Function - 可用函式](https://gitbook.swiftgg.team/swift/swift-jiao-cheng)

|函式|功能| |-|-| |setting(lineWidthType:baseLineColor:touchGap)|設定線寬 / 底線的顏色 / 內縮距離| |drawing(lineCap:animtionType:)|繪製動畫線條 / 動畫類型| |clean()|清除線段|

WWDonutChartViewDelegate

|函式|功能| |-|-| |duration(in:)|動畫的總時間 (360°)| |informations(in:)|取得資料相關資訊| |donutChartView(:didSelectedIndex:)|點到哪一個圓環的Index| |donutChartView(:animation:isStop:isFinished:)|動畫開始 / 停止 / 結束|

Example

import UIKit
import WWPrint
import WWDonutChartView

final class MyDonutChartView: WWDonutChartView {}

final class ViewController: UIViewController {
    
    @IBOutlet weak var touchedIndexLabel: UILabel!
    @IBOutlet weak var donutChartView: MyDonutChartView!
    
    private let infos: [WWDonutChartView.LineInformation] = [
        (title: "Red", strokeColor: .red, percent: 0.1),
        (title: "Green", strokeColor: .green, percent: 0.3),
        (title: "Yellow", strokeColor: .yellow, percent: 0.6),
    ]
    
    private var lineWidthType: WWDonutChartView.LineWidthType = .custom(56)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        donutChartView.delegate = self
    }
    
    @IBAction func toggleLineWidthType(_ sender: UIBarButtonItem) {
        
        lineWidthType = (sender.tag == 101) ? .custom(56) : .radius
        
        donutChartView.setting(lineWidthType: lineWidthType, baseLineColor: .lightGray, touchGap: 0)
        donutChartView.clean()
    }
    
    @IBAction func drawAction(_ sender: UIButton) {
        
        let animtionType: WWDonutChartView.AnimtionType
        
        switch lineWidthType {
        case .radius: animtionType = .queue
        case .custom(_): animtionType = .same
        }
        
        donutChartView.drawing(lineCap: .butt, animtionType: animtionType)
    }
}

extension ViewController: WWDonutChartViewDelegate {
    
    func duration(in donutChartView: WWDonutChartView) -> Double {
        return 1.0
    }
    
    func informations(in donutChartView: WWDonutChartView) -> [WWDonutChartView.LineInformation] {
        return infos
    }
    
    func donutChartView(_ donutChartView: WWDonutChartView, didSelectedIndex index: Int?) {
        
        guard let index = index else { touchedIndexLabel.text = "<null>"; return }
        
        let info = infos[index]
        touchedIndexLabel.text = info.title
    }
    
    func donutChartView(_ donutChartView: WWDonutChartView, animation: CAAnimation, didStop isStop: Bool, isFinished: Bool) {
        wwPrint("\(animation.duration) isStop => \(isStop) => isFinished => \(isFinished)")
    }
}

Package Metadata

Repository: william-weng/wwdonutchartview

Default branch: main

README: README.md