ChartsOrg / Charts

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

Scrolling animation not working #4659

Open gabriele-dechit opened 3 years ago

gabriele-dechit commented 3 years ago

Hello,

animations don't occur when scrolling a line chart on an iOS app. Using instead MPAndroidChart library on an Android app I encounter no issues.

What did you do?

1) Create subclass of LineChartView with a new notifyDataSetChangedNew method according to https://www.programmersought.com/article/33351889267/

`class CustomLineChartView: LineChartView {

func notifyDataSetChangedNew() {

    guard let data = data else { return }

    renderer?.initBuffers()

    calcMinMax()

    self.xAxis.axisRange = Double(xAxisWidth)

    leftYAxisRenderer.computeAxis(min: leftAxis.axisMinimum, max: leftAxis.axisMaximum, inverted: leftAxis.isInverted)
    rightYAxisRenderer.computeAxis(min: rightAxis.axisMinimum, max: rightAxis.axisMaximum, inverted: rightAxis.isInverted)
    xAxisRenderer.computeAxis(min: xAxis.axisMinimum, max: xAxis.axisMaximum, inverted: false)

    legendRenderer.computeLegend(data: data)

    calculateOffsets()

    setNeedsDisplay()
}

}`

2) Setup the cart view

`private func makeChartView() -> CustomLineChartView {

    let dataSet = LineChartDataSet(entries: [])
    dataSet.colors = [.systemBlue]
    dataSet.drawCirclesEnabled = false
    dataSet.drawValuesEnabled = false
    dataSet.lineWidth = 1

    let view = CustomLineChartView()
    view.chartDescription.enabled = false
    view.drawMarkers = false
    view.legend.enabled = false

    view.isUserInteractionEnabled = true
    view.dragEnabled = true
    view.dragXEnabled = true
    view.dragYEnabled = true
    view.setScaleEnabled(true)
    view.pinchZoomEnabled = true

    view.leftAxis.axisMaximum = yMaximumValue
    view.leftAxis.axisMinimum = yMinimumValue
    view.xAxis.axisMinimum = 0
    view.xAxis.axisMaximum = xAxisWidth
    view.rightAxis.drawLabelsEnabled = false
    view.rightAxis.drawGridLinesEnabled = false
    view.rightAxis.drawAxisLineEnabled = false

    let data = LineChartData(dataSet: dataSet)
    data.setValueTextColor(.systemBlue)
    view.data = data

    return view
}`

3) Add new values to chart at regular interval expecting it to automatically and smoothly scroll to show them

`private func fillChart(with entries: [Double]) {

    guard let data = self.chartView.data,
          let dataSet = data.dataSet(at: 0) else { return }

    var count = dataSet.entryCount - 1
    entries.forEach { entry in

        count += 1
        let x = Double(count) / Double(frequency)

        data.appendEntry(ChartDataEntry(x: x, y: entry), toDataSet: .zero)
    }

    data.notifyDataChanged()
    if dataSet.entryCount <= Int(xAxisWidth) * frequency {

        self.chartView.notifyDataSetChangedNew()

    } else {

        self.chartView.notifyDataSetChanged()
    }

    self.chartView.setVisibleXRangeMaximum(xAxisWidth * Double(frequency))

    if dataSet.entryCount > Int(xAxisWidth) * frequency {

        let max = Double(dataSet.entryCount / frequency)
        let min = Double(dataSet.entryCount / frequency) - xAxisWidth

        self.chartView.moveViewToX(Double(data.entryCount))
        self.chartView.xAxis.axisMaximum = max
        self.chartView.xAxis.axisMinimum = min

    } else {

        self.chartView.moveViewToX(0)
    }
}`

4) I tried using the moveViewToAnimated(...) method but the result is the same

What did you expect to happen?

I expected the chart to smoothly scroll like in the attached "android.mp4" video

What happened instead?

The chart doesn't scroll smoothly, it replaces values immediately without any animation like in the attached "ios.mp4" video

Charts Environment

Charts version/Branch/Commit Number: 4.0.1/master/HEAD Xcode version: 12.5 Swift version: 5 Platform(s) running Charts: iOS 14.5 macOS version running Xcode: 11.4

Demo Project

I have attached a sample project archive.zip

Gman9855 commented 3 years ago

I noticed this is happening for me too when i set the xAxis.axisMinimum; removing that line makes it scroll smoothly again with moveViewToAnimated()