From 0126dee30f2e89f464a27cc3ace61f667437f064 Mon Sep 17 00:00:00 2001 From: Anton Vorobev Date: Tue, 24 Sep 2024 12:25:59 +0000 Subject: [PATCH] Pull request #5292: Feature/DXCF-5587 dxc 289 switching to using candle identifiers replace binary search by search by Merge in DXCHARTS/dxchart5 from feature/DXCF-5587-dxc-289-switching-to-using-candle-identifiers-replace-binary-search-by-search-by to master * commit 'c78e9346a440f9a85a5031d5f28ef5e6427da241': [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // pr fix [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // pr fix [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // init [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // init [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // init GitOrigin-RevId: 0eb493a0dc68b01c46632ced1e21bad6f7e99cd8 --- src/chart/chart.config.ts | 4 ++++ src/chart/components/chart/chart.model.ts | 20 +++++++++++--------- src/chart/model/candle.model.ts | 3 +++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/chart/chart.config.ts b/src/chart/chart.config.ts index d919e6a..b2e311b 100644 --- a/src/chart/chart.config.ts +++ b/src/chart/chart.config.ts @@ -14,6 +14,7 @@ import { DrawerType } from './drawers/drawing-manager'; import { DateTimeFormatter, TimeFormatterConfig } from './model/date-time.formatter'; import { DEFAULT_MERGE_OPTIONS, merge, MergeOptions } from './utils/merge.utils'; import { DeepPartial } from './utils/object.utils'; +import { Candle, defaultSortCandles } from './model/candle.model'; export const MAIN_FONT = 'Open Sans Semibold, sans-serif'; @@ -109,6 +110,7 @@ export const getDefaultConfig = (): FullChartConfig => ({ histogram: { barCapSize: 1, }, + sortCandles: defaultSortCandles, }, yAxis: { type: 'regular', @@ -977,6 +979,8 @@ export interface ChartConfigComponentsChart { selectedWidth: number; minCandlesOffset: number; histogram: ChartConfigComponentsHistogram; + // optional because backward compability + sortCandles?: (candles: Candle[]) => Candle[]; } export interface ChartConfigComponentsEvents { diff --git a/src/chart/components/chart/chart.model.ts b/src/chart/components/chart/chart.model.ts index 4194bc0..38c75a2 100755 --- a/src/chart/components/chart/chart.model.ts +++ b/src/chart/components/chart/chart.model.ts @@ -233,7 +233,7 @@ export class ChartModel extends ChartBaseElement { instrument: ChartInstrument = this.mainCandleSeries.instrument, recalculateAndUpdate = true, ): CandleSeriesModel | undefined { - const preparedCandles = prepareCandles(candles); + const preparedCandles = this.prepareCandles(candles); // set correct indexes based on main candles timestamp const reindexCandles = this.reindexCandlesBasedOnSeries(this.mainCandleSeries.dataPoints, preparedCandles); // ensure there are no gaps in new candles @@ -270,7 +270,7 @@ export class ChartModel extends ChartBaseElement { this.mainInstrumentChangedSubject.next(mainSeries.instrument); } this.rememberCurrentTimeframe(); - const preparedCandles = prepareCandles(mainSeries.candles); + const preparedCandles = this.prepareCandles(mainSeries.candles); this.mainCandleSeries.clearData(); reindexCandles(preparedCandles); this.mainCandleSeries.dataPoints = preparedCandles; @@ -364,7 +364,7 @@ export class ChartModel extends ChartBaseElement { return; } - const preparedCandles = prepareCandles(mainSeries.candles); + const preparedCandles = this.prepareCandles(mainSeries.candles); const updateResult = updateCandles(this.mainCandleSeries.dataPoints, preparedCandles); const updatedCandles = updateResult.candles; reindexCandles(updatedCandles); @@ -372,7 +372,7 @@ export class ChartModel extends ChartBaseElement { // re-create series secondarySeries.map(series => { - const preparedCandles = prepareCandles(series.candles); + const preparedCandles = this.prepareCandles(series.candles); const updatedCandles = updateCandles( this.findSecondarySeriesBySymbol(series.instrument?.symbol ?? '')?.dataPoints ?? [], preparedCandles, @@ -1170,6 +1170,13 @@ export class ChartModel extends ChartBaseElement { this.candlesUpdatedSubject.next(); this.canvasModel.fireDraw(); } + + private prepareCandles = (candles: PartialCandle[]): Candle[] => { + const prepared = candles.map(prepareCandle).filter(isCandle); + const { sortCandles } = this.config.components.chart; + + return sortCandles ? sortCandles(prepared) : prepared; + }; } export interface UpdateCandlesResult { @@ -1178,11 +1185,6 @@ export interface UpdateCandlesResult { candles: Candle[]; } -const sortCandles = (candles: Candle[]): Candle[] => - candles.slice().sort((a, b) => (a.timestamp === b.timestamp ? 0 : a.timestamp > b.timestamp ? 1 : -1)); - -const prepareCandles = (candles: PartialCandle[]): Candle[] => sortCandles(candles.map(prepareCandle).filter(isCandle)); - const findFirstNotEmptyCandle = (candles: Array, startIdx: number, iterateStep: number): Candle | undefined => { if (startIdx >= candles.length) { return candles[candles.length - 1]; diff --git a/src/chart/model/candle.model.ts b/src/chart/model/candle.model.ts index 3e4018d..dfbd915 100755 --- a/src/chart/model/candle.model.ts +++ b/src/chart/model/candle.model.ts @@ -29,6 +29,9 @@ export interface Candle { readonly vwap?: number; } +export const defaultSortCandles = (candles: Candle[]): Candle[] => + candles.slice().sort((a, b) => (a.timestamp === b.timestamp ? 0 : a.timestamp > b.timestamp ? 1 : -1)); + export const generateCandleId = (timestamp: number, hashValue: number | string): string => { return `${timestamp}_${hashCode(hashValue.toString())}`; };