簡體   English   中英

如何創建餅圖並快速填充百分比

[英]how to create the pie Chart and fill percentage in swift

我已經從UIViewController創建了UIView ,如何在UIView創建餅形圖?我已經為UIView創建了PieView類,下面是該類的代碼,

import UIKit
import Foundation

class pieView: UIView {

    public var dataPoints: [DataPoint]? {          // use whatever type that makes sense for your app, though I'd suggest an array (which is ordered) rather than a dictionary (which isn't)
        didSet { setNeedsDisplay() }
    }

    @IBInspectable internal var lineWidth: CGFloat = 2 {
        didSet { setNeedsDisplay() }
    }

} 

我很困惑要使用什么格式? coreplot或圖表frameworks或其他任何方法。

Objective-C獲得了自定義代碼,但是我需要swift語言代碼,如何在UIView實現餅圖並填充百分比,

提前致謝。

我認為該代碼段來自https://stackoverflow.com/a/33947052/1271826 ,我在其中想象一個簡單的PieView類定義了類似以下內容:

import UIKit

public class DataPoint {
    let text: String
    let value: Float
    let color: UIColor

    public init(text: String, value: Float, color: UIColor) {
        self.text = text
        self.value = value
        self.color = color
    }
}

@IBDesignable public class PieView: UIView {

    public var dataPoints: [DataPoint]? {          // use whatever type that makes sense for your app, though I'd suggest an array (which is ordered) rather than a dictionary (which isn't)
        didSet { setNeedsDisplay() }
    }

    @IBInspectable public var lineWidth: CGFloat = 2 {
        didSet { setNeedsDisplay() }
    }

    override public func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        dataPoints = [
            DataPoint(text: "foo", value: 3, color: UIColor.redColor()),
            DataPoint(text: "bar", value: 4, color: UIColor.yellowColor()),
            DataPoint(text: "baz", value: 5, color: UIColor.blueColor())
        ]
    }

    public override func drawRect(rect: CGRect) {
        guard dataPoints != nil else {
            return
        }

        let center = CGPoint(x: bounds.size.width / 2.0, y: bounds.size.height / 2.0)
        let radius = min(bounds.size.width, bounds.size.height) / 2.0 - lineWidth
        let total = dataPoints?.reduce(Float(0)) { $0 + $1.value }
        var startAngle = CGFloat(-M_PI_2)

        UIColor.blackColor().setStroke()

        for dataPoint in dataPoints! {
            let endAngle = startAngle + CGFloat(2.0 * M_PI) * CGFloat(dataPoint.value / total!)

            let path = UIBezierPath()
            path.moveToPoint(center)
            path.addArcWithCenter(center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
            path.closePath()
            path.lineWidth = lineWidth
            dataPoint.color.setFill()

            path.fill()
            path.stroke()

            startAngle = endAngle
        }
    }

    public override func layoutSubviews() {
        super.layoutSubviews()
        setNeedsDisplay()
    }
}

並且,如果您在IB中添加了UIView並將基類更改為PieView並添加了@IBOutlet ,則可以執行以下操作:

class ViewController: UIViewController {

    @IBOutlet weak var pieView: PieView!

    override func viewDidLoad() {
        super.viewDidLoad()

        pieView.dataPoints = [
            DataPoint(text: "foo", value: 3, color: UIColor.redColor()),
            DataPoint(text: "bar", value: 4, color: UIColor.yellowColor()),
            DataPoint(text: "baz", value: 5, color: UIColor.blueColor())
        ]
    }

}

產生:

在此處輸入圖片說明

請注意,這是@IBDesignable (並實現了prepareForInterfaceBuilder ),因此,將其添加到情節@IBDesignable中時,您將看到一個示例餅圖。

顯然,這是一個非常簡單的實現,但是您可以根據需要擴展此功能。

AFAIK在swift或apple's框架中沒有對圖表的本地支持。 但是GitHub上有很多項目可以使用。 嘗試使用高級搜索,例如圖表語言:swift

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM