{"id":16853214,"url":"https://github.com/gpbl/swiftchart","last_synced_at":"2025-05-15T22:12:22.027Z","repository":{"id":22944561,"uuid":"26293937","full_name":"gpbl/SwiftChart","owner":"gpbl","description":"Line and area chart library for iOS","archived":false,"fork":false,"pushed_at":"2022-04-24T12:10:41.000Z","size":274,"stargazers_count":1058,"open_issues_count":44,"forks_count":165,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-05-15T12:46:13.571Z","etag":null,"topics":["area-chart","chart","charting-library","ios","ios-lib","line-charts","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gpbl.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-06T22:57:27.000Z","updated_at":"2025-04-16T02:38:23.000Z","dependencies_parsed_at":"2022-07-27T03:47:10.220Z","dependency_job_id":null,"html_url":"https://github.com/gpbl/SwiftChart","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbl%2FSwiftChart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbl%2FSwiftChart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbl%2FSwiftChart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpbl%2FSwiftChart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gpbl","download_url":"https://codeload.github.com/gpbl/SwiftChart/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254430335,"owners_count":22069909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["area-chart","chart","charting-library","ios","ios-lib","line-charts","swift"],"created_at":"2024-10-13T13:50:15.583Z","updated_at":"2025-05-15T22:12:21.969Z","avatar_url":"https://github.com/gpbl.png","language":"Swift","readme":"SwiftChart\n===========\n\n[![Version](https://img.shields.io/cocoapods/v/SwiftChart.svg?style=flat)](http://cocoapods.org/pods/SwiftChart)\n[![License](https://img.shields.io/cocoapods/l/SwiftChart.svg?style=flat)](http://cocoapods.org/pods/SwiftChart)\n[![Platform](https://img.shields.io/cocoapods/p/SwiftChart.svg?style=flat)](http://cocoapods.org/pods/SwiftChart)\n\nA simple line and area charting library for iOS.\n\n* 📈 Line and area charts \n* 🌞 Multiple series \n* 🌒 Partially filled series  \n* 🏊 Works with signed `Double`\n* 🖖 Touch events \n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://cloud.githubusercontent.com/assets/120693/11602670/57ef6b26-9adc-11e5-9f95-b226a2491654.png\" height=\"180\"\u003e\u003cimg src=\"https://cloud.githubusercontent.com/assets/120693/11602672/5c303ac6-9adc-11e5-9006-3275a16b7ec8.png\" height=\"180\"\u003e\n\u003cimg src=\"https://cloud.githubusercontent.com/assets/120693/11602674/5ed8a808-9adc-11e5-9e30-f55beacf9a94.png\" height=\"180\"\u003e\u003cimg src=\"https://cloud.githubusercontent.com/assets/120693/11602678/660d660e-9adc-11e5-8a67-0c3036c20862.gif\" height=\"180\"\u003e\n\u003c/p\u003e\n\n---\n\u003cdetails\u003e\n    \u003csummary\u003e\u003cstrong\u003eTable of Content\u003c/strong\u003e\u003c/summary\u003e\n\n\u003c!-- TOC --\u003e\n\n- [Getting started](#getting-started)\n  - [Installing SwiftChart via CocoaPods](#installing-swiftchart-via-cocoapods)\n  - [Installing SwiftChart manually](#installing-swiftchart-manually)\n  - [What’s included in SwiftChart](#whats-included-in-swiftchart)\n- [How to use SwiftChart](#how-to-use-swiftchart)\n  - [Initialize a chart from the Interface Builder](#initialize-a-chart-from-the-interface-builder)\n  - [Initialize a chart programmatically](#initialize-a-chart-programmatically)\n  - [Adding a series to a chart](#adding-a-series-to-a-chart)\n  - [Using partially filled series](#using-partially-filled-series)\n  - [Using different colors above and below zero](#using-different-colors-above-and-below-zero)\n  - [Adding multiple series to a chart](#adding-multiple-series-to-a-chart)\n  - [Configuring touch events](#configuring-touch-events)\n- [API](#api)\n  - [`Chart` class](#chart-class)\n    - [Chart options](#chart-options)\n    - [Public Methods](#public-methods)\n  - [`ChartSeries` class](#chartseries-class)\n  - [`ChartDelegate` protocol](#chartdelegate-protocol)\n  - [`ChartColors` enum](#chartcolors-enum)\n  - [`ChartPoint` typealias](#chartpoint-typealias)\n  - [`ChartLabelOrientation` enum](#chartlabelorientation-enum)\n- [Common issues and solutions](#common-issues-and-solutions)\n  - [The chart is not showing](#the-chart-is-not-showing)\n- [License](#license)\n\n\u003c!-- /TOC --\u003e\n\u003c/details\u003e\n\n\n# Getting started\n\n## Installing SwiftChart via CocoaPods\n\nSwiftChart is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod \"SwiftChart\"\n```\n\n## Installing SwiftChart manually\n\n1. Download **SwiftChart.zip** from the [last release](https://github.com/gpbl/SwiftChart/releases/latest) and extract its content in your project's folder.\n2. From the Xcode project, choose *Add Files to \u003cProjectName\u003e...* from the *File* menu and add the extracted files.\n\n## What’s included in SwiftChart\n\nThe library includes:\n\n- the [Chart](Source/Chart.swift#L40) main class, to initialize and configure the chart’s content, e.g. for adding series or setting up the its appearance\n- the [ChartSeries](Source/ChartSeries.swift) class, for creating datasets and configure their appearance\n- the [ChartDelegate](Source/Chart.swift#L10-L32) protocol, which tells other objects about the chart’s touch events\n- the [ChartColor](Source/ChartColors.swift) struct, containing some predefined colors\n\n**Example**\n\n```swift\nlet chart = Chart()\nlet series = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])\nseries.color = ChartColors.greenColor()\nchart.add(series)\n```\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n# How to use SwiftChart\n\n## Initialize a chart from the Interface Builder\n\nThe chart can be initialized from the Interface Builder. Drag a normal View into a View Controller and assign to it the `Chart` Custom Class from the Identity Inspector.\n\n## Initialize a chart programmatically\n\nTo initialize a chart programmatically, use the `Chart(frame: ...)` initializer, which requires a `frame`:\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\n```\n\nIf you prefer to use Autolayout, set the frame to `0` and add the constraints later:\n\n```swift\nlet chart = Chart(frame: CGRectZero)\n// add constraints now\n```\n\n## Adding a series to a chart\n\nInitialize each series before adding them to the chart. To do so, pass an array to initialize a `ChartSeries` object:\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\nlet series = ChartSeries([0, 6.5, 2, 8, 4.1, 7, -3.1, 10, 8])\nchart.add(series)\n```\n\n**Result:**\n\n\u003cimg width=\"400\" src=\"https://user-images.githubusercontent.com/120693/34648353-b66f352a-f398-11e7-98b9-9d15dcbdd692.png\"\u003e\n\nAs you can see, as default the values on the x-axis are the progressive indexes of the passed array. You can customize those values by passing an array of `(x: Double, y: Double)` tuples to the series initializer:\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\n// Create a new series specifying x and y values\nlet data = [\n  (x: 0, y: 0),\n  (x: 1, y: 3.1),\n  (x: 4, y: 2),\n  (x: 5, y: 4.2),\n  (x: 7, y: 5),\n  (x: 9, y: 9),\n  (x: 10, y: 8)\n]\nlet series = ChartSeries(data: data)\nchart.add(series)\n```\n\n**Result:**\n\n\u003cimg width=\"400\" src=\"https://user-images.githubusercontent.com/120693/34648477-f8a0c48a-f399-11e7-9e36-123171b6413b.png\"\u003e\n\n## Using partially filled series\n\nUse the `chart.xLabels` property to make the x-axis showing more labels than those inferred from the actual data. For example,\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\nlet data = [\n  (x: 0, y: 0),\n  (x: 3, y: 2.5),\n  (x: 4, y: 2),\n  (x: 5, y: 2.3),\n  (x: 7, y: 3),\n  (x: 8, y: 2.2),\n  (x: 9, y: 2.5)\n]\nlet series = ChartSeries(data: data)\nseries.area = true\n\n// Use `xLabels` to add more labels, even if empty\nchart.xLabels = [0, 3, 6, 9, 12, 15, 18, 21, 24]\n\n// Format the labels with a unit\nchart.xLabelsFormatter = { String(Int(round($1))) + \"h\" }\n\nchart.add(series)\n```\n\n**Result:**\n\n\u003cimg width=\"400\" src=\"https://user-images.githubusercontent.com/120693/34648482-28818ee6-f39a-11e7-99d3-0eb0f1402f73.png\"\u003e\n\n## Using different colors above and below zero\n\nThe chart displays the series in different colors when below or above the zero-axis:\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\nlet data: [Double] = [0, -2, -2, 3, -3, 4, 1, 0, -1]\n\nlet series = ChartSeries(data)\nseries.area = true\n\nchart.add(series)\n\n// Set minimum and maximum values for y-axis\nchart.minY = -7\nchart.maxY = 7\n\n// Format y-axis, e.g. with units\nchart.yLabelsFormatter = { String(Int($1)) +  \"ºC\" }\n```\n\n**Result:**\n\n\u003cimg width=\"410\" src=\"https://user-images.githubusercontent.com/120693/34648596-3f0538be-f39c-11e7-9cb3-ea06c025b09c.png\"\u003e\n\nYou can customize the zero-axis and the colors with the `colors` options in the `ChartSeries` class.\n\n```swift\nseries.colors = (\n  above: ChartColors.greenColor(),\n  below: ChartColors.yellowColor(),\n  zeroLevel: -1\n)\n```\n\n**Result:**\n\n\u003cimg width=\"410\" src=\"https://user-images.githubusercontent.com/120693/34648597-3f269158-f39c-11e7-90d3-d3dfb120c95d.png\"\u003e\n\n\n## Adding multiple series to a chart\n\nUsing the `chart.add(series: ChartSeries)` and `chart.add(series: Array\u003cChartSeries\u003e)` methods you can add more series. Those will be indentified with a progressive index in the chart’s `series` property.\n\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\n\nlet series1 = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])\nseries1.color = ChartColors.yellowColor()\nseries1.area = true\n\nlet series2 = ChartSeries([1, 0, 0.5, 0.2, 0, 1, 0.8, 0.3, 1])\nseries2.color = ChartColors.redColor()\nseries2.area = true\n\n// A partially filled series\nlet series3 = ChartSeries([9, 8, 10, 8.5, 9.5, 10])\nseries3.color = ChartColors.purpleColor()\n\nchart.add([series1, series2, series3])\n```\n\n**Result:**\n\n\u003cimg width=\"412\" alt=\"screen shot 2018-01-07 at 11 06 55\" src=\"https://user-images.githubusercontent.com/120693/34648532-282fcda8-f39b-11e7-93f3-c502329752b5.png\"\u003e\n\n## Configuring touch events\n\nTo make the chart respond to touch events, implement the `ChartDelegate` protocol in your class, e.g. a View Controller, and then set the chart’s `delegate` property:\n\n```swift\nclass MyViewController: UIViewController, ChartDelegate {\n  override func viewDidLoad() {\n    let chart = Chart(frame: CGRect(x: 0, y: 0, width: 100, height: 200))\n    chart.delegate = self\n  }\n\n  // Chart delegate\n  func didTouchChart(chart: Chart, indexes: Array\u003cInt?\u003e, x: Double, left: CGFloat) {\n    // Do something on touch\n  }\n\n  func didFinishTouchingChart(chart: Chart) {\n    // Do something when finished\n  }\n\n  func didEndTouchingChart(chart: Chart) {\n    // Do something when ending touching chart\n  }\n}\n```\n\nThe `didTouchChart` method passes an array of indexes, one for each series, with an optional `Int` referring to the data’s index:\n\n```swift\n func didTouchChart(chart: Chart, indexes: Array\u003cInt?\u003e, x: Double, left: CGFloat) {\n    for (seriesIndex, dataIndex) in enumerate(indexes) {\n      if dataIndex != nil {\n        // The series at `seriesIndex` is that which has been touched\n        let value = chart.valueForSeries(seriesIndex, atIndex: dataIndex)\n      }\n    }\n  }\n```\n\nYou can use `chart.valueForSeries()` to access the value for the touched position.\n\nThe `x: Double` argument refers to the value on the x-axis: it is inferred from the horizontal position of the touch event, and may be not part of the series values.\n\nThe `left: CGFloat` is the x position on the chart’s view, starting from the left side. It may be used to set the  position for a label moving above the chart:\n\n\u003cimg src=\"https://cloud.githubusercontent.com/assets/120693/11602678/660d660e-9adc-11e5-8a67-0c3036c20862.gif\" height=\"200\"\u003e\n\n# API\n\n## `Chart` class\n\nUse the `Chart` class to initialize and configure the chart’s content, e.g. for adding series or setting up the its appearance.\n\n**Example**\n\n```swift\nlet chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))\n```\n\n### Chart options\n\n| Option Name                   | Description                                                                                                                                                                                                 |\n|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `areaAlphaComponent`          | Alpha factor for the areas colors (`CGFloat`, default `0.1`)                                                                                                                                                |\n| `axesColor`                   | The color of the axes (`UIColor`, default `.gray`)                                                                                                                                                          |\n| `bottomInset`                 | Height of the area at the bottom of the chart, containing the labels for the x-axis  (`CGFloat`, default `20`)                                                                                              |\n| `delegate`                    | The delegate to listen to touch events (`ChartDelegate`)                                                                                                                                                    |\n| `highlightLineColor`          | The color of the highlight line (`UIColor`, default `gray`)                                                                                                                                                 |\n| `highlightLineWidth`          | The width of the highlight line (`CGFloat`, default `0.5`)                                                                                                                                                  |\n| `hideHighlightLineOnTouchEnd` | Hide the highlight line when the touch event ends, e.g. when stop swiping over the chart (`Bool`, default `false`)                                                                                         |\n| `gridColor`                   | The color of the grid (`UIColor`, default `.gray`)                                                                                                                                                          |\n| `labelColor`                  | The color of the labels (`UIColor`, default `.black`)                                                                                                                                                       |\n| `labelFont`                   | The font used for the labels (`UIFont?`)                                                                                                                                                                    |\n| `lineWidth`                   | The width of the chart's lines (`CGFloat`, default `2`)                                                                                                                                                     |\n| `maxX`                        | A custom maximum x-value (`Double?`)                                                                                                                                                                        |\n| `maxY`                        | A custom maximum y-value (`Double?`)                                                                                                                                                                        |\n| `minX`                        | A custom minimum x-value (`Double?`)                                                                                                                                                                        |\n| `minY`                        | A custom minimum y-value (`Double?`)                                                                                                                                                                        |\n| `showXLabelsAndGrid`          | Enable the lines for the labels on the x-axis (`Bool`, default `true`)                                                                                                                                      |\n| `showYLabelsAndGrid`          | Enable the lines for the labels on the y-axis (`Bool`, default `true`)                                                                                                                                      |\n| `topInset`                    | Height of the area at the top of the chart, acting a padding to make place for the top y-axis label (`CGFloat`, default `20`)                                                                               |\n| `xLabels`                     | The values to display as labels on the x-axis. You can format these values  with the `xLabelFormatter` attribute. As default, it will display the values of the series which has the most data. `[Double]?` |\n| `xLabelsFormatter`            | Function to format the labels on the x-axis (`(Int, Double) -\u003e String`)                                                                                                                                     |\n| `xLabelsOrientation:`         | Set the x-axis labels orientation to `vertical` or `horizontal` (`ChartLabelOrientation`, default `.horizontal`)                                                                                            |\n| `xLabelsTextAlignment:`       | Alignment for the text in the x-labels (`NSTextAlignment`, default `.left`)                                                                                                                                 |\n| `xLabelsSkipLast:`            | Skip the last x-label. Setting this to `false` will make the label overflow the frame width, so use carefully (`Bool`, default `true`)                                                                      |\n| `yLabels`                     | Values to display as labels of the y-axis. If not specified, will display the lowest, the middle and the highest values.                                                                                    |\n| `yLabelsFormatter`            | Function to format the labels on the y-axis (`(Int, Double) -\u003e String`)                                                                                                                                     |\n| `yLabelsOnRightSide`          | Place the y-labels on the right side (`Bool`, default `false`)                                                                                                                                              |\n\n### Public Methods\n\n| Method Name       | Description                                                                                                              |\n|-------------------|--------------------------------------------------------------------------------------------------------------------------|\n| `add`             | Add a series to the chart `(_ series: ChartSeries)` `(_ series: [ChartSeries])`                                          |\n| `removeSeriesAt`  | Remove the series at the specified index `(_ index: Int)`                                                                |\n| `removeAllSeries` | Remove all the series                                                                                                    |\n| `valueForSeries`  | Returns the value for the specified series at the given index `(_ seriesIndex: Int, atIndex dataIndex: Int?) -\u003e Double?` |\n\n## `ChartSeries` class\n\nUse the `ChartSeries` class to create a chart series and configure its appearance and behavior.\n\n**Example**\n\n```swift\nlet data: [Double] = [0, -2, -2, 3, -3, 4, 1, 0, -1]\nlet series = ChartSeries(data)\n```\n\n| Option Name | Description                                                                                                                                        |\n|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------|\n| `area`      | Draws an area below the series line (`Bool`, default `false`)                                                                                      |\n| `line`      | When set to `false`, will hide the series line. Useful for drawing only the area with `area=true` (`Bool`, default `true`)                         |\n| `color`     | The series color. You can use the `ChartColors` struct for some colors shortcuts. (`UIColor`, default `.blueColor()`)                              |\n| `colors`    | A tuple to specify the color above or below the zero (or the value specified by `zeroLevel`) `(above: UIColor, below: UIColor, zeroLevel: Double)` |\n\n## `ChartDelegate` protocol\n\nUse the `ChartDelegate` protocol to tell other objects about the chart’s touch events.\n\n| Method                   | Description                                                                                                                                            |\n|--------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `didTouchChart`          | Tells the delegate that the specified chart has been touched                                                                                           |\n| `didFinishTouchingChart` | Tells the delegate that the user finished touching the chart. The user will \"finish\" touching the chart only swiping left/right outside the chart.     |\n| `didEndTouchingChart`    | Tells the delegate that the user ended touching the chart. The user will \"end\" touching the chart whenever the `touchesDidEnd` method is being called. |\n\n## `ChartColors` enum\n\nShorthands for various colors.\n\n**Example**\n\n```swift\nlet series = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])\nseries.color = ChartColors.blueColor()\n```\n\n# Common issues and solutions\n\nIf you have issue with this library, please tag your question with `swiftchart` on [Stack Overflow](http://stackoverflow.com/tags/swiftcharts/info).\n\n## The chart is not showing\n\nThe `Chart` class inherits from `UIView`, so if your chart is not displaying it is likely a problem related to the view's size. Check your view constraints and make sure you initialize it on `viewDidLoad`, when UIKit can calculate the view dimensions.\n\nSome tips for debugging an hidden chart:\n\n* start your app and then debug the UI Hierarchy from the Debug navigator\n* initialize a simple UIView with a colored background instead of the chart to easily see how the view is positioned\n* try to not to nest the chart in a subview for better debugging\n\n# License\n\nSwiftChart is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpbl%2Fswiftchart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgpbl%2Fswiftchart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpbl%2Fswiftchart/lists"}