KLineChart
KLineChart copied to clipboard
[Feature] applyMoreData forward
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)
}