如何阻止 Swift LineMark Chart 将日期更改为本地时区?

问题描述 投票:0回答:1

我正在尝试在 LineMark 图表上绘制温度数据。我的 chartDATETIME 值数组被格式化为用户选择的坐标处的时区。我知道这些值是正确的,因为我已经打印了它们。

出于某种原因,当我在 LineMark 上绘制温度和日期时间值时,图表将我的日期时间更改为本地时区。我不知道如何阻止图表这样做。

Chart(viewModel.weatherArray, id: \.chartDATETIME) {
    LineMark(
        x: .value("Date", $0.chartDATETIME, unit: .hour),
        y: .value("Temp", $0.chartTMP)
    )
    .lineStyle(StrokeStyle(lineWidth: lineWidth))
    .foregroundStyle(chartColor.gradient)
    .interpolationMethod(interpolationMethod.mode)  
}
ios swift swiftui swiftcharts
1个回答
0
投票

如评论所述,Swift Date 类型基本上绑定到 UTC。 LineMark 图表还需要日期类型,因为它们会自动将日期值转换为图表标签的本地时区。

解决方法是使用 .chartXAxis 修饰符更改图表上的标签。

var body: some View {
    chartTempVar
        .chartXAxis {
            AxisMarks(format: Date.FormatStyle(
                date: .omitted,
                time: .shortened,
                timeZone: TimeZone(secondsFromGMT: chartUTCOffest)!))
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.