如何在swift中创建饼图和填充百分比

时间:2016-04-19 04:43:53

标签: ios swift2

我已从UIView创建UIViewController,如何在UIView中创建饼图?,我为PieView创建了UIView类代码如下,

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() }
    }

} 

我混淆了什么是要使用的格式?核心图或图表frameworks或其他任何方法。

Objective-C获取自定义代码,但我需要swift语言代码,如何在UIView中实现饼图并填充百分比,

提前致谢。

2 个答案:

答案 0 :(得分:1)

我认为该代码段取自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())
        ]
    }

}

产量:

enter image description here

请注意,@IBDesignable(以及实施prepareForInterfaceBuilder)这样,当您将其添加到故事板时,您将看到示例饼图。

显然,这是一个荒谬的简单实现,但您可以根据需要对此进行扩展。

答案 1 :(得分:0)

AFAIK在swift或apple的框架中没有对图表的原生支持。但是你可以在GitHub上使用很多项目。尝试使用类似chart language:swift

的高级搜索