summaryrefslogtreecommitdiff
path: root/iCUrHealth/HealthChart.swift
diff options
context:
space:
mode:
Diffstat (limited to 'iCUrHealth/HealthChart.swift')
-rw-r--r--iCUrHealth/HealthChart.swift79
1 files changed, 64 insertions, 15 deletions
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()
//}