66 lines
2.0 KiB
Swift
66 lines
2.0 KiB
Swift
//
|
|
// 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)
|
|
)
|
|
.annotation(position: .top) {
|
|
// This annotation displays the data point number and the MPG value.
|
|
Text("\(point.gal, specifier: "%.1f")")
|
|
.font(.caption2)
|
|
.foregroundColor(.blue)
|
|
}
|
|
}
|
|
}
|
|
.chartXAxis {
|
|
AxisMarks(values: .automatic(desiredCount: 4))
|
|
}
|
|
.chartYAxis {
|
|
AxisMarks(values: .automatic(desiredCount: 5))
|
|
}
|
|
|
|
}
|
|
}
|
|
.padding()
|
|
.navigationTitle("Fuel Volume Trend")
|
|
}
|
|
}
|