diff --git a/Gas Man/Stats/DistanceBetweenFillupsTrendChartView.swift b/Gas Man/Stats/DistanceBetweenFillupsTrendChartView.swift new file mode 100644 index 0000000..cb960b4 --- /dev/null +++ b/Gas Man/Stats/DistanceBetweenFillupsTrendChartView.swift @@ -0,0 +1,64 @@ +// +// DistanceBetweenFillupsTrendChartView.swift +// Gas Man +// +// Created by Kameron Kenny on 3/19/25. +// + + +import SwiftUI +import Charts +import CoreData + +struct DistanceBetweenFillupsTrendChartView: View { + let fuelLogs: [FuelLog] + + // Compute trend data by sorting logs by date ascending, + // then mapping each log's date and pricePerGalon. + var trendData: [(date: Date, distance: Double)] { + let sortedLogs = fuelLogs.sorted { ($0.date ?? Date()) < ($1.date ?? Date()) } + var data: [(Date, Double)] = [] + // Start from index 1 because we need a previous log to compute the difference. + for i in 1.. previous.odometer, + current.fuelVolume > 0 { + let distance = (current.odometer - previous.odometer) + data.append((date, distance)) + } + } + return data + } + + var body: some View { + VStack { + if trendData.isEmpty { + Text("No trend data available.") + .foregroundColor(.secondary) + } else { + Chart { + ForEach(trendData, id: \.date) { point in + LineMark( + x: .value("Date", point.date), + y: .value("Distance", point.distance) + ) + PointMark( + x: .value("Date", point.date), + y: .value("Price", point.distance) + ) + } + } + .chartXAxis { + AxisMarks(values: .automatic(desiredCount: 4)) + } + .chartYAxis { + AxisMarks(values: .automatic(desiredCount: 5)) + } + } + } + .padding() + .navigationTitle("Distance Between Fuel-ups Trend") + } +} diff --git a/Gas Man/Stats/GalPerFuelUpTrendChartView.swift b/Gas Man/Stats/GalPerFuelUpTrendChartView.swift new file mode 100644 index 0000000..0e52248 --- /dev/null +++ b/Gas Man/Stats/GalPerFuelUpTrendChartView.swift @@ -0,0 +1,58 @@ +// +// GalPerFuelUpTrendChartView.swift +// Gas Man +// +// Created by Kameron Kenny on 3/19/25. +// + + +import SwiftUI +import Charts +import CoreData + +struct GalPerFuelUpTrendChartView: View { + let fuelLogs: [FuelLog] + + // Compute trend data by sorting logs by date ascending, + // then mapping each log's date and pricePerGalon. + var trendData: [(date: Date, gal: Double)] { + let sortedLogs = fuelLogs.sorted { ($0.date ?? Date()) < ($1.date ?? Date()) } + return sortedLogs.compactMap { log in + if let date = log.date { + return (date, log.fuelVolume) + } else { + return nil + } + } + } + + var body: some View { + VStack { + if trendData.isEmpty { + Text("No trend data available.") + .foregroundColor(.secondary) + } else { + Chart { + ForEach(trendData, id: \.date) { point in + LineMark( + x: .value("Date", point.date), + y: .value("Price", point.gal) + ) + PointMark( + x: .value("Date", point.date), + y: .value("Price", point.gal) + ) + } + } + .chartXAxis { + AxisMarks(values: .automatic(desiredCount: 4)) + } + .chartYAxis { + AxisMarks(values: .automatic(desiredCount: 5)) + } + } + } + .padding() + .navigationTitle("Fuel Volume Trend") + } +} diff --git a/Gas Man/Stats/PricePerGallonTrendChartView.swift b/Gas Man/Stats/PricePerGallonTrendChartView.swift new file mode 100644 index 0000000..cb0155e --- /dev/null +++ b/Gas Man/Stats/PricePerGallonTrendChartView.swift @@ -0,0 +1,58 @@ +// +// PriceTrendChartView.swift +// Gas Man +// +// Created by Kameron Kenny on 3/19/25. +// + + +import SwiftUI +import Charts +import CoreData + +struct PricePerGallonTrendChartView: View { + let fuelLogs: [FuelLog] + + // Compute trend data by sorting logs by date ascending, + // then mapping each log's date and pricePerGalon. + var trendData: [(date: Date, price: Double)] { + let sortedLogs = fuelLogs.sorted { ($0.date ?? Date()) < ($1.date ?? Date()) } + return sortedLogs.compactMap { log in + if let date = log.date { + return (date, log.pricePerGalon) + } else { + return nil + } + } + } + + var body: some View { + VStack { + if trendData.isEmpty { + Text("No price trend data available.") + .foregroundColor(.secondary) + } else { + Chart { + ForEach(trendData, id: \.date) { point in + LineMark( + x: .value("Date", point.date), + y: .value("Price", point.price) + ) + PointMark( + x: .value("Date", point.date), + y: .value("Price", point.price) + ) + } + } + .chartXAxis { + AxisMarks(values: .automatic(desiredCount: 4)) + } + .chartYAxis { + AxisMarks(values: .automatic(desiredCount: 5)) + } + } + } + .padding() + .navigationTitle("Price/Gallon Trend") + } +} diff --git a/Gas Man/Stats/StatsView.swift b/Gas Man/Stats/StatsView.swift index 9453758..ab43ba6 100644 --- a/Gas Man/Stats/StatsView.swift +++ b/Gas Man/Stats/StatsView.swift @@ -172,6 +172,14 @@ struct StatsView: View { Text("N/A") } } + NavigationLink(destination: PricePerGallonTrendChartView(fuelLogs: filteredFuelLogs)) { + HStack { + Text("Trends:") + Spacer() + Image(systemName: "chart.bar.fill") + .foregroundColor(.blue) + } + } } // Distance Between Fill-Ups Stats @@ -204,6 +212,14 @@ struct StatsView: View { Text("N/A") } } + NavigationLink(destination: DistanceBetweenFillupsTrendChartView(fuelLogs: filteredFuelLogs)) { + HStack { + Text("Trends:") + Spacer() + Image(systemName: "chart.bar.fill") + .foregroundColor(.blue) + } + } } // Gallons per Fill-Up Stats @@ -236,6 +252,14 @@ struct StatsView: View { Text("N/A") } } + NavigationLink(destination: GalPerFuelUpTrendChartView(fuelLogs: filteredFuelLogs)) { + HStack { + Text("Trends:") + Spacer() + Image(systemName: "chart.bar.fill") + .foregroundColor(.blue) + } + } } } .navigationTitle("Stats")