1

I'm having an issue with SwiftUI charts and was wondering if anyone knows of a solution or has also run into this. I've been trying to plot data for my app and the X axis does not show the dates and the Y axis only has axis marks 0-9. So, I tried to make another file instance to check what's going on. This is what I have


import Charts
import SwiftUI

struct Exercise: Identifiable {
    let id = UUID()
    var dateCompleted: Date
    var reps: Int
}


struct ContentView: View {
    
    private var exerciseMaxRepsArray: [Exercise] = []

    init() { // init dummy data
        for i in 0 ..< 20 {
            let date = Calendar.current.date(byAdding: .day, value: i, to: .now) ?? .now
            let rep = Int.random(in: 1...20)
            exerciseMaxRepsArray.append(
                Exercise(dateCompleted: date, reps: rep)
            )
        }
    }

    var body: some View {
        
        GroupBox(label: Text("Daily Max Reps")) {
            Chart(exerciseMaxRepsArray) { e in
                LineMark(x: .value("Date", e.dateCompleted, unit: .day),
                         y: .value("Reps", e.reps)
                ) }
            .chartYAxisLabel(position: .trailing, alignment: .center) {
                Text("Reps")
            }
            .chartXAxisLabel(position: .bottom, alignment: .center) {
                Text("Date")
                
            }
            }    
    }
    }
}

And this is the result: enter image description here

I've checked and the issue is not the frame. Has anyone else had this issue or have any clue what could be going on here?

Rachel
  • 51
  • 1
  • 7
  • Could not replicate your problems with the code you give. All works well for me, on MacOS 13.2, Xcode 14.2, tested on real ios 16.3 devices (and simulator), and macCatalyst. Could be different on older systems, or maybe there is something else in your code that you are not showing us. – workingdog support Ukraine Jan 12 '23 at 02:09
  • thank you for trying. I have the same settings. I tried running it on my phone instead of the simulator and it worked as expected. Not sure why but I'll go with it! – Rachel Jan 12 '23 at 02:45

0 Answers0