ios - how to create the pie Chart and fill percentage in swift -
i have create uiview
uiviewcontroller
, how create pie chart in uiview
?, have create pieview
class uiview
, class code below,
import uikit import foundation class pieview: uiview { public var datapoints: [datapoint]? { // use whatever type makes sense app, though i'd suggest array (which ordered) rather dictionary (which isn't) didset { setneedsdisplay() } } @ibinspectable internal var linewidth: cgfloat = 2 { didset { setneedsdisplay() } } }
i have confused format used? coreplot or charts frameworks
or other method.
objective-c
custom code need swift
language code, how implement pie chart in uiview
, fill percentage,
thanks in advance.
i think code snippet taken https://stackoverflow.com/a/33947052/1271826 imagined simple pieview
class defined 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 makes sense app, though i'd suggest array (which ordered) rather 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() 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 added uiview
in ib , changed base class pieview
, add @iboutlet
, 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:
note, that's @ibdesignable
(and implements prepareforinterfacebuilder
) when add storyboard, you'll see sample pie chart.
clearly, absurdly simplistic implementation, can expand upon see fit.
Comments
Post a Comment