@devexperts/dxcharts-lite
Version:
39 lines (38 loc) • 2.35 kB
JavaScript
/*
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
* 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 { lastOf } from '../../utils/array.utils';
import { precisionsToIncrement } from '../../utils/price-increments.utils';
import { NumericAxisLabelsGenerator } from '../labels_generator/numeric-axis-labels.generator';
/**
* Y axis labels generator for prices. Respects price increment from instrument.
*/
export class NumericYAxisLabelsGenerator extends NumericAxisLabelsGenerator {
constructor(increment, dataSeriesProvider, viewportModel, valueFormatter, axisTypeProvider = () => 'regular', singleLabelHeightPixels = 23) {
super(increment, () => [viewportModel.yStart, viewportModel.yEnd], () => viewportModel.getBounds().height, valueFormatter, false, axisTypeProvider, () => { var _a, _b; return (_b = (_a = dataSeriesProvider()) === null || _a === void 0 ? void 0 : _a.getBaseline()) !== null && _b !== void 0 ? _b : 1; }, undefined, singleLabelHeightPixels);
this.dataSeriesProvider = dataSeriesProvider;
}
getLargestLabel() {
var _a;
return ((_a = this.labelsCache.getLastCachedValue()) !== null && _a !== void 0 ? _a : []).reduce((maxLengthText, label) => (label.text.length > maxLengthText.length ? label.text : maxLengthText), '');
}
/**
* Calculates the increment to be used on the chart axis based on the length of the value and the instrument's price increments.
* @param {number} valueLength - The length of the value.
* @returns {number} - The calculated increment.
*/
calculateIncrement(valueLength) {
var _a;
const dataSeries = this.dataSeriesProvider();
if (dataSeries) {
const lastCandle = lastOf(dataSeries.dataPoints);
const priceIncrementBasis = (_a = lastCandle === null || lastCandle === void 0 ? void 0 : lastCandle.close) !== null && _a !== void 0 ? _a : 0;
const increment = precisionsToIncrement(priceIncrementBasis, dataSeries.pricePrecisions);
return this.adjustIncrementOnAxisType(increment);
}
// auto-generated increment
return super.calculateIncrement(valueLength);
}
}