The difficulty with displaying PointMarks in SwiftUI Chart is that once I use PointMark to show a number of level icons, and the Y-axis represents Date sort information, the icons are usually not totally seen. The topmost level icon is lower off, with half of it not being displayed.
To resolve this subject, I attempted including a variety to the Y-axis by calculating the minimal and most values within the code utilizing .chartYAxisRange(min: adjustedMinValue, max: adjustedMaxValue), however it retains throwing an error: “The compiler is unable to type-check this expression in cheap time; strive breaking apart the expression into distinct sub-expressions, I commented out that part of the code.
If anybody may assist check out this subject, I’d tremendously respect it.
import Basis
import SwiftUI
import Charts
func convertFullDateToHMDate(_ fullDateString: String) -> Date {
let fullFormatter = DateFormatter()
fullFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let fullDate = fullFormatter.date(from: fullDateString)!
let calendar = Calendar.present
let elements = calendar.dateComponents([.hour, .minute ,.second], from: fullDate)
return calendar.date(from: elements)!
}
func formatDateToHMS(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
return formatter.string(from: date)
}
struct TemperatureData: Identifiable ,Codable {
var id = UUID()
var day: String
var detailDay: String // element time
var temperature: Double = 0.0
}
struct PointChartView: View {
var lineChartViewData: LineChartViewData
@State personal var selectedWeek: Int = 0
var physique: some View {
VStack(alignment: .main, spacing: 16) {
// let (minDate, maxDate) = lineChartViewData.getMinMaxDate()
TabView(choice: $selectedWeek) {
ForEach(0.. [[TemperatureData]] {
var twoDimensionalArray: [[TemperatureData]] = []
var subarray: [TemperatureData] = []
for aspect in self.dataArray {
subarray.append(aspect)
if subarray.depend == 7 {
twoDimensionalArray.append(subarray)
subarray = []
}
}
if(!subarray.isEmpty) {
twoDimensionalArray.append(subarray)
}
return twoDimensionalArray
}
func getMinMaxDate() -> (min: Date, max: Date) {
let values = dataArray.map { convertFullDateToHMDate($0.detailDay) }
let minValue = values.min() ?? Date()
let maxValue = values.max() ?? Date()
let padding: TimeInterval = 60 * 10
let adjustedMinValue = minValue.addingTimeInterval(-padding)
let adjustedMaxValue = maxValue.addingTimeInterval(padding)
return (adjustedMinValue, adjustedMaxValue)
}
}
struct PointChartView_Previews: PreviewProvider {
static var previews: some View {
let temperatureData: [TemperatureData] = [
TemperatureData(day: "9-9", detailDay: "2024-12-01 10:31:00"),
TemperatureData(day: "9-9", detailDay: "2024-12-01 10:32:00"),
TemperatureData(day: "9-9", detailDay: "2024-12-01 10:33:00"),
]
PointChartView(lineChartViewData:LineChartViewData( imageName: "IconPoop" ,isArtificial:false,dataArray: temperatureData))
}
}