@devexperts/dxcharts-lite
Version:
127 lines (126 loc) • 5.44 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 { CanvasElement } from '../../../canvas/canvas-bounds-container';
import { ChartBaseElement } from '../../../model/chart-base-element';
import { DataSeriesModel, VisualSeriesPoint, defaultValueFormatter, } from '../../../model/data-series.model';
import { mergeHighLow } from '../../../model/scaling/auto-scale.model';
import { uuid } from '../../../utils/uuid.utils';
import { createYExtentFormatters } from '../../chart/price.formatter';
export class YExtentComponent extends ChartBaseElement {
constructor(paneUUID, idx, paneComponent, chartBaseModel, canvasBoundsContainer, hitTestController, dynamicObjectsCanvasModel, scale, createYAxisComponent, dragNDrop, dataSeries = new Set(), formatters = {
regular: defaultValueFormatter,
}) {
super();
this.paneUUID = paneUUID;
this.idx = idx;
this.paneComponent = paneComponent;
this.chartBaseModel = chartBaseModel;
this.canvasBoundsContainer = canvasBoundsContainer;
this.hitTestController = hitTestController;
this.dynamicObjectsCanvasModel = dynamicObjectsCanvasModel;
this.scale = scale;
this.dragNDrop = dragNDrop;
this.dataSeries = dataSeries;
this.formatters = formatters;
this.getYAxisBounds = () => {
return this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID_Y_AXIS(this.paneUUID, this.idx));
};
this.yAxisHT = this.canvasBoundsContainer.getBoundsHitTest(CanvasElement.PANE_UUID_Y_AXIS(this.paneUUID, this.idx));
this.toVisualPoints = (points) => points.map(p => new VisualSeriesPoint(this.chartBaseModel.dataFromTimestamp(p.timestamp).centerUnit, p.close));
this.toY = (value) => {
var _a, _b;
return (_b = (_a = this.mainDataSeries) === null || _a === void 0 ? void 0 : _a.view.toY(value)) !== null && _b !== void 0 ? _b : 1;
};
this.valueFormatter = (value, dataSeries) => {
var _a;
const formatter = (_a = this.formatters[this.yAxis.getAxisType()]) !== null && _a !== void 0 ? _a : this.formatters.regular;
return formatter(value, dataSeries);
};
this.addChildEntity(scale);
this.setValueFormatters(createYExtentFormatters(this));
this.yAxis = createYAxisComponent(this.valueFormatter.bind(this), () => this.mainDataSeries);
this.addChildEntity(this.yAxis);
}
doDeactivate() {
super.doDeactivate();
this.dataSeries.forEach(ds => {
this.paneComponent.seriesRemovedSubject.next(ds);
ds.deactivate();
});
}
/**
* Returns the bounds of the scale model.
* @returns {Bounds} The bounds of the scale model.
*/
getBounds() {
return this.scale.getBounds();
}
getBaseline() {
var _a, _b;
return (_b = (_a = this.mainDataSeries) === null || _a === void 0 ? void 0 : _a.getBaseline()) !== null && _b !== void 0 ? _b : 1;
}
/**
* Creates a new DataSeriesModel object.
* @returns {DataSeriesModel} - The newly created DataSeriesModel object.
*/
createDataSeries() {
const series = new DataSeriesModel(this, uuid(), this.hitTestController.getNewDataSeriesHitTestId());
series.toVisualPoints = this.toVisualPoints;
return series;
}
/**
* Adds a new data series to the chart.
* @param {DataSeriesModel} series - The data series to be added.
* @returns {void}
*/
addDataSeries(series) {
this.dataSeries.add(series);
if (this.dataSeries.size === 1) {
this.mainDataSeries = series;
}
this.paneComponent.updateView();
this.paneComponent.seriesAddedSubject.next(series);
}
/**
* Removes a data series from the chart.
*
* @param {DataSeriesModel} series - The data series to be removed.
* @returns {void}
*/
removeDataSeries(series) {
this.dataSeries.delete(series);
this.paneComponent.updateView();
this.paneComponent.seriesRemovedSubject.next(series);
}
get regularFormatter() {
return this.formatters.regular;
}
/**
* Sets the pane value formatters for the current instance.
* @param {YExtentFormatters} formatters - The pane value formatters to be set.
*/
setValueFormatters(formatters) {
this.formatters = formatters;
}
/**
* Returns the regular value from Y coordinate.
* @param {number} y - The Y coordinate.
* @returns {number} - The regular value.
*/
regularValueFromY(y) {
var _a, _b;
return (_b = (_a = this.mainDataSeries) === null || _a === void 0 ? void 0 : _a.view.priceFromY(y)) !== null && _b !== void 0 ? _b : this.scale.fromY(y);
}
}
export const createDefaultYExtentHighLowProvider = (extent) => ({
isHighLowActive: () => true,
calculateHighLow: state => {
const highLows = Array.from(extent.dataSeries)
.filter(ds => ds.highLowProvider.isHighLowActive())
.map(ds => ds.highLowProvider.calculateHighLow(state));
return mergeHighLow(highLows);
},
});