ChartsOrg / Charts

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

How to draw incoming data gradually? #5057

Open SparklingAir opened 1 year ago

SparklingAir commented 1 year ago

Hello guys,

It's just a question. Sorry if this is a repetition. The problem is I can't manage to draw incoming data step by step. I can't find such a flag. Now entries are coming gradually - not all at once. But sdk renders them across the full width of the chart view straight away despite the count of them. I need them to draw dot by dot from left to right and only after reaching right edge to start scaling. It seems that there is such bool flag, but I didn't manage to fine it. I would appreciate any help. Below the code.

import UIKit
import Charts
import RxSwift
import RxCocoa

struct ChartViewModel {
    let blueValue: Driver<ChartDataEntry?>
}

final class ChartView: UIView {
    private lazy var blueSet = makeEmptySet(color: .blue)
    private lazy var blueLineCharView = makeChartView()
    private let bag = DisposeBag()

    init(model: ChartViewModel) {
        super.init(frame: .zero)
        setup()
        setupConstraints()
        bind(model)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setup() {
        backgroundColor = .clear
        addSubview(blueLineCharView)
    }

    private func setupConstraints() {
        blueLineCharView.make {
            $0.edges.equalToSuperview()
        }
    }

    private func bind(_ model: ChartViewModel) {
        model.blueValue.drive(onNext: { [weak self] value in
            guard let self = self, let value = value else { return }
            self.blueSet.append(value)
            self.blueLineCharView.data = LineChartData(dataSet: self.blueSet)
        })
        .disposed(by: bag)
    }

    private func makeChartView() -> LineChartView {
        let chartView = LineChartView()
        chartView.backgroundColor = .clear
        chartView.leftAxis.enabled = false
        chartView.rightAxis.enabled = false
        chartView.xAxis.enabled = false
        chartView.isUserInteractionEnabled = false
        chartView.legend.enabled = false
        chartView.dragEnabled = false
        return chartView
    }

    private func makeEmptySet(color: UIColor) -> LineChartDataSet {
        let set = LineChartDataSet(entries: [])
        set.drawCirclesEnabled = false
        set.drawValuesEnabled = false
        set.mode = .cubicBezier
        set.lineWidth = 2
        set.setColor(color)
        set.drawVerticalHighlightIndicatorEnabled = false
        set.drawHorizontalHighlightIndicatorEnabled = false
        return set
    }
}