import SwiftUI import Charts import AnalyticsKit /// Daily values with an area fill, a faint grid, an emphasized last point and a hover/drag readout. public struct TrendChart: View { let days: [String], values: [Double], color: Color, label: String @State private var selected: String? @State private var plotWidth: CGFloat = 600 public init(days: [String], values: [Double], color: Color = Tide.accent, label: String) { self.days = days; self.values = values; self.color = color; self.label = label } public var body: some View { Chart { ForEach(Array(zip(days, values)), id: \.0) { day, v in AreaMark(x: .value("Day", Self.date(day)), y: .value(label, v)) .foregroundStyle(LinearGradient(colors: [color.opacity(0.28), color.opacity(0.02)], startPoint: .top, endPoint: .bottom)) LineMark(x: .value("Day", Self.date(day)), y: .value(label, v)).foregroundStyle(color).lineStyle(.init(lineWidth: 2)) } if let last = days.last, let v = values.last { PointMark(x: .value("Day", Self.date(last)), y: .value(label, v)).foregroundStyle(color).symbolSize(40) } if let selected, let i = days.firstIndex(of: selected) { RuleMark(x: .value("Day", Self.date(selected))).foregroundStyle(Tide.ink.opacity(0.3)) .annotation(position: .top, overflowResolution: .init(x: .fit, y: .disabled)) { VStack(alignment: .leading, spacing: 2) { Text(Self.date(selected), format: .dateTime.month().day()).font(Tide.caption) Text(Tide.number(values[i])).font(Tide.figure(13)) } .foregroundStyle(Tide.surface) .padding(.vertical, 6).padding(.horizontal, 8) .background(Tide.ink, in: RoundedRectangle(cornerRadius: 6)) .shadow(color: Color(light: 0x0E2830, dark: 0x000000).opacity(0.35), radius: 8, y: 6) } } } .chartXSelection(value: Binding(get: { selected.map(Self.date) }, set: { selected = $0.map { DayKey.string($0) } })) .chartYAxis { AxisMarks(position: .leading) { _ in AxisGridLine(stroke: StrokeStyle(lineWidth: 1, dash: [2, 3])).foregroundStyle(Tide.line); AxisValueLabel().font(Tide.figure(10.5)).foregroundStyle(Tide.muted) } } .chartXAxis { AxisMarks(values: dayTicks(days, width: plotWidth)) { value in AxisValueLabel(format: .dateTime.month(.abbreviated).day(), anchor: value.index == value.count - 1 ? .topTrailing : .top).font(Tide.figure(10.5)).foregroundStyle(Tide.muted) } } .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { plotWidth = $0 } .accessibilityLabel(label) } static func date(_ s: String) -> Date { ISO8601DateFormatter.day.date(from: s) ?? .now } } /// Stacked daily totals, one color per series (sites, or platforms for App Store downloads). public struct StackedChart: View { public struct Series: Identifiable { public let id: String; public let name: String; public let color: Color; public let values: [Double] public init(id: String, name: String, color: Color, values: [Double]) { self.id = id; self.name = name; self.color = color; self.values = values } } let days: [String], series: [Series], bars: Bool @State private var plotWidth: CGFloat = 600 public init(days: [String], series: [Series], bars: Bool = false) { self.days = days; self.series = series; self.bars = bars } public var body: some View { Chart { ForEach(series) { s in ForEach(Array(zip(days, s.values)), id: \.0) { day, v in if bars { BarMark(x: .value("Day", TrendChart.date(day), unit: .day), y: .value("Count", v)).foregroundStyle(by: .value("Series", s.name)) } else { AreaMark(x: .value("Day", TrendChart.date(day)), y: .value("Count", v), stacking: .standard).foregroundStyle(by: .value("Series", s.name)) } } } } .chartForegroundStyleScale(domain: series.map(\.name), range: series.map(\.color)) .chartYAxis { AxisMarks(position: .leading) { _ in AxisGridLine(stroke: StrokeStyle(lineWidth: 1, dash: [2, 3])).foregroundStyle(Tide.line); AxisValueLabel().font(Tide.figure(10.5)).foregroundStyle(Tide.muted) } } .chartXAxis { AxisMarks(values: dayTicks(days, width: plotWidth)) { value in AxisValueLabel(format: .dateTime.month(.abbreviated).day(), anchor: value.index == value.count - 1 ? .topTrailing : .top).font(Tide.figure(10.5)).foregroundStyle(Tide.muted) } } .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { plotWidth = $0 } .chartLegend(position: .bottom, alignment: .leading) { HStack(spacing: 14) { ForEach(series) { s in HStack(spacing: 6) { RoundedRectangle(cornerRadius: 2).fill(s.color).frame(width: 9, height: 9) Text(s.name).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(1) } } } } } } public struct Sparkline: View { let values: [Double], color: Color public init(_ values: [Double], color: Color) { self.values = values; self.color = color } public var body: some View { Chart(Array(values.enumerated()), id: \.offset) { i, v in AreaMark(x: .value("i", i), y: .value("v", v)).foregroundStyle(color.opacity(0.18)) LineMark(x: .value("i", i), y: .value("v", v)).foregroundStyle(color) } .chartXAxis(.hidden).chartYAxis(.hidden).frame(width: 80, height: 26) .accessibilityHidden(true) } } /// Ranked rows: label and value on one line, a thin bar under them, so the fill never cuts through text. public struct BarList: View { let rows: [(String, Double)] public init(_ rows: [(String, Double)]) { self.rows = rows } public var body: some View { let top = max(1, rows.map(\.1).max() ?? 1) VStack(spacing: 10) { ForEach(rows, id: \.0) { label, v in VStack(spacing: 5) { HStack(alignment: .firstTextBaseline) { Text(label).lineLimit(1).truncationMode(.middle).font(Tide.secondary).foregroundStyle(Tide.ink) Spacer(minLength: 8) Text(Tide.number(v)).font(Tide.figure(12)).foregroundStyle(Tide.ink) } GeometryReader { g in ZStack(alignment: .leading) { Capsule().fill(Tide.line) Capsule().fill(Tide.accent).frame(width: max(4, g.size.width * v / top)) } }.frame(height: 5) } } } } } /// X-axis ticks counted back from the last day, so the newest day always gets a label, spaced /// for the chart's width (about 90 pt per date, room for the newest label to shift inside the edge) so labels never touch. func dayTicks(_ days: [String], width: CGFloat) -> [Date] { guard !days.isEmpty else { return [] } let fits = max(2, Int(width / 90)) let needed = Int((Double(days.count - 1) / Double(fits - 1)).rounded(.up)) let steps = days.count <= 10 ? [1, 2, 3, 5] : [7, 14, 21, 30, 60, 90] let step = steps.first { $0 >= needed } ?? needed return stride(from: days.count - 1, through: 0, by: -step).map { TrendChart.date(days[$0]) }.reversed() } extension ISO8601DateFormatter { nonisolated(unsafe) static let day: ISO8601DateFormatter = { let f = ISO8601DateFormatter(); f.formatOptions = [.withFullDate]; f.timeZone = .current; return f }() }