Line Chart library for iOS written in Swift

Overview

Swift LineChart

line chart demo

Usage

var lineChart = LineChart()
lineChart.addLine([3, 4, 9, 11, 13, 15])

Features

  • Super simple
  • Highly customizable
  • Auto scaling
  • Touch enabled
  • Area below lines

Properties

Both x and y properties are of type Coordinate. Each can be customized separately and has its own settings for labels, gridlines and axis.

  • labels: Labels
  • grid: Grid
  • axis: Axis

Labels can be switched on and off and they can have custom values.

  • visible: Bool = true
  • values: [String] = []

Grid can also be switched on/off, has a custom color and you can specify how many gridlines you'd like to show.

  • visible: Bool = true
  • count: CGFloat = 10
  • color: UIColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1) // #eeeeee

Axis can be switched on/off, has a property to its color and you can specify how much the axis is inset from the border of your UIView.

  • visible: Bool = true
  • color: UIColor = UIColor(red: 96/255.0, green: 125/255.0, blue: 139/255.0, alpha: 1) // 607d8b
  • inset: CGFloat = 15

Animations can be customized through the Animation settings.

  • enabled: Bool = true
  • duration: CFTimeInterval = 1

If you'd like to show extra dots at your data points use the Dots features.

  • visible: Bool = true
  • color: UIColor = UIColor.whiteColor()
  • innerRadius: CGFloat = 8
  • outerRadius: CGFloat = 12
  • innerRadiusHighlighted: CGFloat = 8
  • outerRadiusHighlighted: CGFloat = 12

In addition to the above mentioned features you can further customize your chart.

  • area: Bool = true - Fill the area between line and x axis
  • lineWidth: CGFloat = 2 - Set the line width
  • colors: [UIColor] = [...] - Colors for your line charts

Methods

Add line to chart.

lineChart.addLine(data: [CGFloat])

Remove charts, areas and labels but keep axis and grid.

lineChart.clear()

Make whole UIView white again

lineChart.clearAll()

Delegates

didSelectDataPoint()

Touch event happened at or close to data point.

func didSelectDataPoint(x: CGFloat, yValues: [CGFloat]) {
  println("\(x) and \(yValues)")
}

Examples

Single line with default settings.

line chart demo

var lineChart = LineChart()
lineChart.addLine([3, 4, 9, 11, 13, 15])

Two lines without grid and dots.

two lines without grid and dots

var lineChart = LineChart()
lineChart.area = false
lineChart.x.grid.visible = false
lineChart.x.labels.visible = false
lineChart.y.grid.visible = false
lineChart.y.labels.visible = false
lineChart.dots.visible = false
lineChart.addLine([3, 4, 9, 11, 13, 15])
lineChart.addLine([5, 4, 3, 6, 6, 7])

Show x and y axis

chart with x and y axis

var lineChart = LineChart()
lineChart.area = false
lineChart.x.grid.count = 5
lineChart.y.grid.count = 5
lineChart.addLine([3, 4, 9, 11, 13, 15])
lineChart.addLine([5, 4, 3, 6, 6, 7])

License

MIT

Comments
  • small changes to get it build

    small changes to get it build

    Set was undefined, CGFloat Array wrong type

    Had some initial build problems. I’m new to iOS and xcode, so maybe its my fault, but I think the project should build out of the box. That’s what my changes should provide.

    opened by transistorgit 4
  • Y Values over 99 show ellipses

    Y Values over 99 show ellipses

    Not sure if this is a problem, or if I missed something. But if I try to display data that is greater than 99 then there does not seem to be enough room on the axis to show it. I will have Y values that will be over 10,000. Any suggestions?

    opened by zoomies 3
  • Still have 3 errors in example

    Still have 3 errors in example

    LineChart.swift:

    1. In: // LineChart class class LineChart: UIControl {

    Error: LineChart.swift:29:7: Class 'LineChart' does not implement its superclass's required members

    1. In: /**
    2. Fill area between charts. */ func drawAreaBetweenLineCharts() { . . . . . . CGContextSetFillColorWithColor(context, positiveAreaColor.CGColor)

    Error: LineChart.swift:459:23: 'ClosedInterval' does not have a member named 'Generator'

    DotCALayer.swift: 3. In: class DotCALayer: CALayer {

    Error: DotCALayer.swift:5:7: Class 'DotCALayer' does not implement its superclass's required members

    screenshot at 26 14-01-21

    opened by torquemada163 3
  • Y-Axis label for range > 100 becomes

    Y-Axis label for range > 100 becomes "..." & Missing Argument for parameter 'labels'

    Hi,

    Issue : 1 I'm trying to integrate swift-linechart with one of my projects (in Xcode 6.2). It's working fine with Xcode 6.3 beta. But when I'm trying with Xcode 6.2 there are some compatibility issues. I resolved most of them but this particular one I'm not able to. When I recreate the same scenario in Playground it works fine. I did checkout your old commits but couldn't resolve the issue

    Can you help me with this ?

    screen shot 2015-03-20 at 4 04 32 pm

    Issue : 2 By default Y- axis label for range after 100 becomes becomes "..."

    opened by ghost 2
  • Can't add graphs from storyboard

    Can't add graphs from storyboard

    Hello, I've been experimenting with your Swift files to create a graph on an existing UIView layer, but can't seem to get it to work.

    I tried to adding the lines of the graph during viewWillLayoutSubviews, but I keep getting error on:

    layer.strokeColor = colors[lineIndex].CGColor

    in drawLine() in LineChart.swift file.

    "fatal error: Cannot index empty buffer"

    Is there any other way that I can plot graphs without having to initialise a LineChart instance manually?

    opened by mschinis 2
  • Chart doesn't use entire UIView area

    Chart doesn't use entire UIView area

    I'm trying to create a line graph similar to the one Robinhood uses:

    But when I remove the axises and set their insets to 0 it still seems to have padding. Any idea what is going on?

    img_0146

    opened by lightclient 1
  • colors array not initialized when using nib/storyboard

    colors array not initialized when using nib/storyboard

    When using nib or storyboard to create custom view with class 'LineChart', then only "init(coder aDecoder: NSCoder)" is called, not "init(frame: CGRect)". This means the initialization code for "self.colors" is never called and app will crash during "drawLine" at "layer.strokeColor = colors[lineIndex].CGColor"

    opened by swift-js 1
  • 1 data point

    1 data point

    I am getting

    Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 30]'

    when i try to draw a chart with only 1 point on it.

     func drawDataDots(xAxis: Array<CGFloat>, yAxis: Array<CGFloat>, lineIndex: Int) {
        var dots: Array<DotCALayer> = []
        for index in 0..<xAxis.count {
            var xValue = xAxis[index] + axisInset - outerRadius/2
            var yValue = self.bounds.height - yAxis[index] - axisInset - outerRadius/2
    
            // draw custom layer with another layer in the center
            var dotLayer = DotCALayer()
            dotLayer.dotInnerColor = colors[lineIndex]
            dotLayer.innerRadius = innerRadius
            dotLayer.backgroundColor = dotsBackgroundColor.CGColor
            dotLayer.cornerRadius = outerRadius / 2
            dotLayer.frame = CGRect(x: xValue, y: yValue, width: outerRadius, height: outerRadius) 
            self.layer.addSublayer(dotLayer)
            dots.append(dotLayer)
    
            // animate opacity
            if animationEnabled {
                var animation = CABasicAnimation(keyPath: "opacity")
                animation.duration = animationDuration
                animation.fromValue = 0
                animation.toValue = 1
                dotLayer.addAnimation(animation, forKey: "opacity")
            }
    
        }
        dotsDataStore.append(dots)
    }
    

    dotLayer.frame = CGRect(x: xValue, y: yValue, width: outerRadius, height: outerRadius) is the line throwing the error

    opened by werne2j 1
  • Animates only first line after .addLine() invoked

    Animates only first line after .addLine() invoked

    Hello! When I first time invoke addLine function it works great (line appears with animation), but when I try add second line by invoking the same addLine function, first line appears with animation but second line and other appear without animation. `@IBAction func addLine(_ sender: UIButton) {

        let data2: [CGFloat] = [1, 3, 5, 13, 17, 20]
        lineChart.colors.append(UIColor.cyan)
        lineChart.addLine(data2) // add second line
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        label.text = "..."
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
        
        // simple arrays
        let data: [CGFloat] = [3, 4, 2, 11, 13, 15]
        
        
        // simple line with custom x axis labels
        let xLabels: [String] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        
        //lineChart = LineChart()
        lineChart.animation.enabled = true
        lineChart.area = true
        lineChart.x.labels.visible = true
        lineChart.x.grid.count = 10
        lineChart.y.grid.count = 10
        lineChart.x.labels.values = xLabels
        lineChart.y.labels.visible = true
        lineChart.addLine(data) //add first line
        
        lineChart.dots.outerRadius = 6
        lineChart.dots.innerRadius = 4
        
        lineChart.colors = [UIColor.black]
        
        lineChart.translatesAutoresizingMaskIntoConstraints = false
        lineChart.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }`
    
    opened by yablodev 0
  • How to clear lines and dots

    How to clear lines and dots

    When I use open func clear(), I get an index out of bound error on the first line of fileprivate var drawingWidth. What is the best way to clear just the lines and the dots, but keep the axis and labels?

    opened by wjd157 0
  • APIs deprecated as of iOS 7 and earlier are unavailable in Swift

    APIs deprecated as of iOS 7 and earlier are unavailable in Swift

    Hi Zemirco, i've follow your instructions for install LineCharts into my project. But when i put LineChart.swift into my project at line path = layer.path i've this error:

    LineChart.swift:799:26: 'path' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift

    Can i solve it?

    Thanks and Regards

    opened by mitsus 0
Owner
Mirco Zeiss
IT Architect
Mirco Zeiss
Line Chart library for iOS written in Swift

Swift LineChart Usage var lineChart = LineChart() lineChart.addLine([3, 4, 9, 11, 13, 15]) Features Super simple Highly customizable Auto scaling Touc

Mirco Zeiss 601 Nov 15, 2022
An interactive line chart written in SwiftUI with many customizations.

LineChartView LineChartView is a Swift Package written in SwiftUI to add a line chart to your app. It has many available customizations and is interac

Jonathan Gander 59 Dec 10, 2022
FSLineChart A line chart library for iOS.

FSLineChart A line chart library for iOS. Screenshots Installing FSLineChart Add the contents of the FSLineChart project to your directory or simply a

Arthur 856 Nov 24, 2022
A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.

⚡ A powerful & easy to use chart library for Android ⚡ Charts is the iOS version of this library Table of Contents Quick Start Gradle Maven Documentat

Philipp Jahoda 36k Jan 5, 2023
Demonstrate a way to build your own line chart without using any third-party library

LineChart This code demonstrate a way to build your own line chart without using any third-party library. It contains a simple yet effective algorithm

Van Hung Nguyen 0 Oct 17, 2021
ANDLineChartView is easy to use view-based class for displaying animated line chart.

ANDLineChartView for iOS ANDLineChartView is easy to use view-based class for displaying animated line chart. Usage API is simple. Just implement foll

Andrzej Naglik 421 Dec 11, 2022
Fully customizable line chart for SwiftUI 🤩

?? CheesyChart Create amazing Crypto and Stock charts ?? ?? Looking for an easy to use and fully customizable charting solution written in SwiftUI? Th

adri567 13 Dec 14, 2022
Easy to use Spider (Radar) Chart library for iOS written in Swift.

DDSpiderChart Easy to use Spider (Radar) Chart library for iOS written in Swift. Requirements iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+ Xcode

Deniz Adalar 82 Nov 14, 2022
Flower-like chart written in Swift

FlowerChart - custom chart written in Swift Fully vector flower-shaped chart written in Swift Flower-shaped chart written in Swift, this repo is a sam

Alexander Telegin 15 Jun 9, 2021
FlowerChart - custom chart written in Swift

FlowerChart - custom chart written in Swift Fully vector flower-shaped chart written in Swift Flower-shaped chart written in Swift, this repo is a sam

Alexander Telegin 15 Jun 9, 2021
Core Charts | Basic Scrollable Chart Library for iOS

Core Charts | Basic Chart Library for iOS HCoreBarChart VCoreBarChart Requirements Installation Usage Appearance Customization Getting Started You nee

Çağrı ÇOLAK 71 Nov 17, 2022
MSBBarChart is an easy to use bar chart library for iOS

MSBBarChart MSBBarChart is an easy to use bar chart library for iOS. Usage if you want to hide label above bar barChart.setOptions([.isHiddenLabelAbov

misyobun 45 May 3, 2022
A simple and beautiful chart lib used in Piner and CoinsMan for iOS(https://github.com/kevinzhow/PNChart) Swift Implementation

PNChart-Swift PNChart(https://github.com/kevinzhow/PNChart) Swift Implementation Installation This isn't on CocoaPods yet, so to install, add this to

Kevin 1.4k Nov 7, 2022
A simple and beautiful chart lib used in Piner and CoinsMan for iOS

PNChart You can also find swift version at here https://github.com/kevinzhow/PNChart-Swift A simple and beautiful chart lib with animation used in Pin

Kevin 9.8k Jan 6, 2023
iOS Chart. Support animation, click, scroll, area highlight.

XJYChart XJYChart - A High-performance, Elegant, Easy-to-integrate Charting Framework. The Best iOS Objc Charts. chart more beautiful support chart sc

junyixie 868 Nov 16, 2022
A simple and animated Pie Chart for your iOS app.

XYPieChart XYPieChart is an simple and easy-to-use pie chart for iOS app. It started from a Potion Project which needs an animated pie graph without i

XY Feng 1.7k Sep 6, 2022
iOS/iPhone/iPad Chart, Graph. Event handling and animation supported.

#EChart A highly extendable, easy to use chart with event handling, animation supported. ##Test How To Use Download and run the EChartDemo project is

Scott Zhu 646 Dec 27, 2022
an iOS open source Radar Chart implementation

JYRadarChart an open source iOS Radar Chart implementation ##Screenshots Requirements Xcode 5 or higher iOS 5.0 or higher ARC CoreGraphics.framework D

Johnny Wu 416 Dec 31, 2022
candlestick chart for ios

###Licenses (The MIT License) Copyright ©2012 zhiyu zheng all rights reserved. Permission is hereby granted, free of charge, to any person obtaining a

ZhiYu 962 Oct 17, 2022