Getting Started with Chart view

Getting Started with Chart view

The Chart view in SwiftUI is the foundational component for creating various chart types. It serves as a container for different chart elements, such as marks, scales, and axes. The Chart view provides a declarative and flexible way to visualize data in the apps.

Let’s create a basic line chart to visualize temperature data over a week to see Chart view in action.

We will start with temperature model

struct TemperatureData: Identifiable {
  let id = UUID()
  let day: String
  let temperature: Double
}

Next, we will create a view model with some sample data

import Observation

@Observable
final class TemperatureViewModel {
  var temperatures: [TemperatureData] = [
    TemperatureData(day: "Mon", temperature: 52),
    TemperatureData(day: "Tue", temperature: 76),
    TemperatureData(day: "Wed", temperature: 75),
    TemperatureData(day: "Thu", temperature: 90),
    TemperatureData(day: "Fri", temperature: 76),
    TemperatureData(day: "Sat", temperature: 66),
    TemperatureData(day: "Sun", temperature: 75)
  ]
}

With the data structure in place, we are ready to create our first chart using the Charts framework.

import Charts
import SwiftUI

struct ExampleChartView: View {
  @State private var viewModel = TemperatureViewModel()
  var body: some View {
    Chart {
      ForEach(viewModel.temperatures) { temperature in
        LineMark(
          x: .value("Day", temperature.day),
          y: .value("Temperature", temperature.temperature)
        )
      }
    }
    .frame(height: 200)
    .padding()
  }
}

In this view, we use the Chart view to encapsulate the plotted chart. A ForEach loop iterates over the temperatures collection from the view model, creating a LineMark for each data point. The LineMark plots a line connecting these data points in the chart.

The Chart view offers numerous modifiers to customize its appearance and behavior (we will explore them in later chapters):

  • Chart Style: Modify the overall style of the chart using the chartStyle modifier.

  • Legend: Add a legend to explain the chart elements using the chartLegend modifier.

  • Axes: Customize the appearance and labels of the axes using chartXAxis and chartYAxis modifiers.

  • Title: Add a title to the chart using the chartTitle modifier.

  • Data Labels: Display data labels on the chart using the dataLabels modifier.

Beyond line charts, the Charts framework supports a variety of chart types, including:

  • Bar charts

  • Scatter plots

  • Area charts

  • Pie charts

  • Radar charts

  • And more

By combining different mark types and customizing the chart’s appearance, we can create complex and informative visualizations to suit various data scenarios.

The Chart view does all the heavy lifting by creating the necessary components for the given data. A basic Chart view consists of the following components:

  • Data: The data to be visualized, typically an array of data points.

  • Marks: Visual representations of data points, such as lines, bars, or points.

  • Scales: Define how data values are mapped to visual coordinates on the chart.

  • Axes: Provide context and reference points for interpreting chart data.

  • Modifiers: Customize the appearance and behavior of the chart.

The picture below shows the basic components generated by the Chart view.

Mastering Charts Framework in SwiftUI & iOS 18

Buy nowLearn more

Introduction

  • Introduction to Swift Charts
  • Benefits of Using Swift Charts Over Other Libraries
  • Getting Started with Chart view
  • Marks in Swift Charts

Bar Charts using BarMark

  • Bar Chart using BarMark
  • Bar Marks with Dynamic Data
  • Bar Mark Formatting Options
  • Bar Chart Foreground Style Scale
  • Enhance Bar Charts with Annotations
  • Stacked Bar Chart
  • Multi-Series Bar Chart
  • Custom Bar Placement in BarMark

Line Charts using LineMark

  • Creating Line Charts with LineMark
  • Annotating a Line Chart
  • Multi-Series Line Chart

Area Charts using AreaMark

  • Area Charts
  • Multi-Series Area Chart

RectangleMark in Charts Framework

  • Rectangle Mark in SwiftUI

Point Charts using PointMark

  • Point Chart for Scatter Plots

Combined Chart

  • Rule Mark & Combined Chart
  • Range Area Chart
  • Range Chart using BarMark, RectangleMark and RuleMark

SectorMarks for Pie Charts

  • Pie & Donut Charts with SectorMark
  • SwiftUI Pie & Donut Charts Quiz

Vectorized Plots

  • Vectorized Plots in Swift Charts (iOS 18)
  • AreaPlot : Vectorized Plots
  • BarPlot : Vectorized Plots
  • LinePlot : Vectorized Plots
  • PointPlot & RectanglePlot : Vectorized Plots
  • SectorPlot : Vectorized Plots

Chart Configurations

  • ChartPlotStyle, ChartBackground & ChartOverlay Customization
  • Charts Framework Legend
  • Chart Axis Content and Axis Marks
  • Chart Axis Configuration
  • Chart Scale Type
  • Chart Symbol Shape in Charts Framework
  • Align Chart’s Mark style with Chart Plot Area

Chart interactivity

  • Chart Scroll Behavior
  • Pie Chart Interaction with ChartAngleSelection
  • Chart Interaction using ChartOverlay
  • Interactive Line Chart in Swift Charts with ChartProxy and Gesture Overlay
  • Chart Gesture Modifier
  • Empty State Chart : Perfect Blend of LineMark, AreaMark and RuleMark

Animated Charts

  • Animated Bar and Line Charts
  • Animated Reveal Chart
  • Layered Chart Animation
  • Animated Pie Charts

3D Charts - Xcode 26+, iOS, macOS, visionOS, watchOS & iPadOS 26+

  • Chart3D View in Xcode 26+
  • 3D SurfacePlot Xcode 26+