Repository Public MIT
1import SwiftUI2import SPGlassKit34public struct GlassChartExample: GlassExample {5 public static let componentID = "glass-chart"67 // Fictitious data: energy produced by two imaginary sites.8 private static let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]9 private static let north: [Double] = [42, 48, 55, 61, 58, 70]10 private static let south: [Double] = [65, 60, 57, 52, 49, 44]1112 private static let production: [GlassChartPoint] =13 zip(months, north).map { GlassChartPoint(label: $0, value: $1, series: "North") }14 + zip(months, south).map { GlassChartPoint(label: $0, value: $1, series: "South") }1516 private static let visits: [GlassChartPoint] = {17 let start = Date(timeIntervalSince1970: 1_780_272_000)18 let values: [Double] = [120, 135, 128, 160, 190, 240, 210]19 return values.enumerated().map { GlassChartPoint(date: start.addingTimeInterval(Double($0.offset) * 86_400), value: $0.element) }20 }()2122 @State private var kind = GlassChartKind.line23 @State private var series = "All"2425 public init() {}2627 private var visibleProduction: [GlassChartPoint] {28 series == "All" ? Self.production : Self.production.filter { $0.series == series }29 }3031 public var body: some View {32 ScrollView {33 VStack(spacing: 16) {34 Picker("Mark", selection: $kind) {35 Text("Line").tag(GlassChartKind.line)36 Text("Bar").tag(GlassChartKind.bar)37 Text("Area").tag(GlassChartKind.area)38 }39 .pickerStyle(.segmented)4041 GlassChart("Energy produced", points: visibleProduction, kind: kind, xAxisTitle: "Month", yAxisTitle: "Energy", unit: "MWh") {42 Picker("Series", selection: $series) {43 Text("All").tag("All")44 ForEach(GlassChartLogic.seriesNames(Self.production), id: \.self) { Text(verbatim: $0).tag($0) }45 }46 .pickerStyle(.menu)47 }4849 GlassChart("Daily visits", points: Self.visits, kind: .area, xAxisTitle: "Day", yAxisTitle: "Visits")5051 GlassChart("Nothing measured yet", points: [])52 }53 .padding()54 }55 }56}5758#Preview("GlassChart") { GlassChartExample().glassTheme(.ocean) }59