Friday, August 2, 2024
HomeiOS Developmentios - Tips on how to skip Zero values when plotting a...

ios – Tips on how to skip Zero values when plotting a SwiftUI Chart


I’m plotting a SwiftUI chart from a Struct that appears like this:

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

}

I’m then creating an array of this Struct that I’ll use to plot the Chart:

 personal 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.present.date(byAdding: .month, worth: -1, to: selectedDate) ?? selectedDate

Then I’m iterating via bestWeights and plotting them:

                    Chart {
                        ForEach(bestWeights) { bestWeight in
                        
                            LineMark(x: .worth("Date",bestWeight.date), y: .worth("MaxWeight", bestWeight.maxWeight))
                                .image {
                                    Circle()
                                        .fill(.blue)
                                        .body(width: 7, peak: 7)
                                }
                                

                        }
                    }

The Results of this appears to be like like this:

Including 0s

This isn’t what I need I do not need the 0’s to be drawn on the Y axis so I then added a test to not even append any bestWeight the place the burden is 0:

              if(bestWeightOfPeriod != 0) {
                    bestWeights.append(BestWeights(date: formattedMonth, maxWeight: bestWeightOfPeriod))
                }
                }

this I precisely what I need however that creates an issue in my x axis the place its skipping months it goes from Feb Straight to June, I nonetheless wish to mark the entire months however because the date is saved in my Struct and its skipping the months which have a 0 weight its not labeling it appropriately how I label these lacking months?

Skipping 0s

I need the Y axis of the second picture however the X axis of the primary Picture



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments