GlassChart
Swift Charts line, bar and area charts with axes, units and accessible descriptions.
- Content
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-chart
Content on an opaque, readable surface. Glass is kept for controls.
CaptureInstallation
spglass add glass-chartCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| 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
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.
Read-out and selection
| Parameter | Type | Default | Meaning |
|---|---|---|---|
selection | Binding<GlassChartX?>? | nil | The selected horizontal position. Without a binding the chart keeps its own. |
The header shows the title, the vertical axis title with its unit, and a read-out: the value of each series at the selected position, or at the last position when nothing is selected. Touching or dragging the plot selects a position (Swift Charts' own selection gesture, so scrolling keeps working) and draws a rule there. Gridlines are hairlines, axis labels use the theme's secondary text colour, lines are smoothed and carry point symbols, areas are a light fill under a line, bars have rounded corners and series sit side by side. The plot is between 180 and 260 points tall. GlassChartLogic.readout(of:at:) and nearestPosition(to:in:) are pure and public.
States
- Empty (no finite value): the title and a
ContentUnavailableViewreading "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
@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
showsSummaryisfalse. - 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
FormatStylein 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
primarycolour. 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.
Example
import SwiftUI
import SPGlassKit
public struct GlassChartExample: GlassExample {
public static let componentID = "glass-chart"
// Fictitious data: energy produced by two imaginary sites.
private static let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
private static let north: [Double] = [42, 48, 55, 61, 58, 70]
private static let south: [Double] = [65, 60, 57, 52, 49, 44]
private static let production: [GlassChartPoint] =
zip(months, north).map { GlassChartPoint(label: $0, value: $1, series: "North") }
+ zip(months, south).map { GlassChartPoint(label: $0, value: $1, series: "South") }
private static let visits: [GlassChartPoint] = {
let start = Date(timeIntervalSince1970: 1_780_272_000)
let values: [Double] = [120, 135, 128, 160, 190, 240, 210]
return values.enumerated().map { GlassChartPoint(date: start.addingTimeInterval(Double($0.offset) * 86_400), value: $0.element) }
}()
@State private var kind = GlassChartKind.line
@State private var series = "All"
public init() {}
private var visibleProduction: [GlassChartPoint] {
series == "All" ? Self.production : Self.production.filter { $0.series == series }
}
public var body: some View {
ScrollView {
VStack(spacing: 16) {
Picker("Mark", selection: $kind) {
Text("Line").tag(GlassChartKind.line)
Text("Bar").tag(GlassChartKind.bar)
Text("Area").tag(GlassChartKind.area)
}
.pickerStyle(.segmented)
GlassChart("Energy produced", points: visibleProduction, kind: kind, xAxisTitle: "Month", yAxisTitle: "Energy", unit: "MWh") {
Picker("Series", selection: $series) {
Text("All").tag("All")
ForEach(GlassChartLogic.seriesNames(Self.production), id: \.self) { Text(verbatim: $0).tag($0) }
}
.pickerStyle(.menu)
}
GlassChart("Daily visits", points: Self.visits, kind: .area, xAxisTitle: "Day", yAxisTitle: "Visits")
GlassChart("Nothing measured yet", points: [])
}
.padding()
}
}
}
#Preview("GlassChart") { GlassChartExample().glassTheme(.ocean) }
Registry entry
| Identifier | glass-chart |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-data-support, glass-foundation |
| Frameworks | SwiftUI, Charts |
| Public symbols | GlassChart, GlassChartKind, GlassChartX, GlassChartPoint, GlassChartSummary, GlassChartReadout, GlassChartLogic |
| Licence | MIT |
| Manifest | manifest.json |
Files
Data/Data/GlassChart.swift15972 bytes · sha256 a751f3edbcbb…Data/Data/GlassChartModel.swift11050 bytes · sha256 351c754bd589…