|
| 1 | +import SwiftUI |
| 2 | +import SwiftUICharts |
| 3 | +import UIKit |
| 4 | + |
| 5 | +struct ShowcaseDynamicLabView: View { |
| 6 | + @ObservedObject private var stream = ChartStreamingDataSource(initialValues: [28, 31, 30, 35, 33, 36, 34, 37], |
| 7 | + windowSize: 8, |
| 8 | + autoScroll: true) |
| 9 | + @State private var timer: Timer? |
| 10 | + @State private var callbackText = "Touch bars to receive callback events." |
| 11 | + |
| 12 | + private var pageBackgroundColor: Color { Color(UIColor.systemGroupedBackground) } |
| 13 | + private var cardBackgroundColor: Color { Color(UIColor.secondarySystemGroupedBackground) } |
| 14 | + private var chartSurfaceColor: Color { Color(UIColor.secondarySystemBackground) } |
| 15 | + |
| 16 | + var body: some View { |
| 17 | + NavigationView { |
| 18 | + ScrollView { |
| 19 | + VStack(alignment: .leading, spacing: 16) { |
| 20 | + liveStreamSection |
| 21 | + callbackSection |
| 22 | + } |
| 23 | + .padding(16) |
| 24 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 25 | + } |
| 26 | + .navigationBarTitle("Dynamic Data Lab", displayMode: .inline) |
| 27 | + .background(pageBackgroundColor) |
| 28 | + } |
| 29 | + .onAppear(perform: startFeed) |
| 30 | + .onDisappear(perform: stopFeed) |
| 31 | + } |
| 32 | + |
| 33 | + private var liveStreamSection: some View { |
| 34 | + VStack(alignment: .leading, spacing: 8) { |
| 35 | + Text("Streaming Feed (Mock Dynamic)") |
| 36 | + .font(.headline) |
| 37 | + Text("A timer appends points continuously to emulate live network updates.") |
| 38 | + .font(.caption) |
| 39 | + .foregroundColor(.secondary) |
| 40 | + |
| 41 | + AxisLabels { |
| 42 | + ChartGrid { |
| 43 | + LineChart() |
| 44 | + .chartData(stream) |
| 45 | + .chartYRange(stream.suggestedYRange) |
| 46 | + .chartStyle(ChartStyle(backgroundColor: chartSurfaceColor, |
| 47 | + foregroundColor: ColorGradient(.green, .blue))) |
| 48 | + .chartLineMarks(true, color: ColorGradient(.green, .blue)) |
| 49 | + } |
| 50 | + .chartGridLines(horizontal: 5, vertical: max(2, stream.values.count)) |
| 51 | + } |
| 52 | + .chartXAxisLabels(stream.xLabels) |
| 53 | + .chartYAxisAutoTicks(5, format: .number) |
| 54 | + .chartAxisFont(.caption) |
| 55 | + .chartAxisColor(.secondary) |
| 56 | + .frame(height: 240) |
| 57 | + .padding(12) |
| 58 | + .background(RoundedRectangle(cornerRadius: 14).fill(cardBackgroundColor)) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private var callbackSection: some View { |
| 63 | + VStack(alignment: .leading, spacing: 8) { |
| 64 | + Text("Selection Callback Output") |
| 65 | + .font(.headline) |
| 66 | + Text(callbackText) |
| 67 | + .font(.caption.monospacedDigit()) |
| 68 | + .foregroundColor(.secondary) |
| 69 | + |
| 70 | + AxisLabels { |
| 71 | + ChartGrid { |
| 72 | + BarChart() |
| 73 | + .chartData(stream.values) |
| 74 | + .chartStyle(ChartStyle(backgroundColor: chartSurfaceColor, |
| 75 | + foregroundColor: [ |
| 76 | + ColorGradient(.orange, .red), |
| 77 | + ColorGradient(.blue, .purple), |
| 78 | + ColorGradient(.green, .yellow) |
| 79 | + ])) |
| 80 | + } |
| 81 | + .chartGridLines(horizontal: 4, vertical: 0) |
| 82 | + } |
| 83 | + .chartXAxisLabels(stream.xLabels) |
| 84 | + .chartYAxisAutoTicks(4, format: .number) |
| 85 | + .chartAxisFont(.caption) |
| 86 | + .chartAxisColor(.secondary) |
| 87 | + .chartSelectionHandler { event in |
| 88 | + guard event.isActive, |
| 89 | + let value = event.value, |
| 90 | + let index = event.index else { |
| 91 | + callbackText = "No active selection" |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + callbackText = "Slot \(index + 1): \(String(format: "%.2f", value))" |
| 96 | + } |
| 97 | + .frame(height: 230) |
| 98 | + .padding(12) |
| 99 | + .background(RoundedRectangle(cornerRadius: 14).fill(cardBackgroundColor)) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private func startFeed() { |
| 104 | + guard timer == nil else { return } |
| 105 | + |
| 106 | + let next = Timer.scheduledTimer(withTimeInterval: 1.5, repeats: true) { _ in |
| 107 | + let drift = Double.random(in: -2.0...2.0) |
| 108 | + let value = min(45, max(20, stream.latestValue + drift)) |
| 109 | + stream.append(value) |
| 110 | + } |
| 111 | + next.tolerance = 0.25 |
| 112 | + timer = next |
| 113 | + } |
| 114 | + |
| 115 | + private func stopFeed() { |
| 116 | + timer?.invalidate() |
| 117 | + timer = nil |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +struct ShowcaseDynamicLabView_Previews: PreviewProvider { |
| 122 | + static var previews: some View { |
| 123 | + ShowcaseDynamicLabView() |
| 124 | + } |
| 125 | +} |
0 commit comments