An interactive line chart written in SwiftUI with many customizations.

Overview

LineChartView

LineChartView is a Swift Package written in SwiftUI to add a line chart to your app. It has many available customizations and is interactive (user can move finger on line to get values details).

It is really easy to use and add to your app. It only takes an array of Double values as mandatory parameter. All other parameters are here to customize visual aspect and interactions.

Features

  • Displays Double values in a line chart
  • Add labels to each value (as String)
  • Change label (value) and secondary label colors
  • Change labels alignment (left, center, right)
  • Change indicator point color and size
  • Change line color (simple color or linear gradient with two colors)
  • Display dots for each point/value on line
  • Enable/disable drag gesture
  • Enable/disable haptic feedback when drag on exact value
  • Use SwiftUI modifiers to custom chart (like background or frame size)

Installation

Add LineChartView package to your project.

In Xcode 13.1: File -> Add Packages... then enter my project GitHub URL:
https://github.com/Jonathan-Gander/LineChartView

Usage

Quick first chart

In file you want to add a chart:

import LineChartView

Then first create a LineChartParameters and set parameters to customize your brand new chart. Only first data parameter is mandatory to provide your values (as an array of Double):

let chartParameters = LineChartParameters(data: [42.0, 25.8, 88.19, 15.0])

Then create a LineChartView by passing your LineChartParameter:

LineChartView(lineChartParameters: chartParameters)

Complete example

Here is an example of a View displaying a chart with values and labels, and set its height:

import SwiftUI
import LineChartView

struct ContentView: View {
    
    private let data: [Double] = [42.0, 25.8, 88.19, 15.0]
    private let labels: [String] = ["The answer", "Birthday", "2021-11-21", "My number"]
    
    var body: some View {
        
        let chartParameters = LineChartParameters(data: data)
        LineChartView(lineChartParameters: chartParameters)
            .frame(height: 300)
    }
}

Customize your chart

To customize your chart, you can set parameters of LineChartParameters. Here are explanations of each parameter:

  • data: Array of Double containing values to display
  • dataLabels: Array of String containing label for each value
  • labelColor: Color of values text
  • secondaryLabelColor: Color of labels text
  • labelsAlignment: .left, .center, .right to align both labels above chart
  • indicatorPointColor: Color of indicator point displayed when user drag finger on chart
  • indicatorPointSize: Size of indicator point
  • lineColor: First color of line
  • lineSecondColor: If set, will display a linear gradient from left to right from lineColor to lineSecondColor
  • lineWidth: Line width
  • dotsWidth: Display a dot for each value (set to -1 to hide dots)
  • dragGesture: Enable or disable drag gesture on chart
  • hapticFeedback: Enable or disable haptic feedback on each value point

Example of a complete LineChartParameters:

let chartParameters = LineChartParameters(
    data: data,
    dataLabels: labels,
    labelColor: .primary,
    secondaryLabelColor: .secondary,
    labelsAlignment: .left,
    indicatorPointColor: .blue,
    indicatorPointSize: 20,
    lineColor: .blue,
    lineSecondColor: .purple,
    lineWidth: 3,
    dotsWidth: 8,
    dragGesture: true,
    hapticFeedback: true
)

I'm working on...

🚧 Developer at work 🚧

  • Animation when line is drawn

Licence

Be free to use my LineChartView Package in your app. Licence is available here. Please only add a copyright and licence notice.

Note that I've based my solution on stock-charts. I've totally modified and changed it but you may found similar code parts.

Comments
  • Option to specify precision or use [Int]

    Option to specify precision or use [Int]

    Hi,

    I love this library you built!

    I thought it might be nice to be able to have an option to round numbers to for example 0 decimal points, as I kind of want to use this for [Int]. When I convert to Double, the chart will always display XXX.00.

    Cheers!

    enhancement 
    opened by jojost1 3
  • Support timestamps as x values

    Support timestamps as x values

    Support of time serie as x values.

    Suggested here: link

    • [x] Points do not have equal space between them. Need to calculate space according to time spent between each.
    • [x] Can continue to have 'classical' x values (with equal space between them)
    new feature 
    opened by Jonathan-Gander 2
  • Improve data arrays as parameters

    Improve data arrays as parameters

    Now in LineChartParameters there are three ways to set data: data, dataTimestamps and dataLabels.

    I want to set an array of a custom struct containing all values / timestamps / labels for each value. It would be a lot easier to set the chart.

    opened by Jonathan-Gander 1
  • Moved minimum version to iOS 14

    Moved minimum version to iOS 14

    The project didn't need a minimum deployment target of iOS 15, so I moved it down to 14 so I could use it in a project for work.

    The only consequence is the removal of an unnecessary test, which was causing the package build to fail.

    opened by JordanOsterberg 1
  • Option to fill the area below the line

    Option to fill the area below the line

    For a look similar to https://github.com/pichukov/LightChart and https://github.com/AppPear/ChartView for example, I really like that look πŸ˜„

    Optionally give an option to provide two colors to display a gradient, similar to lineSecondColor.

    new feature 
    opened by jojost1 2
Owner
Jonathan Gander
IT Business Analyst, Software Engineer, Software Teacher
Jonathan Gander
Simple and intuitive iOS chart library. Contribution graph, clock chart, and bar chart.

TEAChart Simple and intuitive iOS chart library, for Pomotodo app. Contribution graph, clock chart, and bar chart. Supports Storyboard and is fully ac

柳东原 · Dongyuan Liu 1.2k Nov 29, 2022
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
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
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
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
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
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
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
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
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
SwiftUI Bar Chart

SwiftUI BarChart Lightweight and easy to use SwiftUI chart library for all Apple platforms Features Scaling on both axes Fully customizable axes (labe

Roman Baitaliuk 158 Jan 6, 2023
A SwiftUI Framework for Drawing Chart

PrettyAxis A SwiftUI Framework for drawing charts. Fearture Support Drawing Bar Chart RadarChart Line Chart and Scatter Charts Pie Chart and Donut Cha

RiuHDuo 24 Oct 2, 2022
Demo-implementation of 5 different Chart Libraries in SwiftUI

Comparison of Chart Libraries for SwiftUI Read the entire blog post including images on jannikarndt.de! I want to add charts to my SwiftUI iOS App, Ze

Jannik Arndt 73 Oct 12, 2022
A SwiftUI Contribution Chart (GitHub-like) implementation package

ContributionChart A contribution chart (aka. heatmap, GitHub-like) library for iOS, macOS, and watchOS. 100% written in SwiftUI. It Supports Custom Bl

null 41 Dec 27, 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
TKRadarChart - A customizable radar chart in Swift

TKRadarChart A customizable radar chart in Swift Requirements iOS 8.0+ Xcode 9.0 Swift 4.0 Installation CocoaPods You can use CocoaPods to install TKR

TBXark 203 Dec 28, 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