Post

Replies

Boosts

Views

Activity

Reply to Charts: customising chartYAxis values
This is absolutely the wrong way to do it - It relies on debug descriptions - but until we find another solution, this will work: .chartYAxis {       AxisMarks() { value in                     AxisGridLine()                     AxisTick()                     AxisValueLabel {                         Text("\(abbreviateAxisValue(string: self.parseAxisValue(value: value) ?? ""))")            }      } } func parseAxisValue(value: AxisValue) -> String? {         let input = String(describing: value)         let regex = /\((\d*.0)|\((0)|\((-\d*.0)/         if let match = input.firstMatch(of: regex) {             return "\(match.1 ?? match.2 ?? match.3 ?? "")"         }         return nil     } func abbreviateAxisValue(string: String) -> String {         let decimal = Decimal(string: string)         if decimal == nil {             return string         } else {             if abs(decimal!) > 1000000000000.0 {                 return "\(decimal! / 1000000000000.0)t"             } else if abs(decimal!) > 1000000000.0 {                 return "\(decimal! / 1000000000.0)b"             } else if abs(decimal!) > 1000000.0 {                 return "\(decimal! / 1000000.0)m"             } else if abs(decimal!) > 1000.0 {                 return "\(decimal! / 1000.0)k"             } else {                 return "\(decimal!)"             }         } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22