简体   繁体   English

为什么一些 BarMark 数据点的注释在 swiftui 图表中有两个值?

[英]why annotations for some BarMark datapoints have two values in swiftui charts?

I have a chart showing a 10 day forecast using weatherkit, I want to show min and max temp per day so I use a BarMark with two values in the x axis.我有一个使用 weatherkit 显示 10 天预报的图表,我想显示每天的最低和最高温度,所以我使用 BarMark 在 x 轴上有两个值。 I want to show annotation for the min and one for the max.我想显示最小值的注释和最大值的注释。 but somehow some of them appear double with different values.但不知何故,其中一些看起来是双重的,具有不同的价值。

在此处输入图像描述 is this an Apple issue or my code issue?这是苹果问题还是我的代码问题?

struct TenDayForecastViewChart: View {
    let dayWeatherList: [DayWeather]
    
    var body: some View {
        VStack(alignment: .leading) {
            Text("10-DAY FORECAST")
                .font(.caption)
                .opacity(0.5)
                .padding()
            
            Chart{
                ForEach(dayWeatherList, id: \.date) { dailyWeather in
                    BarMark(xStart: .value("Temperature", dailyWeather.lowTemperature.converted(to: .fahrenheit).value),
                            xEnd: .value("Temperature", dailyWeather.highTemperature.converted(to: .fahrenheit).value),
                            y: .value("Day", dailyWeather.date.formatAsAbbreviatedDay())
                    )
                    .foregroundStyle(Color.black)
                    .annotation(position: .overlay, alignment: .leading) {
                        HStack {
                            
                            //Image(systemName: "\(dailyWeather.symbolName)").foregroundColor(.white)
                            
                            Text("\(dailyWeather.lowTemperature.converted(to: .fahrenheit).value, format: .number.precision(.fractionLength(0)))")
                                .foregroundColor(.white)
                        }
                    }
                    .annotation(position: .overlay, alignment: .trailing) {
                                                    Text("\(dailyWeather.highTemperature.converted(to: .fahrenheit).value, format: .number.precision(.fractionLength(0)))")
                                                        .foregroundColor(.blue)
                    }
                    

                    
                
                }

            }
            //.chartLegend(position: .top, alignment: .bottomTrailing)
            .chartXAxis(.hidden)
            //.chartYAxis(.hidden)
            .frame(height: 250)
            .padding(.horizontal)
        }
        
        
    }
}

I suppose in your 10 days you have 2x "Sun", "Mon", "Tue" each.我想在你的 10 天里你有 2 个“星期日”、“星期一”、“星期二”。 So the y plot value for these data points is identical, and by default BarCharts adds those together.因此,这些数据点的 y plot 值是相同的,默认情况下BarCharts将它们相加。

  1. You can pass the full Date (not only the day String) into the BarChart and use您可以将完整的Date (不仅是日期字符串)传递到 BarChart 中并使用
.value("Day", dailyWeather.date, unit: .day)

and add a custom formatting in .chartYAxis :并在.chartYAxis中添加自定义格式:

.chartYAxis {
    AxisMarks(values: .stride(by: .day)) { _ in
        AxisGridLine()
        AxisTick()
        AxisValueLabel(format: .dateTime.weekday(.abbreviated), centered: true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM