From 2fe1e4d615f8a35f97cf6a546f1ca50a01a3a268 Mon Sep 17 00:00:00 2001 From: Kirill Bobkov Date: Wed, 1 Nov 2023 15:45:57 +0400 Subject: [PATCH 1/2] fix: sometimes volume label is displayed incorrectly --- .../components/cross_tool/cross-tool.model.ts | 2 +- .../volumes/separate-volumes.component.ts | 3 +- .../components/volumes/volumes.formatter.ts | 62 +++++++++---------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/chart/components/cross_tool/cross-tool.model.ts b/src/chart/components/cross_tool/cross-tool.model.ts index c54148ac..84d78ffb 100644 --- a/src/chart/components/cross_tool/cross-tool.model.ts +++ b/src/chart/components/cross_tool/cross-tool.model.ts @@ -126,7 +126,7 @@ export class CrossToolModel extends ChartBaseElement { } else { this.currentHover.y = hover.y; } - this.currentHover.paneId = CHART_UUID; + this.currentHover.paneId = hover.paneId; this.currentHoverSubject.next(this.currentHover); } } diff --git a/src/chart/components/volumes/separate-volumes.component.ts b/src/chart/components/volumes/separate-volumes.component.ts index cf190db1..87360aa4 100644 --- a/src/chart/components/volumes/separate-volumes.component.ts +++ b/src/chart/components/volumes/separate-volumes.component.ts @@ -48,9 +48,10 @@ export class SeparateVolumesComponent extends ChartBaseElement { */ activateSeparateVolumes() { if (this.paneManager.panes[SeparateVolumesComponent.UUID] === undefined) { + const precision = 1; const volumePane = this.paneManager.createPane(SeparateVolumesComponent.UUID, { paneFormatters: { - regular: volumeFormatter, + regular: (value: number) => volumeFormatter(value, precision), }, useDefaultHighLow: false, increment: 1, diff --git a/src/chart/components/volumes/volumes.formatter.ts b/src/chart/components/volumes/volumes.formatter.ts index 28fc67c3..4309d46b 100644 --- a/src/chart/components/volumes/volumes.formatter.ts +++ b/src/chart/components/volumes/volumes.formatter.ts @@ -3,42 +3,38 @@ * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ - -import { NumberFormatLabels } from '../../utils/math.utils'; - -const DEFAULT_PRECISION = 5; -const MIN_VALUE = 0.0000000001; -const TRAILLING_ZEROES_REGEXP = /\.?0+$/; -const TRAILLING_FRACTION_WITH_ZEROES_REGEXP = /(\.[1-9]*)0+$/; - -export const volumeFormatter = (value: number): string => { - const formatValue = (value: number, amountToCut?: NumberFormatLabels): string => { - const rounder = Math.pow(10, DEFAULT_PRECISION); - const roundedValue = Math.round(value * rounder) / rounder; - - if (roundedValue < 1 && roundedValue >= MIN_VALUE) { - return roundedValue - .toFixed(DEFAULT_PRECISION) - .replace(TRAILLING_ZEROES_REGEXP, '') - .replace(TRAILLING_FRACTION_WITH_ZEROES_REGEXP, ''); +export const volumeFormatter = (value: number, precision = 1) => { + function formatNumber(value: number): string { + let formattedResult: string; + const priceScale = Math.pow(10, precision); + value = Math.round(value * priceScale) / priceScale; + if (value >= 1e-15 && value < 1) { + formattedResult = value.toFixed(precision).replace(/\.?0+$/, ''); // regex removes trailing zeroes } else { - return (roundedValue + '').replace(TRAILLING_FRACTION_WITH_ZEROES_REGEXP, '') + (amountToCut || ''); + formattedResult = String(value); } - }; - - if (Math.abs(value) > 999_999_999) { - value = Math.round(value / 1_000_000) * 1_000_000; - return formatValue(value / 1_000_000_000, 'B'); + return formattedResult.replace(/(\.[1-9]*)0+$/, (e: string, p1: string): string => p1); } - if (Math.abs(value) > 999_999) { - value = 1000 * Math.round(value / 1000); - return formatValue(value / 1_000_000, 'M'); - } - - if (Math.abs(value) > 9_999) { - return formatValue(value / 1000, 'K'); + function format(value: number): string { + let sign = ''; + if (value < 0) { + sign = '-'; + value = -value; + } + if (value < 995) { + return sign + formatNumber(value); + } + if (value < 999995) { + return sign + formatNumber(value / 1000) + 'K'; + } + if (value < 999999995) { + value = 1000 * Math.round(value / 1000); + return sign + formatNumber(value / 1000000) + 'M'; + } + value = 1000000 * Math.round(value / 1000000); + return sign + formatNumber(value / 1000000000) + 'B'; } - return formatValue(value); -}; + return format(value); +}; \ No newline at end of file From fb96a0c160561e3bc3b663e306554d9411a16f03 Mon Sep 17 00:00:00 2001 From: Kirill Bobkov Date: Wed, 1 Nov 2023 18:58:40 +0400 Subject: [PATCH 2/2] fix: sometimes volume label is displayed incorrectly // unexpected export --- src/chart/utils/math.utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chart/utils/math.utils.ts b/src/chart/utils/math.utils.ts index c559f15c..1f17225c 100644 --- a/src/chart/utils/math.utils.ts +++ b/src/chart/utils/math.utils.ts @@ -5,7 +5,7 @@ import { StringTMap } from './object.utils'; * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -export const MAX_DECIMAL_DIGITS = 14; +const MAX_DECIMAL_DIGITS = 14; // Array of powers of 10. Used in roundDecimal to walk through mantissa. const POW10: number[] = [];