SPB Git

Repository Public MIT

glass-chart.md 4.3 KB

# GlassChart

Identifier glass-chart
Module SPGlassData
Kind content-component — Swift Charts on an opaque surface; glass only for the optional floating control
Platforms iOS 26, iPadOS 26, macOS 26
Frameworks Charts
Example Examples/Gallery/Data/GlassChartExample.swift

# Minimal usage

swift
GlassChart("Monthly revenue", points: [
    GlassChartPoint(label: "Jan", value: 42),
    GlassChartPoint(label: "Feb", value: 48),
    GlassChartPoint(label: "Mar", value: 55),
])

# Variants

Variant Use
kind: .line Evolution of a value. Series are told apart by colour, by symbol and by the legend.
kind: .bar Comparison between categories.
kind: .area Volume over time; several series are stacked by Swift Charts.
Category axis Points created with label:.
Time axis Used when every point is created with date:.
Several series Points with a series name; a legend appears.
Overlay control A trailing closure with a small control (series or range picker), drawn on a glass capsule over the top trailing corner of the plot.

# Parameters

Parameter Type Default Meaning
title LocalizedStringKey — Heading and accessibility label of the chart.
points [GlassChartPoint] — Typed values. Non-finite values are skipped.
kind GlassChartKind .line Mark type.
xAxisTitle String? nil Title of the horizontal axis, already localised.
yAxisTitle String? nil Title of the vertical axis, already localised.
unit String? nil Unit, appended to the vertical axis title ("Energy (MWh)") and to spoken values.
showsSummary Bool true Shows the generated description under the plot.
overlay @ViewBuilder none Control floating on glass.

GlassChartPoint(id:label:value:series:) and GlassChartPoint(id:date:value:series:); without an id, one is derived from the series and the position.

# States

  • Empty (no finite value): the title and a ContentUnavailableView reading "No data".
  • Populated: the plot, the optional overlay control and the summary sentence.
  • Loading and failure are the host's responsibility (wrap the chart, or show a GlassMetricCard-style state around it).

# Advanced example

swift
@State private var series = "All"

var visible: [GlassChartPoint] {
    series == "All" ? production : production.filter { $0.series == series }
}

GlassChart("Energy produced", points: visible, kind: .area, xAxisTitle: "Month", yAxisTitle: "Energy", unit: "MWh") {
    Picker("Series", selection: $series) {
        Text("All").tag("All")
        ForEach(GlassChartLogic.seriesNames(production), id: \.self) { Text(verbatim: $0).tag($0) }
    }
    .pickerStyle(.menu)
}

// The description is a pure function, usable outside the view:
let text = GlassChartLogic.description(of: production, unit: "MWh")
// "North: 6 values from Jan to Jun. Minimum 42 MWh, maximum 70 MWh. Trend: Up. South: …"

# Accessibility

  • Every mark has an accessibility label (series and position) and value (formatted number and unit), so the chart can be explored element by element and with the system's audio graph.
  • A sentence per series states the number of values, the first and last positions, the minimum, the maximum and the trend. It is visible under the plot, or kept as an accessibility-only element when showsSummary is false.
  • Series never rely on colour alone: the legend names them and line charts add a distinct symbol per series.
  • Numbers and dates are formatted with FormatStyle in the environment's locale and time zone.

# Limits

  • The trend compares the last value with the first one; it is flat when they differ by at most 1 % of the series' scale. It is a description, not a regression.
  • The overlay control covers the top trailing corner of the plot; keep it small.
  • Series colours come from Swift Charts' default palette; a single series uses the theme's primary colour. Per-series theme colours are not exposed.
  • No selection, zooming or scrolling of the plot: use Swift Charts directly for interactive charts.
  • A chart mixing dated and labelled points uses a category axis, with dates shown as abbreviated text.
  • Rendering was compiled for macOS and iOS; the visual result was not checked on a device in this phase.
Files main
Menu

Theme