'watchOS 8.5: Invalid frame dimension (negative or non-finite)

So I'm trying to draw a bar chart using the code below:

HStack(alignment: .bottom) {
                ForEach(0..<values.count) { idx in
                    let max = values.max() ?? 0
                    
                    VStack {
                        Text(labels[idx])
                            .font(.caption)
                            .rotationEffect(.degrees(-60))
                        
                        RoundedRectangle(cornerRadius: 5)
                            .fill(Color.white)
                            .frame(width: 20, height: CGFloat(values[idx]) / CGFloat(max) * geo.size.height * 0.6)
                        
                        Text(xAxisLabels[idx])
                            .font(.caption)
                    }
                }
            }.frame(maxWidth: .infinity, maxHeight: .infinity)
            .cornerRadius(10)
            .padding(.bottom, 20)
        }

Note line:

.frame(width: 20, height: CGFloat(values[idx]) / CGFloat(max) * geo.size.height * 0.6)

I am dynamically assigning the height to the bar chart from the values from HealthKit (The 'values' variable).

I'm just wondering is there anyway I could fix this issue? I have prior versions where the bar chart can be displayed using static variables and from what I can understand from the error it doesn't like the fact that the potential height is not static.



Solution 1:[1]

Try replacing 0 for 1 here:

let max = values.max() ?? 1

This will prevent a division by 0 when view is refreshing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChrisR