diff options
-rw-r--r-- | iCUrHealth/ContentView.swift | 11 | ||||
-rw-r--r-- | iCUrHealth/HealthChart.swift | 79 | ||||
-rw-r--r-- | iCUrHealth/UserCharts.swift | 14 |
3 files changed, 76 insertions, 28 deletions
diff --git a/iCUrHealth/ContentView.swift b/iCUrHealth/ContentView.swift index 2f6933a..d544e09 100644 --- a/iCUrHealth/ContentView.swift +++ b/iCUrHealth/ContentView.swift @@ -11,7 +11,7 @@ import HealthKitUI import Charts struct chartData: Identifiable { - + let tag: String let dateInterval: Date let data: Double var id: TimeInterval { dateInterval.timeIntervalSince1970 } @@ -86,9 +86,10 @@ struct ContentView: View { Image(systemName: "gear") Text("Home") } - let ch1 = userChart(type: "bar", data1: data) - let ch2 = userChart(type: "line", data1: data) - UserCharts(charts: [ch1, ch2]) + let ch1 = userChart(type: "bar", metric:"Steps", data: data) + let ch2 = userChart(type: "line", metric:"Steps", data: data) + let ch3 = userChart(type: "trend", metric:"Steps", data: data) + UserCharts(charts: [ch1]) .tabItem { Image(systemName: "chart.xyaxis.line") Text("Charts") @@ -141,7 +142,7 @@ struct ContentView: View { if let quantity = stats.sumQuantity() { //print(quantity, stats.startDate) dailyData.append( - chartData(dateInterval: stats.startDate, data: quantity.doubleValue(for: HKUnit.count())) + chartData(tag: "activity", dateInterval: stats.startDate, data: quantity.doubleValue(for: HKUnit.count())) ) } else { } diff --git a/iCUrHealth/HealthChart.swift b/iCUrHealth/HealthChart.swift index 971c8ea..c520228 100644 --- a/iCUrHealth/HealthChart.swift +++ b/iCUrHealth/HealthChart.swift @@ -8,29 +8,78 @@ import SwiftUI import Charts +func generateTrendData(chart: [chartData], average: Double) -> [chartData]{ + var trendData: [chartData] = [] + for chartPoint in chart { + trendData.append(chartData(tag: "trend", dateInterval: chartPoint.dateInterval, data: average)) + } + return trendData +} + +func combineTrend(chart: [chartData], trend: [chartData]) -> [chartData]{ + return(chart+trend) +} + struct HealthChart: View { var chart: userChart + var average: Double var body: some View { - switch chart.type{ - case "bar": - Chart(chart.data1) { - BarMark(x: .value("Date", $0.dateInterval), - y: .value("Count", $0.data) - ) - } - case "line": - Chart(chart.data1) { - LineMark(x: .value("Date", $0.dateInterval), - y: .value("Count", $0.data) - ) - } - default: - Text("No chart found") + VStack{ + Text("Your Charts").font(.title) + switch chart.type{ + case "bar": + ScrollView{ + VStack(alignment: .leading){ + VStack(alignment: .leading) { + Text(chart.metric) + .font(.title3).bold() + Text("Last 30 days") + .font(.subheadline) + .foregroundStyle(.secondary) + .padding(.bottom) + } + Chart(chart.data) { + BarMark(x: .value("Date", $0.dateInterval), + y: .value("Count", $0.data) + ) + RuleMark(y: .value("Average", average)) + .foregroundStyle(Color.secondary) + .lineStyle(StrokeStyle(lineWidth: 0.8, dash: [10])) + .annotation(alignment: .bottomTrailing) { + Text(String(format: "Your average is: %.0f", average)) + .font(.subheadline).bold() + .padding(.trailing, 32) + .foregroundStyle(Color.secondary) + } + } + } + } + .frame(height: 300) + .padding() + + case "line": + Chart(chart.data) { + LineMark(x: .value("Date", $0.dateInterval), + y: .value("Count", $0.data) + ) + RuleMark(y: .value("Average", average)) + .foregroundStyle(Color.secondary) + } + case "trend": + Chart(generateTrendData(chart: chart.data, average: average)) { + LineMark(x: .value("Date", $0.dateInterval), + y: .value("Count", $0.data) + ) + } + default: + Text("No chart found") + } } } } + //#Preview { // HealthChart() //} diff --git a/iCUrHealth/UserCharts.swift b/iCUrHealth/UserCharts.swift index 739abdb..fe78dcd 100644 --- a/iCUrHealth/UserCharts.swift +++ b/iCUrHealth/UserCharts.swift @@ -9,15 +9,17 @@ import SwiftUI struct userChart: Identifiable { let type: String - let data1: [chartData] + let metric: String + let data: [chartData] var id = UUID() func getTrend() -> Double{ var trend: Double var values: [Double] = [] - for dataPoint in data1 { + for dataPoint in data { values.append(dataPoint.data) } - trend = values.reduce(0.0, +) + let sum = values.reduce(0.0, +) + trend = sum / Double(values.count) return trend } @@ -27,14 +29,10 @@ struct UserCharts: View { var charts: [userChart] var body: some View { VStack{ - ForEach(charts) { chart in VStack{ - let trend = chart.getTrend() - let trendString = String(trend) - Text(trendString) - HealthChart(chart: chart) + HealthChart(chart: chart, average: chart.getTrend()) } } } |