Code Monkey home page Code Monkey logo

piecharts's Introduction

PieCharts

Version Carthage compatible License

Easy to use and highly customizable pie charts library for iOS

Swift 4.2, iOS 8+

Video

ScreenShotScreenShot

Features:

  • Customizable slices
  • Add overlays using simple UIViews
  • Interactive
  • Animated
  • Dynamic slice insertion
  • Reusable components via extensible layer system
  • Configurable in interface builder
  • Legends. This is in a separate project to keep things focused and reusable.

Installation

CocoaPods

Add to your Podfile:

use_frameworks!
pod 'PieCharts'

Carthage

Add to your Cartfile:

github "i-schuetz/PieCharts"

Usage

Basic chart:

@IBOutlet weak var chartView: PieChart!

chartView.models = [
    PieSliceModel(value: 2.1, color: UIColor.yellow),
    PieSliceModel(value: 3, color: UIColor.blue),
    PieSliceModel(value: 1, color: UIColor.green)
]

Configurable in interface builder, with live update of the view:

ScreenShot

Overlays:

Overlays are implemented using layers. There are several built in layers and you also can implement your own ones.

To add text e.g. text labels inside the slices + text with lines outside, simply:

chartView.layers = [PiePlainTextLayer(), PieLineTextLayer()]

Each layer has its own customization options. For example, here we customize the plain labels layer:

let textLayerSettings = PiePlainTextLayerSettings()
textLayerSettings.viewRadius = 55
textLayerSettings.hideOnOverflow = true
textLayerSettings.label.font = UIFont.systemFont(ofSize: 8)

let formatter = NumberFormatter()
formatter.maximumFractionDigits = 1
textLayerSettings.label.textGenerator = {slice in
    return formatter.string(from: slice.data.percentage * 100 as NSNumber).map{"\($0)%"} ?? ""
}

let textLayer = PiePlainTextLayer()
textLayer.animator = AlphaPieViewLayerAnimator()
textLayer.settings = textLayerSettings

This is the custom views layer, which makes possible to create custom views:

let viewLayer = PieCustomViewsLayer()

let settings = PieCustomViewsLayerSettings()
settings.viewRadius = 135
settings.hideOnOverflow = false
viewLayer.settings = settings

viewLayer.viewGenerator = {slice, center in
    let myView = UIView()
    // add images, animations, etc.
    return myView
}

Interactivity, events:

Conform to PieChartDelegate to react to interaction and other events:

func onGenerateSlice(slice: PieSlice)
func onStartAnimation(slice: PieSlice)
func onEndAnimation(slice: PieSlice)
func onSelected(slice: PieSlice, selected: Bool)

Dynamic slice insertion:

chartView.insertSlice(index: 1, model: PieSliceModel(value: 5, color: UIColor.blue))

Contributing

  1. Fork
  2. Commit changes to a branch in your fork
  3. Push your code and make a pull request

Outlook

The layer system can be abstracted a step further in order to make the slices themselves be in a layer. This way we can combine multiple slice-layers to create more complex types of pie charts.

Credits

The food images used in the demo are from freepik.com, and flaticon.com/authors/madebyoliver

Created By:

Ivan Schütz

License

SwiftCharts is Copyright (c) 2017 Ivan Schütz and released as open source under the attached Apache 2.0 license.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

piecharts's People

Contributors

ivnsch avatar guerrix avatar markus-mohemian avatar jrafaelm avatar nitrodamsel avatar hyunminch avatar arbitur avatar simonwaldherr avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.