ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.52k stars 5.99k forks source link

Barchart Animation type easeInBack crashes #3340

Open PrashantKT opened 6 years ago

PrashantKT commented 6 years ago

I have extension extension

BarChartView {

    private class BarChartFormatter: NSObject,IAxisValueFormatter {

        var values : [String]
        required init (values : [String]) {
            self.values = values
            super.init()
        }

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

    func setChartValues (xAxisValues : [String] , values : [Double],label : String) {

        var barChartDataEntries = [BarChartDataEntry]()

        for i in 0..<values.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
            barChartDataEntries.append(dataEntry)
        }
        let chartDataSet = BarChartDataSet(values: barChartDataEntries, label: label)
        chartDataSet.colors = ChartColorTemplates.colorful()
        let chartData = BarChartData(dataSet: chartDataSet)

        let formatter = BarChartFormatter(values: xAxisValues)
        let xAxis = XAxis()
        xAxis.valueFormatter = formatter
        self.xAxis.valueFormatter = xAxis.valueFormatter
        self.xAxis.labelPosition = .bottom

        self.data = chartData
       // self.data?.notifyDataChanged()
       // self.notifyDataSetChanged()

        self.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBack) // Cause  crash

    }

}

And app crashes here

for j in 0 ..< Int(ceil(Double(dataSet.entryCount) * animator.phaseX)) // Crash here { guard let e = dataSet.entryForIndex(j) as? BarChartDataEntry else { continue }

Fatal error: Can't form Range with upperBound < lowerBound

(lldb) po dataSet.entryCount 12

(lldb) po animator.phaseX -0.085005447941268492

Please solve this issue

thierryH91200 commented 6 years ago

I think it is possible that in your project there is a big function that takes time the time is over

la solution

        DispatchQueue.main.async(execute: {() -> Void in
       self.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBack) 
        })
ghost commented 4 years ago

I think so, for j in 0 ..< Int(ceil(Double(dataSet.entryCount) * animator.phaseX)) change for
for j in stride(from: 0, to: Int(ceil(Double(dataSet.entryCount) * animator.phaseX)), by: 1)

albertopeam commented 4 years ago

@PrashantKT I have the same issue and the snippet that @txoan have posted is working, but it would be great if anyone can check if the solution doesn't introduce new bugs or any other problem. @jjatie @liuxuan30