简体   繁体   中英

How to customize the y-axis values in Swift Charts?

I have a simple graph (made using Swift Charts framework ) where I am showing the percentage of something over time. The x-axis is the time here and y-axis is the percentage. Ergo, I think if the values in y-axis would be like 25%, 50%, etc., it would make more sense to the user.

How can I customize the y-axis values and add a symbol (in this case a % ) at the end?

This is the code so far:

Chart(data, id: \.date) { datum in
    LineMark(
        x: .value("Date", datum.date, unit: .day),
        y: .value("Done", datum.doneCount * 100 / (datum.doneCount + datum.notDoneCount))
    )
    .interpolationMethod(.catmullRom)
    .foregroundStyle(Color.orange)
}
.chartXAxis {
    AxisMarks(values: .stride(by: .day))
}

在此处输入图像描述

Just add this code block to your Chart

.chartYAxis {
   AxisMarks() {
       let value = $0.as(Int.self)!
       AxisValueLabel {
           Text("\(value)%")
       }
   }
}

Your Chart will then show 0%, 25%, ... as labels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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