From debdab67da63f1af62ceffafb940973dfe10e453 Mon Sep 17 00:00:00 2001 From: liihuu Date: Mon, 18 Mar 2024 00:20:21 +0800 Subject: [PATCH] opt: opt add data --- src/Chart.ts | 7 +++-- src/common/LoadDataCallback.ts | 1 + src/store/ChartStore.ts | 54 +++++++++++++++++++--------------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/Chart.ts b/src/Chart.ts index 5ad0b5bc1..0dcacdc10 100644 --- a/src/Chart.ts +++ b/src/Chart.ts @@ -33,6 +33,7 @@ import { getPixelRatio } from './common/utils/canvas' import { isString, isArray, isValid, merge, isNumber } from './common/utils/typeChecks' import { logWarn } from './common/utils/logger' import { binarySearchNearest } from './common/utils/number' +import { LoadDataType } from './common/LoadDataCallback' import ChartStore from './store/ChartStore' @@ -675,7 +676,7 @@ export default class ChartImp implements Chart { if (isValid(callback)) { logWarn('applyNewData', '', 'param `callback` has been deprecated since version 9.8.0, use `subscribeAction(\'onDataReady\')` instead.') } - this._chartStore.addData(data, true, more).then(() => {}).catch(() => {}).finally(() => { callback?.() }) + this._chartStore.addData(data, LoadDataType.Init, more).then(() => {}).catch(() => {}).finally(() => { callback?.() }) } /** @@ -685,14 +686,14 @@ export default class ChartImp implements Chart { applyMoreData (data: KLineData[], more?: boolean, callback?: () => void): void { logWarn('', '', 'Api `applyMoreData` has been deprecated since version 9.8.0.') const dataList = data.concat(this._chartStore.getDataList()) - this._chartStore.addData(dataList, false, more ?? true).then(() => {}).catch(() => {}).finally(() => { callback?.() }) + this._chartStore.addData(dataList, LoadDataType.Forward, more ?? true).then(() => {}).catch(() => {}).finally(() => { callback?.() }) } updateData (data: KLineData, callback?: () => void): void { if (isValid(callback)) { logWarn('updateData', '', 'param `callback` has been deprecated since version 9.8.0, use `subscribeAction(\'onDataReady\')` instead.') } - this._chartStore.addData(data, false).then(() => {}).catch(() => {}).finally(() => { callback?.() }) + this._chartStore.addData(data).then(() => {}).catch(() => {}).finally(() => { callback?.() }) } /** diff --git a/src/common/LoadDataCallback.ts b/src/common/LoadDataCallback.ts index efb817819..905cc788e 100644 --- a/src/common/LoadDataCallback.ts +++ b/src/common/LoadDataCallback.ts @@ -16,6 +16,7 @@ import type Nullable from './Nullable' import type KLineData from './KLineData' enum LoadDataType { + Init = 'init', Forward = 'forward', Backward = 'backward' } diff --git a/src/store/ChartStore.ts b/src/store/ChartStore.ts index f7b33fa9e..676eca729 100644 --- a/src/store/ChartStore.ts +++ b/src/store/ChartStore.ts @@ -17,7 +17,7 @@ import type KLineData from '../common/KLineData' import type Precision from '../common/Precision' import type VisibleData from '../common/VisibleData' import { getDefaultStyles, type Styles } from '../common/Styles' -import { isArray, isBoolean, isNumber, isString, isValid, merge } from '../common/utils/typeChecks' +import { isArray, isNumber, isString, isValid, merge } from '../common/utils/typeChecks' import { formatValue } from '../common/utils/format' import type LoadDataCallback from '../common/LoadDataCallback' import { type LoadDataParams, LoadDataType } from '../common/LoadDataCallback' @@ -222,18 +222,30 @@ export default class ChartStore { return this._visibleDataList } - async addData (data: KLineData | KLineData[], isFirstAdd: boolean, more?: boolean): Promise { + async addData (data: KLineData | KLineData[], type?: LoadDataType, more?: boolean): Promise { let success = false + let adjustFlag = false + if (isArray(data)) { - if (isFirstAdd) { - this.clear() - this._dataList = data - this._forwardMore = more ?? true - this._timeScaleStore.resetOffsetRightDistance() - } else { - this._dataList = data - if (isBoolean(more)) { - this._forwardMore = more + switch (type) { + case LoadDataType.Init: { + this.clear() + this._dataList = data + this._forwardMore = more ?? true + this._timeScaleStore.resetOffsetRightDistance() + adjustFlag = true + break + } + case LoadDataType.Backward: { + this._dataList = this._dataList.concat(data) + this._backwardMore = more ?? false + adjustFlag = data.length > 0 + break + } + case LoadDataType.Forward: { + this._dataList = data.concat(this._dataList) + this._forwardMore = more ?? false + adjustFlag = data.length > 0 } } this._loading = false @@ -250,16 +262,20 @@ export default class ChartStore { this._timeScaleStore.setLastBarRightSideDiffBarCount(--lastBarRightSideDiffBarCount) } success = true + adjustFlag = true } else if (timestamp === lastDataTimestamp) { this._dataList[dataCount - 1] = data success = true + adjustFlag = true } } if (success) { - this._timeScaleStore.adjustVisibleRange() - this._tooltipStore.recalculateCrosshair(true) try { - await this._indicatorStore.calcInstance() + if (adjustFlag) { + this._timeScaleStore.adjustVisibleRange() + this._tooltipStore.recalculateCrosshair(true) + await this._indicatorStore.calcInstance() + } this._chart.adjustPaneViewport(false, true, true, true) this._actionStore.execute(ActionType.OnDataReady) } catch {} @@ -291,15 +307,7 @@ export default class ChartStore { ) ) { const cb: ((data: KLineData[], more?: boolean) => void) = (data: KLineData[], more?: boolean) => { - let dataList: KLineData[] = [] - if (params.type === LoadDataType.Backward) { - dataList = this._dataList.concat(data) - this._backwardMore = more ?? false - } else { - dataList = data.concat(this._dataList) - this._forwardMore = more ?? false - } - this.addData(dataList, false).then(() => {}).catch(() => {}) + this.addData(data, params.type, more).then(() => {}).catch(() => {}) } this._loading = true this._loadDataCallback({ ...params, callback: cb })