klinecharts / KLineChart

📈Lightweight k-line chart that can be highly customized. Zero dependencies. Support mobile.(可高度自定义的轻量级k线图,无第三方依赖,支持移动端)

Home Page:https://klinecharts.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature] applyMoreData forward

oneart-dev opened this issue · comments

Feature Description

Currently if we updating data using updateData() function it will force scroll to the last bar as it calls adjustPaneViewport inside.
For our use case it will lead for infinity loop, because we are loading more data not only backward but forward as well (showing historical data).

There is no good solution for this task based on current API. But it can be easily fixed by a few lines of code.

To Do

Rewrite ChartStore.addData. So we can concat data not only before but data happened after.

addData (data: KLineData | KLineData[], pos: number, more?: boolean): void {
    if (isArray<KLineData>(data)) {
      this._timeScaleStore.setLoading(false)
      this._timeScaleStore.setMore(more ?? true)
      const isFirstAdd = this._dataList.length === 0
      this._dataList = data.concat(this._dataList) // <<<<<<< this have to be changed

One of smart solutions:

addData (data: KLineData | KLineData[], pos: number, more?: boolean): void {
    if (isArray<KLineData>(data)) {
      this._timeScaleStore.setLoading(false)
      this._timeScaleStore.setMore(more ?? true)
      const isFirstAdd = this._dataList.length === 0

      // change
      if (this._dataList[this._dataList.length - 1].timestamp < data[0].timestamp) {
          this._dataList = this._dataList.concat(data)
      } else {
          this._dataList = data.concat(this._dataList)
      }

      if (isFirstAdd) {
        this._timeScaleStore.resetOffsetRightDistance()
      }
      this._timeScaleStore.adjustVisibleRange()
    } else {
      const dataSize = this._dataList.length
      if (pos >= dataSize) {
        this._dataList.push(data)
        let offsetRightBarCount = this._timeScaleStore.getOffsetRightBarCount()
        if (offsetRightBarCount < 0) {
          this._timeScaleStore.setOffsetRightBarCount(--offsetRightBarCount)
        }
        this._timeScaleStore.adjustVisibleRange()
      } else {
        this._dataList[pos] = data
        this.adjustVisibleDataList()
      }
    }
    this._crosshairStore.recalculate(true)
  }