@devexperts/dxcharts-lite
Version:
123 lines (122 loc) • 6.28 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 { getPrimaryLabelTextColor } from '../label-color.functions';
import { lastOf } from '../../../utils/array.utils';
import { getLabelTextColorByBackgroundColor } from '../../../utils/canvas/canvas-text-functions.utils';
export class LastCandleLabelsProvider {
constructor(chartModel, chartConfig, yAxisConfig, lastCandleLabelsByChartType, resolveLabelColorFn) {
this.chartModel = chartModel;
this.chartConfig = chartConfig;
this.yAxisConfig = yAxisConfig;
this.lastCandleLabelsByChartType = lastCandleLabelsByChartType;
this.resolveLabelColorFn = resolveLabelColorFn;
}
/**
* Returns an array of LabelGroup objects that contain the labels for the yAxis of the chart.
* @returns {LabelGroup[]} An array of LabelGroup objects that contain the labels for the yAxis of the chart.
*/
getUnorderedLabels() {
const collectedLabels = [];
const visible = this.yAxisConfig.labels.settings.lastPrice.mode !== 'none' && this.yAxisConfig.visible;
if (!visible) {
return collectedLabels;
}
// main candle series
const yAxisVisualLabel = this.getYAxisVisualLabel(this.chartModel.mainCandleSeries);
const mainCandleSeriesVisualLabel = yAxisVisualLabel
? Object.assign(Object.assign({}, yAxisVisualLabel), this.getLabelDrawConfig(this.chartModel.mainCandleSeries, true)) : yAxisVisualLabel;
if (mainCandleSeriesVisualLabel) {
const mainCandleSeriesLabels = { labels: [mainCandleSeriesVisualLabel] };
const handler = this.lastCandleLabelsByChartType[this.chartConfig.components.chart.type];
handler === null || handler === void 0 ? void 0 : handler(mainCandleSeriesLabels, this.chartModel.mainCandleSeries);
collectedLabels.push(mainCandleSeriesLabels);
}
// compare candle series
this.chartModel.candleSeries.forEach((series, index) => {
if (index === 0) {
return;
}
const yAxisVisualLabel = this.getYAxisVisualLabel(series);
const secondarySeriesVisualLabel = yAxisVisualLabel
? Object.assign(Object.assign({}, yAxisVisualLabel), this.getLabelDrawConfig(series, false)) : yAxisVisualLabel;
if (secondarySeriesVisualLabel) {
const secondarySeriesLabel = {
labels: [secondarySeriesVisualLabel],
};
const handler = this.lastCandleLabelsByChartType[series.config.type];
handler === null || handler === void 0 ? void 0 : handler(secondarySeriesLabel, this.chartModel.mainCandleSeries);
collectedLabels.push(secondarySeriesLabel);
}
});
return collectedLabels;
}
/**
* Returns the visual label for the Y axis of a candle series.
* @param {CandleSeriesModel} series - The candle series model.
* @returns {Omit<VisualYAxisLabel, 'bgColor'> | null} - The visual label for the Y axis or null if there is no data.
*/
getYAxisVisualLabel(series) {
const lastCandle = lastOf(series.dataPoints);
if (lastCandle) {
const y = series.view.toY(lastCandle.close);
if (isFinite(y)) {
const mode = this.yAxisConfig.labels.settings.lastPrice.mode;
const appearanceType = this.yAxisConfig.labels.settings.lastPrice.type;
return {
y,
labelWeight: 0,
labelText: this.chartModel.pane.valueFormatter(lastCandle.close, series),
mode,
labelType: appearanceType,
description: series.instrument.symbol,
};
}
}
return null;
}
/**
* Returns the configuration object for drawing the label of the Y-axis.
* @param {CandleSeriesModel} series - The series model for which the label configuration is to be returned.
* @param {boolean} primary - A boolean value indicating whether the label is for the main series or not.
* @returns {YAxisLabelDrawConfig} - The configuration object for drawing the label of the Y-axis.
*/
getLabelDrawConfig(series, primary) {
const appearanceType = this.yAxisConfig.labels.settings.lastPrice.type;
const colors = series.colors.labels;
const { rectLabelTextColor = 'white', rectLabelInvertedTextColor = 'black' } = this.chartConfig.colors.yAxis;
const getLabelColorBySeries = this.resolveLabelColorFn(series.config.type);
if (!colors) {
return {
bgColor: '#FFFFFF',
textColor: getLabelTextColorByBackgroundColor('#FFFFFF', '#000000', rectLabelInvertedTextColor),
rounded: true,
};
}
const boxColor = getLabelColorBySeries(series.lastPriceMovement, series.colors);
// if the label is for the main candle series
if (primary) {
const isScatterPlot = series.config.type === 'scatterPlot';
const textColor = isScatterPlot
? rectLabelTextColor
: getPrimaryLabelTextColor(series.lastPriceMovement, colors);
return {
bgColor: boxColor,
textColor: appearanceType === 'plain'
? getLabelColorBySeries(series.lastPriceMovement, series.colors)
: getLabelTextColorByBackgroundColor(boxColor, textColor, rectLabelInvertedTextColor),
rounded: true,
};
}
// if the label is for the secondary candle series
return {
bgColor: boxColor,
textColor: appearanceType === 'plain'
? getLabelColorBySeries(series.lastPriceMovement, series.colors)
: getLabelTextColorByBackgroundColor(boxColor, rectLabelTextColor, rectLabelInvertedTextColor),
rounded: true,
};
}
}