Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sometimes volume label is displayed incorrectly #71

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/chart/components/cross_tool/cross-tool.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/chart/components/volumes/separate-volumes.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export class SeparateVolumesComponent extends ChartBaseElement {
*/
activateSeparateVolumes() {
if (this.paneManager.panes[SeparateVolumesComponent.UUID] === undefined) {
const precision = 1;
disyakidneyshot marked this conversation as resolved.
Show resolved Hide resolved
const volumePane = this.paneManager.createPane(SeparateVolumesComponent.UUID, {
paneFormatters: {
regular: volumeFormatter,
regular: (value: number) => volumeFormatter(value, precision),
},
useDefaultHighLow: false,
increment: 1,
Expand Down
62 changes: 29 additions & 33 deletions src/chart/components/volumes/volumes.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion src/chart/utils/math.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down