UNPKG

@devexperts/dxcharts-lite

Version:
81 lines (80 loc) 3.26 kB
/* * 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/. */ import { Subject } from 'rxjs'; import { ChartBaseElement } from '../../model/chart-base-element'; import { getElementsInRange } from '../../utils/array.utils'; export const HIGHLIGHTS_TYPES = ['AFTER_MARKET', 'PRE_MARKET', 'NO_TRADING', 'REGULAR']; const DAY_MS = 24 * 60 * 60 * 1000; export class HighlightsModel extends ChartBaseElement { constructor(chartModel) { super(); this.chartModel = chartModel; this.highlights = []; this.visualHighlights = {}; this.highlightsUpdatedSubject = new Subject(); } /** * Activates the chart by subscribing to the xChanged event of the scaleModel. * If there are highlights, it recalculates the visual highlights. */ activate() { this.addRxSubscription(this.chartModel.scale.xChanged.subscribe(() => this.highlights.length && this.recalculateVisualHighlights())); } /** * Returns an array of Highlight objects. * @returns {Highlight[]} - An array of Highlight objects. */ getHighlights() { return this.highlights; } /** * Returns the visual highlights object. * * @returns {VisualHighlights} The visual highlights object. */ getVisualHighlights() { return this.visualHighlights; } /** * Recalculates the visual highlights based on the current chart model and highlights. * @private * @returns {void} */ recalculateVisualHighlights() { this.visualHighlights = {}; const firstTimestamp = this.chartModel.getFirstTimestamp(); const lastTimestamp = this.chartModel.getLastTimestamp() + DAY_MS; const highlightsInRange = getElementsInRange(this.highlights, firstTimestamp, lastTimestamp, highlightTransformFunc); highlightsInRange.forEach(h => { var _a; if (!this.visualHighlights[h.type]) { this.visualHighlights[h.type] = []; } (_a = this.visualHighlights[h.type]) === null || _a === void 0 ? void 0 : _a.push(h); }); } /** * Sets the highlights of the component and updates the visual highlights accordingly. * @param {Highlight[]} highlights - An array of Highlight objects to be set as the new highlights. * @returns {void} */ setHighlights(highlights) { this.highlights = highlights; this.highlights.sort((a, b) => a.to - b.to); this.highlightsUpdatedSubject.next(this.highlights); this.highlights.length ? this.recalculateVisualHighlights() : (this.visualHighlights = {}); } /** * Returns an Observable that emits an array of Highlight objects whenever the highlightsUpdatedSubject emits a new value. * @returns {Observable<Highlight[]>} An Observable that emits an array of Highlight objects. */ observeHighlightsUpdated() { return this.highlightsUpdatedSubject.asObservable(); } } const highlightTransformFunc = (item) => { return item.to; };