@devexperts/dxcharts-lite
Version:
82 lines (81 loc) • 3.41 kB
JavaScript
/*
* Copyright (C) 2019 - 2026 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/.
*/
/*
* Copyright (C) 2019 - 2024 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 { getFontFromConfig } from '../chart.config';
import { calculateTextWidth } from '../utils/canvas/canvas-font-measure-tool.utils';
export class YAxisBoundsContainer {
constructor(config, mainCanvasModel) {
this.config = config;
this.mainCanvasModel = mainCanvasModel;
this.extentsOrder = new Map();
this.yAxisWidthContributors = [];
}
/**
* Registers a YAxisWidthContributor to the chart.
*
* @param {YAxisWidthContributor} contributor - The YAxisWidthContributor to be registered.
* @returns {void}
*/
registerYAxisWidthContributor(contributor) {
this.yAxisWidthContributors.push(contributor);
}
/**
* Removes a YAxisWidthContributor from the chart.
*
* @param {YAxisWidthContributor} contributor - The YAxisWidthContributor to be removed.
* @returns {void}
*/
removeYAxisWidthContributor(contributor) {
this.yAxisWidthContributors = this.yAxisWidthContributors.filter(c => c !== contributor);
}
/**
* Calculates the width of a given text using the font from the yAxis component of the chart configuration
* @param {string} text - The text to calculate the width of
* @returns {number} - The width of the text in pixels
*/
getTextWidth(text) {
const font = getFontFromConfig(this.config.components.yAxis);
return calculateTextWidth(text, this.mainCanvasModel.ctx, font);
}
/**
* Updates width of Y axis.
* Considers max text of YAxis labels.
*/
getYAxisWidths() {
this.extentsOrder.clear();
const left = [];
const right = [];
this.yAxisWidthContributors.forEach(c => {
var _a, _b, _c;
const state = c.getYAxisState();
// if YAxis is not visible, do not add it to contributors width
if (!state.visible) {
return;
}
const margin = state.labelBoxMargin.start + state.labelBoxMargin.end;
const width = this.getTextWidth(c.getLargestLabel()) + margin;
const idx = c.getYAxisIndex();
const uuid = c.getPaneUUID();
const extentOrder = (_a = this.extentsOrder.get(uuid)) !== null && _a !== void 0 ? _a : { left: [], right: [] };
if (state.align === 'left') {
const i = extentOrder.left.length;
left[i] = Math.max((_b = left[i]) !== null && _b !== void 0 ? _b : 0, width);
extentOrder.left.push(idx);
}
else {
const i = extentOrder.right.length;
right[i] = Math.max((_c = right[i]) !== null && _c !== void 0 ? _c : 0, width);
extentOrder.right.push(idx);
}
this.extentsOrder.set(uuid, extentOrder);
});
return { left, right };
}
}