This commit is contained in:
Kameron Kenny 2025-03-19 14:17:31 -04:00
parent db49cf17cd
commit e103168802
4 changed files with 204 additions and 0 deletions

View File

@ -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..<sortedLogs.count {
let previous = sortedLogs[i - 1]
let current = sortedLogs[i]
if let date = current.date,
current.odometer > 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")
}
}

View File

@ -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")
}
}

View File

@ -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")
}
}

View File

@ -172,6 +172,14 @@ struct StatsView: View {
Text("N/A") Text("N/A")
} }
} }
NavigationLink(destination: PricePerGallonTrendChartView(fuelLogs: filteredFuelLogs)) {
HStack {
Text("Trends:")
Spacer()
Image(systemName: "chart.bar.fill")
.foregroundColor(.blue)
}
}
} }
// Distance Between Fill-Ups Stats // Distance Between Fill-Ups Stats
@ -204,6 +212,14 @@ struct StatsView: View {
Text("N/A") Text("N/A")
} }
} }
NavigationLink(destination: DistanceBetweenFillupsTrendChartView(fuelLogs: filteredFuelLogs)) {
HStack {
Text("Trends:")
Spacer()
Image(systemName: "chart.bar.fill")
.foregroundColor(.blue)
}
}
} }
// Gallons per Fill-Up Stats // Gallons per Fill-Up Stats
@ -236,6 +252,14 @@ struct StatsView: View {
Text("N/A") Text("N/A")
} }
} }
NavigationLink(destination: GalPerFuelUpTrendChartView(fuelLogs: filteredFuelLogs)) {
HStack {
Text("Trends:")
Spacer()
Image(systemName: "chart.bar.fill")
.foregroundColor(.blue)
}
}
} }
} }
.navigationTitle("Stats") .navigationTitle("Stats")