How do you skip 0 values on x axis in a SwiftUI Chart

I am plotting a SwiftUI chart from a Struct that looks like this:

struct BestWeights: Identifiable { let id = UUID() let date: String let maxWeight: Int

} I am then creating an array of this Struct that I will use to plot the Chart:

private var bestWeights: [BestWeights] {

 let unformattedMonth = DateFormatter().monthSymbols[month - 1]
                let formattedMonth =  String(unformattedMonth.prefix(3))
bestWeights.append(BestWeights(date: formattedMonth, maxWeight: bestWeightOfPeriod))
            //decrementing down one month
           selectedDate = Calendar.current.date(byAdding: .month, value: -1, to: selectedDate) ?? selectedDate

Then I am iterating through bestWeights and plotting them:

                Chart {
                    ForEach(bestWeights) { bestWeight in
                    
                        LineMark(x: .value("Date",bestWeight.date), y: .value("MaxWeight", bestWeight.maxWeight))
                            .symbol {
                                Circle()
                                    .fill(.blue)
                                    .frame(width: 7, height: 7)
                            }
                            

                    }
                }
  • The problem is that this produces 0 values on the Y axis that scrape the bottom of the LineMark, now I can fix this by not adding all of the bestWeights who's weight is 0 but then I don't get the full x axis I want which is 6 full months, it would show only the number of months as we have records and would jump from February to July etc.. is there any way to remove the 0 weights while keeping the X axis full of dates

@milanlabus

When you filter out the 0 value, you can specify a chartXScale.

from the start of the year to the end of year.

chartXScale(domain: startOfYear...endOfYear)

    private var startOfYear: Date {
        let calendar = Calendar.autoupdatingCurrent
        return calendar.date(from: DateComponents(year: 2020, month: 1, day: 1))!
    }
    
    private var endOfYear: Date {
        let calendar = Calendar.autoupdatingCurrent
        return calendar.date(from: DateComponents(year: 2020, month: 12, day: 31))!
    }


How do you skip 0 values on x axis in a SwiftUI Chart
 
 
Q