ios – Swift ui Chart Problem: Date Sort on Y-Axis Exhibiting Smaller Time on High and Bigger Time on Backside in BarMark

ios – Swift ui Chart Problem: Date Sort on Y-Axis Exhibiting Smaller Time on High and Bigger Time on Backside in BarMark


There are totaly 2 questions

  1. When utilizing SwiftUI’s Chart with BarMark for plotting knowledge with Date values on the Y-axis, the difficulty arises the place the time displayed on the Y-axis seems to be inverted—i.e., the smaller occasions (earlier hours) are proven on the high, whereas the bigger occasions (later hours) are displayed on the backside.

  2. There’s one other query, I do not know why this code all the time reviews The compiler is unable to kind examine this expression in affordable time ; attempt breaking apart the expression into distinct sub-expressions

The second problem is brought on by extracting this part of the code for show from the unique code.

import SwiftUI
import Charts

// Helper operate to extract the hour and minute from a Date
func extractHourMinuteFromDate(_ date: Date) -> Date {
    let calendar = Calendar.present
    let parts = calendar.dateComponents([.hour, .minute], from: date)
    return calendar.date(from: parts) ?? date
}



// Helper operate to transform a time string to TimeInterval (if wanted)
func timeStringToTimeInterval(_ timeString: String) -> TimeInterval? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm:ss"
    if let date = dateFormatter.date(from: timeString) {
        return date.timeIntervalSinceReferenceDate
    }
    return nil
}

struct HomeHistogramView: View {
    var viewData: HistogramViewData
    @State personal var selectedWeek: Int = 0
    
    var physique: some View {

        let weeklyData=viewData.getWeeklyData()
        TabView(choice: $selectedWeek) {
            ForEach(0.. Date {
    let calendar = Calendar.present
    var parts = DateComponents()
    parts.hour = hour
    parts.minute = minute
    return calendar.date(from: parts) ?? Date()
}

struct myData: Identifiable, Codable {
    var id = UUID()
    var date: String
    var detailDate: String
    var begin: Date
    var finish: Date
}

struct HistogramViewData: Identifiable, Codable {
    var id = UUID()
    var time: String
    var isArtificial: Bool
    var dataArray: [myData] = []
    
    // Technique to get weekly knowledge
    func getWeeklyData() -> [[myData]] {
        var twoDimensionalArray: [[myData]] = []
        var subarray: [myData] = []
        
        for ingredient in self.dataArray {
            subarray.append(ingredient)
            if subarray.depend == 7 {
                twoDimensionalArray.append(subarray)
                subarray = []
            }
        }
        
        if !subarray.isEmpty {
            twoDimensionalArray.append(subarray)
        }
        
        return twoDimensionalArray
    }
}

struct MainChartsView_Previews: PreviewProvider {
    static var previews: some View {
        let sampleSleepData: [myData] = [
            myData(date: "9", detailDate: "9", start: extractHourMinuteFromDate(dateFrom(hour: 6, minute: 0)), end: extractHourMinuteFromDate(dateFrom(hour: 22, minute: 0))),
            myData(date: "10", detailDate: "9", start: extractHourMinuteFromDate(dateFrom(hour: 6, minute: 0)), end: extractHourMinuteFromDate(dateFrom(hour: 23, minute: 0))),
            myData(date: "11", detailDate: "9", start: extractHourMinuteFromDate(dateFrom(hour: 6, minute: 30)), end: extractHourMinuteFromDate(dateFrom(hour: 21, minute: 30))),
            myData(date: "12", detailDate: "9", start: extractHourMinuteFromDate(dateFrom(hour: 0, minute: 0)), end: extractHourMinuteFromDate(dateFrom(hour: 8, minute: 0))),
            myData(date: "13", detailDate: "9", start: extractHourMinuteFromDate(dateFrom(hour: 6, minute: 30)), end: extractHourMinuteFromDate(dateFrom(hour: 7, minute: 30)))
        ]
        
        let viewData = HistogramViewData(
            time: "8:42 ",
            isArtificial: true,
            dataArray: sampleSleepData
        )
        
        HomeHistogramView(viewData: viewData)
    }
}

enter picture description right here

I’ve explored totally different approaches however none of them have efficiently addressed the issue. I want an answer that ensures the time values are displayed accurately, with earlier occasions on the backside and later occasions on the high, as anticipated.

It will be enormously appreciated if somebody may clarify this.

Leave a Reply

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