iOS Development

ios – Swift Chart Axis ordering problem

Spread the love


that tracks somebody’s sleep into the 4 classes awake, rem, core and deep. I’ve the graph nearly how I need it, displaying the sleep knowledge at varied instances of the evening (every hour), nevertheless I’m misplaced on how I’m meant to get my Y axis displaying in the correct order. I need it break up into 4, for every of the sleep phases, beginning high awake, beneath REM, beneath core, on the backside deep, 25% of the peak of the graph. Nevertheless, it doesn’t matter what I attempt, it maintains the order core, awake, deep, REM. I’ve tried utilizing the .chartYAxis, nevertheless it didnt appear to do something. I can see within the documentation the best way to modify this for numbers, or dates, however for what I need, I’m fairly misplaced, particularly as I get no errors, it simply fails silently to organize them. I considered one way or the other utilizing an Integer to signify every of the sleep sorts, 0 = awake, 4 = deep, and one way or the other correspond these to a String worth, however I’m not certain the best way to obtain that. Thanks for the assistance!

enum SleepType: String, Plottable, CaseIterable {
    case Awake
    case REM
    case Core
    case Deep
}

struct SingleSleep: Hashable, Identifiable {
    let id = UUID()
    let startDate: Date
    let stopDate: Date
    var sleepTime: TimeInterval = 0 //in minutes
    var sleepType: SleepType
}

struct SleepExampleView: View {
    
    var sleep = [
        SingleSleep(startDate: Date(timeIntervalSinceReferenceDate: 729208384), stopDate: Date(timeIntervalSinceReferenceDate: 729209374), sleepTime: 16.5, sleepType: .Core),
        SingleSleep(startDate: Date(timeIntervalSinceReferenceDate: 729209374), stopDate: Date(timeIntervalSinceReferenceDate: 729209404), sleepTime: 16.5, sleepType: .Awake),
        SingleSleep(startDate: Date(timeIntervalSinceReferenceDate: 729209404), stopDate: Date(timeIntervalSinceReferenceDate: 729209794), sleepTime: 16.5, sleepType: .Core),
        SingleSleep(startDate: Date(timeIntervalSinceReferenceDate: 729209794), stopDate: Date(timeIntervalSinceReferenceDate: 729211234), sleepTime: 16.5, sleepType: .Deep),
        SingleSleep(startDate: Date(timeIntervalSinceReferenceDate: 729211234), stopDate: Date(timeIntervalSinceReferenceDate: 729211864), sleepTime: 16.5, sleepType: .REM),
    ]
    
    var physique: some View {
        //chart
        
        Chart {
            ForEach(sleep, id: .self) { sleep in
                LineMark(
                    x: .worth("Time", sleep.startDate),
                    y: .worth("Kind", sleep.sleepType.rawValue)
                )
            }
        }
        .chartYAxis {
            AxisMarks(
                values: [SleepType.Awake.rawValue, SleepType.REM.rawValue, SleepType.Core.rawValue, SleepType.Deep.rawValue]
              )
        }
        .chartXAxis {
            AxisMarks(values: .stride(by: .hour, rely: 1)) { worth in
                AxisValueLabel(format: .dateTime.hour(), centered: true)
            }
        }
        .body(width: 300, top: 300)
        .preferredColorScheme(.darkish)
        .background(.clear)
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *