Skip to content

Commit

Permalink
opt: opt add data
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Mar 17, 2024
1 parent a09fc26 commit debdab6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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?.() })
}

/**
Expand All @@ -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?.() })
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/common/LoadDataCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type Nullable from './Nullable'
import type KLineData from './KLineData'

enum LoadDataType {
Init = 'init',
Forward = 'forward',
Backward = 'backward'
}
Expand Down
54 changes: 31 additions & 23 deletions src/store/ChartStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -222,18 +222,30 @@ export default class ChartStore {
return this._visibleDataList
}

async addData (data: KLineData | KLineData[], isFirstAdd: boolean, more?: boolean): Promise<void> {
async addData (data: KLineData | KLineData[], type?: LoadDataType, more?: boolean): Promise<void> {
let success = false
let adjustFlag = false

if (isArray<KLineData>(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
Expand All @@ -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 {}
Expand Down Expand Up @@ -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 })
Expand Down

0 comments on commit debdab6

Please sign in to comment.