TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Custom chartXAxis Label not displaying

Forums > SwiftUI

@pf  

I am trying to display custom XAxis labels. If I leave this line without passing the values it shows correctly

AxisMarks() { value in

but when I update to this to the following it doesn't display anything on the xAxis, although model.xAxisLabels is correct, and even if I replace with another array it also doesn't display.

AxisMarks(values: model.xAxisLabels) { value in
Chart {
   ForEach(model.items) { item in
      BarMark(
         x: .value("Time", item.period.identifier),
         y: .value("Distance", item.distance)
      )
   }
 }  
 .chartXAxis {
    AxisMarks(values: model.xAxisLabels) { value in
        AxisValueLabel {
          if let stringValue = value.as(String.self) {
             Text(stringValue)
          }
        }
     }
 }

If I print stringValue I see the following:

AxisValue(_value: Charts.AnyPrimitivePlottable.category("J"), _index: 0, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("F"), _index: 1, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("M"), _index: 2, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("A"), _index: 3, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("M"), _index: 4, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("J"), _index: 5, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("J"), _index: 6, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("A"), _index: 7, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("S"), _index: 8, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("O"), _index: 9, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("N"), _index: 10, _count: 12)
AxisValue(_value: Charts.AnyPrimitivePlottable.category("D"), _index: 11, _count: 12)

Can anyone see what i am doing incorrectly?

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.