Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ios - Facing error in swift UI “Invalid frame dimension (negative or non-finite)” while drawing negative value in chart

In widget iOS14, I want to show values in graphical form using bar chart.

I have used this library "https://github.com/dawigr/BarChart" to draw bar chart.

But in Xcode12+, it's not showing negative values and considering negative value as 0 and throwing warning as shown in screen shot.

"[SwiftUI] Invalid frame dimension (negative or non-finite)"

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could try to normalise your input Values to prevent getting errors like this.
e.g.: if your data set contains values from -10 to 100, your min normalised value would be 0 and your max normalised value 1. This only works if your numbers are CGFloat, Double or something like this, numbers in Int format would be rounded up.
This could be done by using an extension like this:


extension Array where Element == Double {

    var noramlized: [Double] {
       
     
        if let min = self.min(), let max = self.max() {
            
            return self.map{ ($0 - min) / (max - min) }
            
            
        }
        
        return []
     
    }
    
 
 
}

I don't no how you get your values for the frame exactly, but I think you did something like this:

// normalise your data set: 

let data : [Double] = [Double]() 

youChart(inputData: data.noramlized)


// get values for the frame 

let height = data.noramlized.max()

// if your normalised value is too small for your purpose (your max will always be 1.0 but in relation to the rest it might fit), you can scale it up like height * 20.

// the width might be a non changing value that you will enter manually or it will append on the count of bars in your chart.



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...