CocoaPods trunk is moving to be read-only. Read more on the blog, there are 2 months to go.

MRLCircleChart 0.5.2

MRLCircleChart 0.5.2

TestsTested ✗
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2017
SwiftSwift Version 3.0
SPMSupports SPM ✓

Maintained by Marek Lisik.



  • By
  • mlisik

MRLCircleChart

Animated .gif of chart reloading it's data

MRLCircleChart is a small pie/circle chart UI component written in Swift. Aims to take care of most of the work for you (just pass in a data source and configure the view) at the expense of customizability.

Main Features

  • [x] configuration through a dataSource
  • [x] customization through IB
  • [x] color, linewidth, begin/end angle animations
  • [x] callbacks on selection / deselection

Usage

Data setup

Chart‘s DataSource is comprised of an array of Segments and a maxValue property that functions as a reference for Segments angle values. You can easily map your source values to [Segment], then initialize a data source conforming to the MRLCircleChart.DataSource protocol, and pass this to the Chart:

struct DataSource: MRLCircleChart.DataSource {
  // protocol conformance
}

let segments = [90, 80, 60].map {
  Segment(value: $0, description: "\($0)")
}

chart.dataSource = DataSource(items: segments, maxValue: 250)

Customization

All customization you need can be done via InterfaceBuilder, through the following IBDesignable properties:

InterfaceBuilder's AttributesInspector pane contains a Chart section where primary properties are easily accessible

If you are laying out your Chart without IB, these are also readily available.

@IBInspectable public var lineWidth: CGFloat = 25
@IBInspectable public var padding: CGFloat = 0
@IBInspectable public var chartBackgroundColor: UIColor = UIColor(white: 0.7, alpha: 0.26)
@IBInspectable public var beginColor: UIColor? = UIColor.beginColor()
@IBInspectable public var endColor: UIColor? = UIColor.endColor()

Main public APIs

See the entire documentation on Cocoadocs.

Beyond customization, the main public APIs revolve around modifying and updating the available data.

DataSource

public mutating func remove(index: Int) -> MRLCircleChart.Segment?
public mutating func insert(item: MRLCircleChart.Segment, index: Int)
public mutating func append(item: MRLCircleChart.Segment)
public mutating func empty()

Chart

final public func reloadData(animated animated: Bool = true, completion: () -> () = {})
public func empty(animated animated: Bool = true, color: UIColor? = nil)
public func select(index selectIndex: Int)
public func deselect(index index: Int)

Callbacks

Two callbacks are available for user interaction events: when a segment is selected and when it’s deselected.

chart.selectHandler = {
  index in print("selected \(index)")
}

chart.deselectHandler = {
  index in print("deselected \(index)")
}

Demo

To run the example project, clone the repo, and run pod install from the Example directory first, or run pod try MRLCircleChart to check it with a temporary clone.

Installation

MRLCircleChart is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "MRLCircleChart"

Collaboration

If you’d like to help out, check open tickets on waffle.io. There’s no roadmap for the project yet, but pull requests are welcome (whether you want to clean up, fix or add something), and so are feature requests.

Author

mlisik, [email protected]

License

MRLCircleChart is available under the MIT license. See the LICENSE file for more info.