Charts for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Charts for Swift

\n\n

Charts is a powerful and flexible open-source library for data visualization on Apple platforms (iOS, tvOS, macOS, watchOS). It is written in Swift and provides a wide range of chart types: from simple line and bar charts to complex combined and bubble charts. The library is a fork of the famous iOS library ios-charts, which in turn was ported from the Java library MPAndroidChart.

\n\n

Why use Charts?

\n
    \n
  • Data Visualization: Transforms boring numbers into clear graphs and charts.
  • \n
  • Rich Functionality: Supports animation, interactive interaction (touches, highlighting), legends, axes, data markers, and much more.
  • \n
  • Native Performance: Written in Swift, uses Core Graphics and Core Animation, ensuring smooth operation even with large datasets.
  • \n
  • Easy Integration: Easily installed via Swift Package Manager, CocoaPods, or Carthage.
  • \n
  • Active Community: Regularly updated, has extensive documentation and many examples.
  • \n
\n\n

Installing the Charts Library

\n\n

Charts can be installed using one of three main methods. The recommended method is Swift Package Manager (SPM).

\n\n

1. Swift Package Manager (SPM) — Recommended Method

\n
    \n
  1. In Xcode, open your project.
  2. \n
  3. Select File → Add Packages...
  4. \n
  5. In the search field, enter the repository URL: https://github.com/danielgindi/Charts.git
  6. \n
  7. Select the desired version (usually the latest stable one).
  8. \n
  9. Click Add Package.
  10. \n
  11. Ensure the package is added to your target.
  12. \n
\n\n

2. CocoaPods

\n

Add to your Podfile:

\n
platform :ios, '13.0'\nuse_frameworks!\n\ntarget 'YourApp' do\n  pod 'Charts'\nend
\n

Then run the command in the terminal:

\n
pod install
\n\n

3. Carthage

\n

Add to your Cartfile:

\n
github "danielgindi/Charts"
\n

Then run:

\n
carthage update
\n\n

Quick Start: Creating Your First Chart

\n\n

Let's create a simple line chart displaying the temperature change over a week.

\n\n
    \n
  1. Import the library into your controller:\n
    import UIKit\nimport Charts
    \n
  2. \n
  3. Add a View on Storyboard or create it programmatically:\n

    On the storyboard, drag a UIView and change its class to LineChartView in the Identity Inspector. Create an IBOutlet:

    \n
    @IBOutlet weak var lineChartView: LineChartView!
    \n
  4. \n
  5. Configure the data:\n
    func setupLineChart() {\n    // 1. Create data points\n    let dataEntries: [ChartDataEntry] = [\n        ChartDataEntry(x: 1.0, y: 20.0),\n        ChartDataEntry(x: 2.0, y: 22.0),\n        ChartDataEntry(x: 3.0, y: 19.0),\n        ChartDataEntry(x: 4.0, y: 25.0),\n        ChartDataEntry(x: 5.0, y: 23.0),\n        ChartDataEntry(x: 6.0, y: 21.0),\n        ChartDataEntry(x: 7.0, y: 26.0)\n    ]\n\n    // 2. Create a data set\n    let lineDataSet = LineChartDataSet(entries: dataEntries, label: "Temperature (°C)")\n    lineDataSet.colors = [.systemBlue] // Line color\n    lineDataSet.circleColors = [.systemRed] // Point color\n    lineDataSet.circleRadius = 4.0\n    lineDataSet.lineWidth = 2.0\n\n    // 3. Create a data object\n    let lineData = LineChartData(dataSet: lineDataSet)\n\n    // 4. Assign data to the chart\n    lineChartView.data = lineData\n\n    // 5. Additional settings\                    

Recommendations