UNPKG

@devexperts/dxcharts-lite

Version:
68 lines (67 loc) 4.02 kB
/* * 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 { CanvasElement, CHART_UUID, limitYToBounds, } from '../../../canvas/canvas-bounds-container'; import { fillRect } from '../../../utils/canvas/canvas-drawing-functions.utils'; import { drawLabel } from './price-label.drawer'; export class YAxisPriceLabelsDrawer { constructor(yAxisLabelsCanvasModel, yAxisDescriptionsCanvasModel, backgroundCanvasModel, canvasBoundsContainer, fullConfig, paneManager) { this.yAxisLabelsCanvasModel = yAxisLabelsCanvasModel; this.yAxisDescriptionsCanvasModel = yAxisDescriptionsCanvasModel; this.backgroundCanvasModel = backgroundCanvasModel; this.canvasBoundsContainer = canvasBoundsContainer; this.fullConfig = fullConfig; this.paneManager = paneManager; } draw() { const ctx = this.yAxisLabelsCanvasModel.ctx; const backgroundCtx = this.backgroundCanvasModel.ctx; const descriptionsCtx = this.yAxisDescriptionsCanvasModel.ctx; this.paneManager.yExtents.forEach(extent => { if (extent.yAxis.state.visible) { const yAxisBounds = extent.getYAxisBounds(); const paneBounds = this.canvasBoundsContainer.getBounds(CanvasElement.ALL_PANES); const orderedLabels = extent.yAxis.model.fancyLabelsModel.orderedLabels; this.drawHighlightedBackgroundBetweenLabels(orderedLabels); orderedLabels.forEach(l => { var _a; const bounds = (_a = l.bounds) !== null && _a !== void 0 ? _a : yAxisBounds; l.labels.forEach(vl => drawLabel(ctx, descriptionsCtx, backgroundCtx, bounds, paneBounds, vl, this.canvasBoundsContainer, extent.yAxis.state, this.fullConfig.colors)); }); // TODO I added this as a simple mechanism to add custom labels, we need to review it Object.values(extent.yAxis.model.fancyLabelsModel.customLabels).forEach(l => drawLabel(ctx, descriptionsCtx, backgroundCtx, yAxisBounds, paneBounds, l, this.canvasBoundsContainer, extent.yAxis.state, this.fullConfig.colors)); } }); } // this is a very simple solution which matches 2 labels with same "subGroupId" // in future we may want to change it, but for now it's enough // and I guess it used only for drawings drawHighlightedBackgroundBetweenLabels(orderedLabels) { const ctx = this.yAxisLabelsCanvasModel.ctx; const map = {}; orderedLabels.forEach(group => { group.labels.forEach(label => { var _a, _b, _c; if (label.subGroupId) { const labels = (_a = map[label.subGroupId]) !== null && _a !== void 0 ? _a : []; map[label.subGroupId] = labels; labels.push(label); if (labels.length === 2) { const bounds = (_b = group.bounds) !== null && _b !== void 0 ? _b : this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID_Y_AXIS(CHART_UUID)); ctx.save(); ctx.fillStyle = (_c = labels[0].highlightColor) !== null && _c !== void 0 ? _c : labels[0].bgColor; // start and end are sorted in ascending order const [startY, endY] = labels[0].y > labels[1].y ? [labels[1].y, labels[0].y] : [labels[0].y, labels[1].y]; fillRect(ctx, { x: bounds.x, y: limitYToBounds(startY, bounds) }, { x: bounds.x + bounds.width - 6, y: limitYToBounds(endY, bounds) }); ctx.restore(); } } }); }); } getCanvasIds() { return [this.yAxisLabelsCanvasModel.canvasId]; } }