-2

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

Shipra Gupta
  • 171
  • 1
  • 2
  • 10

1 Answers1

0

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.


DoTryCatch
  • 1,052
  • 6
  • 17
  • But I have to show negative values below x-axis pf chart as per my graph requirement – Shipra Gupta Jan 12 '21 at 13:27
  • you could grab from the unnormalised data the max and min value for the axis label. If you cloud post the code snippet where you embed the graph and enter the data, I could show you probably a way. – DoTryCatch Jan 12 '21 at 13:54