You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift Charts X轴最后一个AxisValueLabel无法显示问题求助

解决Swift Charts X轴标签缺失问题

你遇到的问题是因为.automatic(desiredCount:)仅作为框架的参考值,它仍会根据图表布局自动调整标签数量,导致最后一个标签被隐藏。要强制显示所有11个X轴标签,直接指定所有需要展示的X轴值即可,无需依赖自动计算。

修改步骤:

  • 提前计算并存储排序后的数据源,避免重复计算;
  • 在chartXAxis的AxisMarks中,直接传入所有X轴的key值,替代.automatic。

修改后的完整代码:

import Charts
import Foundation
import SwiftUI

struct FakeData: Codable {
    var questionAndAnswers: [Int: Int]
    var timePerQuestion: [Double]
    var date: Date = .now
}

extension FakeData {
    static let oneFakeInstance = FakeData(questionAndAnswers: [1875: 1875,
                                                               1890: 1890,
                                                               1980: 1980,
                                                               2112: 2112,
                                                               2726: 2726,
                                                               4088: 4088,
                                                               4284: 4284,
                                                               4784: 4784,
                                                               4800: 4800,
                                                               663: 663,
                                                               1098: 1098], timePerQuestion: [
            28.700000000000138,
            11.600000000000165,
            12.00000000000017,
            25.599999999999376,
            11.999999999999318,
            19.19999999999891,
            12.799999999999272,
            7.199999999999605,
            11.699999999999335,
            39.299999999997766, 19.299999999997766
        ])
}

struct CH1: View {
    func convertToShowable(_ QuizquestionAnswers: [Int: Int] = FakeData.oneFakeInstance.questionAndAnswers, _ quizTimes: [Double] = FakeData.oneFakeInstance.timePerQuestion) -> [Int: Double] {
        var time_per_question: [Int: Double] = [:]
        for (index, key_value) in QuizquestionAnswers.enumerated() {
            if key_value.value == key_value.key {
                time_per_question[index] = quizTimes[index]
            }
        }
        return time_per_question
    }
    
    var body: some View {
        ZStack {
            Color.black.edgesIgnoringSafeArea(.all)
        
            VStack {
                // 提前计算排序后的数据源,复用避免重复计算
                let sortedData = convertToShowable().sorted(by: { $0.key < $1.key })
                
                Chart {
                    ForEach(sortedData, id: \.key) { key, value in
                        BarMark(x: .value("Question", key),
                                y: .value("Time", value))
                            .foregroundStyle(Color.white)
                    }
                }
                .chartYAxis {
                    AxisMarks(values: .automatic) { _ in
                        AxisValueLabel().foregroundStyle(Color.orange).offset(x: 10).font(.subheadline)
                    }
                }
                .chartXAxis {
                    // 直接传入所有X轴的key值,强制显示每个标签
                    AxisMarks(values: .values(sortedData.map { $0.key })) { _ in
                        AxisGridLine(stroke: .init(lineWidth: 1)).foregroundStyle(Color.orange)
                        AxisValueLabel().foregroundStyle(Color.orange).font(.subheadline).offset(x: -10)
                    }
                }
                .frame(width: 350, height: 250)
            }
        }
    }
}

关键修改点说明:

  • 新增sortedData变量存储排序后的数据源,避免在Chart和AxisMarks中重复计算;
  • 将AxisMarks(values: .automatic(...))替换为AxisMarks(values: .values(sortedData.map { $0.key })),直接指定所有需要显示的X轴值,框架会为每个值生成对应的标签,不会自动省略。

内容的提问来源于stack exchange,提问作者apod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:01:07