Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

how to create the pie Chart and fill percentage in swift

I have create UIView from UIViewController, how to create pie chart in UIView?, I have create the PieView class for UIView and the class code is below,

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

} 

I have confused what is format to be used? coreplot or charts frameworks or other any method.

Objective-C is get the custom code but i need swift language code, how to implement pie chart in UIView and fill percentage,

Thanks in advance.

Upvotes: 0

Views: 1736

Answers (2)

Rob
Rob

Reputation: 437632

I think that code snippet was taken from https://stackoverflow.com/a/33947052/1271826 where I imagined a simple PieView class defined something like:

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

And, if you added a UIView in IB and changed the base class to PieView and add an @IBOutlet, you could then do something like:

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

}

That yields:

enter image description here

Note, that's @IBDesignable (and implements prepareForInterfaceBuilder) so that when you add it to a storyboard, you'll see a sample pie chart.

Clearly, this is an absurdly simplistic implementation, but you can expand upon this as you see fit.

Upvotes: 1

Martin Makarsky
Martin Makarsky

Reputation: 2650

AFAIK there is no native support for charts in swift or in apple`s framework. But there are plenty of projects on GitHub you can use. Try to use advanced search something like chart language:swift

Upvotes: 0

Related Questions