import SwiftUI
import Charts

struct ElevationSample: Identifiable {

    let timestamp: Date

    let altitude: Double

    var id: Date {
        timestamp
    }
}

struct ElevationGraph: View {

    let samples: [ElevationSample]

    var body: some View {
        Chart {
            // Active segments as area + line
            ForEach(samples) { sample in
                LineMark(
                    x: .value("Time", sample.timestamp),
                    y: .value("Altitude", sample.altitude)
                )
                .foregroundStyle(by: .value("Series", "Altitude"))
                //.interpolationMethod(.catmullRom)

                AreaMark(
                    x: .value("Time", sample.timestamp),
                    y: .value("Altitude", sample.altitude)
                )
                //.interpolationMethod(.catmullRom)
                .foregroundStyle(LinearGradient(
                    gradient: Gradient(colors: [.blue.opacity(0.8), .blue.opacity(0.2)]),
                    startPoint: .top,
                    endPoint: .bottom
                ))
            }
        }
        .chartYAxis {
            AxisMarks(position: .leading)
        }
        .chartXScale(domain: samples.first!.timestamp...samples.last!.timestamp)
        .frame(width: 700, height: 220)
        .padding()
    }
}
