From 17fc7cfdabb6c67e4f97972ffdeb7cd111a544b8 Mon Sep 17 00:00:00 2001 From: Gregory Sinnott Date: Sat, 10 Feb 2024 03:40:59 -0700 Subject: Basic chart implementation with tab view --- iCUrHealth/ContentView.swift | 86 +++++++++++++++++++++++++------------------- iCUrHealth/HealthChart.swift | 36 +++++++++++++++++++ iCUrHealth/UserCharts.swift | 47 ++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 iCUrHealth/HealthChart.swift create mode 100644 iCUrHealth/UserCharts.swift diff --git a/iCUrHealth/ContentView.swift b/iCUrHealth/ContentView.swift index 200e14c..2b532ce 100644 --- a/iCUrHealth/ContentView.swift +++ b/iCUrHealth/ContentView.swift @@ -38,48 +38,62 @@ struct ContentView: View { var body: some View { NavigationView { - VStack { - HStack { - Text("Steps") - Chart(data) { - BarMark(x: .value("Date", $0.dateInterval), - y: .value("Count", $0.data) - ) - - } - }.frame(maxHeight: 100) - Button(action: { - Task { - try await fetchStepCountData() - } - }) - { - Text("Exp Function") - } - Spacer() - .onAppear() { - if HKHealthStore.isHealthDataAvailable() { - trigger.toggle() - } - } - .healthDataAccessRequest(store: healthStore, - readTypes: allTypes, - trigger: trigger) { result in - switch result { - - case .success(_): - authenticated = true + TabView{ + VStack { + HStack { + Text("Steps") + Chart(data) { + BarMark(x: .value("Date", $0.dateInterval), + y: .value("Count", $0.data) + ) + + } + }.frame(maxHeight: 100) + Button(action: { Task { try await fetchStepCountData() } - case .failure(let error): - // Handle the error here. - fatalError("*** An error occurred while requesting authentication: \(error) ***") + }) + { + Text("Exp Function") } + Spacer() + .onAppear() { + if HKHealthStore.isHealthDataAvailable() { + trigger.toggle() + } + } + .healthDataAccessRequest(store: healthStore, + readTypes: allTypes, + trigger: trigger) { result in + switch result { + + case .success(_): + authenticated = true + Task { + try await fetchStepCountData() + } + case .failure(let error): + // Handle the error here. + fatalError("*** An error occurred while requesting authentication: \(error) ***") + } + } } + .padding() + .navigationTitle("iCUrHealth") + + .tabItem { + Image(systemName: "gear") + Text("Home") + } + let ch1 = userChart(type: "bar", data1: data) + let ch2 = userChart(type: "line", data1: data) + UserCharts(charts: [ch1, ch2]) + .tabItem { + Image(systemName: "chart.xyaxis.line") + Text("Charts") + } } - .padding() - .navigationTitle("iCUrHealth") } } diff --git a/iCUrHealth/HealthChart.swift b/iCUrHealth/HealthChart.swift new file mode 100644 index 0000000..971c8ea --- /dev/null +++ b/iCUrHealth/HealthChart.swift @@ -0,0 +1,36 @@ +// +// HealthChart.swift +// iCUrHealth +// +// Created by Gregory Sinnott on 2/10/24. +// + +import SwiftUI +import Charts + +struct HealthChart: View { + var chart: userChart + 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") + } + + } +} + +//#Preview { +// HealthChart() +//} diff --git a/iCUrHealth/UserCharts.swift b/iCUrHealth/UserCharts.swift new file mode 100644 index 0000000..739abdb --- /dev/null +++ b/iCUrHealth/UserCharts.swift @@ -0,0 +1,47 @@ +// +// UserCharts.swift +// iCUrHealth +// +// Created by Gregory Sinnott on 2/10/24. +// + +import SwiftUI + +struct userChart: Identifiable { + let type: String + let data1: [chartData] + var id = UUID() + func getTrend() -> Double{ + var trend: Double + var values: [Double] = [] + for dataPoint in data1 { + values.append(dataPoint.data) + } + trend = values.reduce(0.0, +) + return trend + } + +} + +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) + } + } + } + + } +} +// +//#Preview { +// UserCharts() +//} -- cgit v1.2.3