IOS Charts crashes when data source has only 1 value

179 Views Asked by At

I am not sure how IOS Charts should behave when there is only 1 value to display, but for me it crashes all the time with the error: fatal error: Index out of range

Here is where the problem is happening:

func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let period = getXValues()
    return period[Int(value)]
}

value comes as -1.

Did someone had this issue before ?

Or is there a way to display data only when there is minimum 2 values ?

1

There are 1 best solutions below

0
On
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            
            let index = Int(value)
            if (index < labels.count && index >= 0 ) {
            return labels[Int(value)]
            }
            return ""
        }