UNPKG

@devexperts/dxcharts-lite

Version:
75 lines (74 loc) 3.56 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 { DEFAULT_DATA_SERIES_PAINT_CONFIG } from '../../../model/data-series.config'; import { getLabelTextColorByBackgroundColor } from '../../../utils/canvas/canvas-text-functions.utils'; export class DataSeriesYAxisLabelsProvider { constructor(series, config, yAxisBoundsProvider, axisState) { this.series = series; this.config = config; this.yAxisBoundsProvider = yAxisBoundsProvider; this.axisState = axisState; } /** * Returns an array of LabelGroup objects that contain VisualYAxisLabel objects. * The labels are unordered and are based on the last data series point or the last visual series point, depending on the configuration. * The labels are formatted using the series value formatter. * The label appearance type and draw configuration are also based on the configuration. * @returns {LabelGroup[]} An array of LabelGroup objects that contain VisualYAxisLabel objects. */ getUnorderedLabels() { const visible = this.config.visible; if (!visible) { return []; } const getLastPointForLabel = this.config.labelLastValue === 'series' ? this.series.getLastDataSeriesPoint : this.series.getLastVisualSeriesPoint; const bounds = this.yAxisBoundsProvider(); const mode = this.config.labelMode; const appearanceType = this.config.labelAppearanceType; const lastPoint = getLastPointForLabel(); if (lastPoint === undefined) { return []; } const lastPointY = this.series.view.toY(lastPoint.close); if (!isFinite(lastPointY)) { return []; } const label = this.series.valueFormatter(lastPoint.close); const drawConfig = this.getLabelDrawConfig(); return [ { labels: [ Object.assign({ y: lastPointY, description: this.series.name, mode, labelType: appearanceType, labelText: label }, drawConfig), ], axisState: this.axisState, bounds, }, ]; } /** * Retrieves the `config` object from the `series` object and then retrieves the `paintConfig` object from the `config` object. * If `paintConfig` is null or undefined, it uses the `DEFAULT_DATA_SERIES_PAINT_CONFIG`. It then retrieves the `bgColor` * from the `paintConfig` object and calculates the `textColor` based on the `bgColor` using the `getLabelTextColorByBackgroundColor` function. * Finally, it returns an object with `textColor`, `bgColor`, `paddingBottom`, `paddingEnd`, and `paddingTop` properties. * @returns {YAxisLabelDrawConfig} */ getLabelDrawConfig() { var _a; const config = this.series.config; const paintConfig = (_a = config.paintConfig[0]) !== null && _a !== void 0 ? _a : DEFAULT_DATA_SERIES_PAINT_CONFIG; const bgColor = paintConfig.color; const textColor = getLabelTextColorByBackgroundColor(bgColor, 'white', 'black'); return { textColor, bgColor, paddingBottom: config.labelPaddingBottom, paddingEnd: config.labelPaddingEnd, paddingTop: config.labelPaddingTop, }; } }