swiftbar-chartjawbone

Cannot invoke initializer for type CGFloat with an argument list of type 'String'


I am making a bar chart from JBChart. I would like to change the chartData to String. please help me do it.

here's the chartData code: var chartData = [1, 2, 3]

Then I want to change it to a String from a UILabel from other view controller.

var chartData = [score1, score2, score3]

But I always get this error (Cannot invoke initializer for type CGFloat with an argument list of type 'String')in this code:

    public func barChartView(_ barChartView: JBBarChartView!, heightForBarViewAt index: UInt) -> CGFloat {
        return CGFloat(chartData[Int(index)])
    }

Solution

  • The error is clearly saying that CGFloat doesn't have initializer that accept String as argument. You can use wrapped around like first convert String to Float and then convert Float to CGFloat.

    public func barChartView(_ barChartView: JBBarChartView!, heightForBarViewAt index: UInt) -> CGFloat {
    
        if let value = Float(chartData[Int(index)]) {
            return CGFloat(value)
        }
        return 0
    }
    

    Note: Be sure that this String have number as value otherwise it will return 0 for height.