Back to Chartview

SwiftUICharts Migration Guide

MIGRATION.md

2.0.04.8 KB
Original Source

SwiftUICharts Migration Guide

Target release

This guide targets 2.0.0.

2.0.0 is a major breaking release and phases out the old mutable chain API in favor of composable modifier-based configuration.

Version direction

This release moves to a strict SwiftUI-idiomatic modifier API.

  • Immutable configuration
  • ViewModifier composition
  • Environment keys instead of mutable reference state in view structs

Migration checklist

  1. Replace legacy chart types with LineChart, BarChart, PieChart, RingsChart.
  2. Replace old chain methods with chart... modifiers.
  3. Migrate interaction:
    • shared value model: chartInteractionValue(_:)
    • callback model: chartSelectionHandler(_:)
  4. Validate axis label/range behavior under the unified X-axis alignment model.
  5. Run swift test and build the showcase app to verify rendering parity.

Old -> new mapping

Previous chart data/range chains

OldNew
.data([Double]).chartData([Double])
.data([(Double, Double)]).chartData([(Double, Double)])
.rangeX(...).chartXRange(...)
.rangeY(...).chartYRange(...)

Grid chains

OldNew
.setNumberOfHorizontalLines(h) + .setNumberOfVerticalLines(v).chartGridLines(horizontal: h, vertical: v)
.setStoreStyle(style) + .setColor(color).chartGridStroke(style: style, color: color)
.showBaseLine(show, with: style).chartGridBaseline(show, style: style)

Axis chains

OldNew
.setAxisXLabels([String]).chartXAxisLabels([String])
.setAxisXLabels([(Double, String)], range:).chartXAxisLabels([(Double, String)], range:)
.setAxisYLabels([String], position:).chartYAxisLabels([String], position:)
.setAxisYLabels([(Double, String)], range:, position:).chartYAxisLabels([(Double, String)], range:, position:)
.setFont(font).chartAxisFont(font)
.setColor(color).chartAxisColor(color)

Line-specific chains

OldNew
.setLineWidth(width:).chartLineWidth(...)
.setBackground(colorGradient:).chartLineBackground(...)
.showChartMarks(_, with:).chartLineMarks(_, color:)
.setLineStyle(to:).chartLineStyle(...)
.withAnimation(_).chartLineAnimation(...)

Interaction wiring

OldNew
.chartValue(...) on chart views.chartInteractionValue(...) on any parent container
@EnvironmentObject ChartValue requirementoptional environment interaction value
N/A.chartSelectionHandler { event in ... } callback-based selection

Legacy public type replacements

Legacy typeReplacement
LineChartViewLineChart with modifiers
BarChartViewBarChart with modifiers
PieChartViewPieChart with modifiers
MultiLineChartViewmultiple LineChart overlays
LineViewCardView + LineChart + modifiers
GradientColorColorGradient
GradientColorsexplicit ColorGradient values
ColorsChartColors + Color
Stylesexplicit ChartStyle initialization
ChartFormSwiftUI layout (frame, stacks, spacing)
MultiLineChartDatamultiple chartData-configured line layers
MagnifierRectno direct replacement
TestDataapp/test-local fixtures

Example migration

Before:

swift
AxisLabels {
    ChartGrid {
        LineChart()
            .showChartMarks(true)
            .data([8, 23, 54, 32])
            .rangeY(0...60)
            .chartStyle(ChartStyle(backgroundColor: .white,
                                   foregroundColor: ColorGradient(.orange, .red)))
    }
    .setNumberOfHorizontalLines(5)
    .setNumberOfVerticalLines(4)
}
.setAxisXLabels(["Q1", "Q2", "Q3", "Q4"])

After:

swift
AxisLabels {
    ChartGrid {
        LineChart()
            .chartLineMarks(true)
            .chartData([8, 23, 54, 32])
            .chartYRange(0...60)
            .chartStyle(ChartStyle(backgroundColor: .white,
                                   foregroundColor: ColorGradient(.orange, .red)))
    }
    .chartGridLines(horizontal: 5, vertical: 4)
}
.chartXAxisLabels(["Q1", "Q2", "Q3", "Q4"])

Advanced migration examples

Dynamic streaming data

swift
@ObservedObject private var stream = ChartStreamingDataSource(initialValues: [12, 14, 18, 16],
                                                              windowSize: 8,
                                                              autoScroll: true)

LineChart()
    .chartData(stream)
    .chartYRange(stream.suggestedYRange)

Performance mode for large datasets

swift
LineChart()
    .chartData(largeSeries)
    .chartPerformance(.automatic(threshold: 600, maxPoints: 180, simplifyLineStyle: true))