How to scale LineMark to fill all chart automatically?
Here is my sample:
import SwiftUI
import Charts
struct EnergyView: View {
var body: some View {
VStack {
Chart {
ForEach(DailyEnergy.mockData) { dailyEnergy in
LineMark(x: .value("Data", dailyEnergy.date),
y: .value("Value", dailyEnergy.energy))
PointMark(x: .value("Data", dailyEnergy.date),
y: .value("Value", dailyEnergy.energy))
.annotation(alignment: .center,
spacing: 10) {
Text("\(Int(dailyEnergy.energy))")
.minimumScaleFactor(0.5)
.scaledToFit()
.frame(width: 40)
}
}
.interpolationMethod(.monotone)
}
}.frame(height: 100)
}
}
struct EnergyView_Previews: PreviewProvider {
static var previews: some View {
EnergyView()
}
}
struct DailyEnergy: Identifiable {
let id: UUID = .init()
let date: Date
let energy: Double
}
extension DailyEnergy {
static var mockData: [Self] {
[
.init(date: "2023-08-01".formattedDate, energy: 1376),
.init(date: "2023-08-02".formattedDate, energy: 1576),
.init(date: "2023-08-03".formattedDate, energy: 1875),
.init(date: "2023-08-04".formattedDate, energy: 1676),
.init(date: "2023-08-05".formattedDate, energy: 1475),
.init(date: "2023-08-06".formattedDate, energy: 1574),
.init(date: "2023-08-07".formattedDate, energy: 1674)
]
}
}
and here is result what I have:
and here is result what I'm expect:
I want to build LineMark on full chart view, but how I can do this?
I tried to use scalledToFill()
, but it change all chart instead only LineMark, also I tried to use .chartYScale(domain: 1200...2800)
, but in this case I can't be sure that all chart line will be fitted to chart view including my PointMark
. Maybe Charts
has any function to do this?