@devexperts/dxcharts-lite
Version:
58 lines (57 loc) • 2.53 kB
JavaScript
/*
* 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/.
*/
/*
* Copyright (C) 2019 - 2024 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 { BehaviorSubject, merge } from 'rxjs';
import { ChartBaseElement } from '../../model/chart-base-element';
import { firstOf, maxMin } from '../../utils/array.utils';
const volumeMaxMinFn = maxMin(candle => candle.candle.volume);
export const VOLUMES_UUID = 'volumes';
export class VolumesModel extends ChartBaseElement {
constructor(chartComponent, scale) {
super();
this.chartComponent = chartComponent;
this.scale = scale;
this.id = VOLUMES_UUID;
// max volume in all data series
this.volumeMax = new BehaviorSubject(0);
this.highLowProvider = {
calculateHighLow: () => ({ high: this.volumeMax.getValue(), low: 0 }),
isHighLowActive: () => true,
};
}
doActivate() {
super.doActivate();
this.addRxSubscription(merge(this.chartComponent.chartModel.observeCandlesChanged(), this.scale.xChanged).subscribe(() => this.updateVolumeMax()));
this.addRxSubscription(this.chartComponent.chartModel.mainCandleSeries
.observeLastVisualCandleChanged()
.subscribe(() => this.recalculateLastVisualVolume()));
}
/**
* Used for optimization when we have to update only the last candle
* Doesn't work for line chart types
* @doc-tags tricky
*/
recalculateLastVisualVolume() {
// TODO rework, move this logic to drawer
}
/**
* Updates the maximum volume value of the chart.
* @function
* @name updateVolumeMax
* @memberof ChartComponent
* @returns {void}
*/
updateVolumeMax() {
var _a;
// Use the same parameters as in drawer to ensure volumeMax is calculated from the same candles that will be drawn
const candles = this.chartComponent.chartModel.mainCandleSeries.getSeriesInViewport(this.scale.xStart - 1, this.scale.xEnd + 1);
this.volumeMax.next((_a = firstOf(volumeMaxMinFn(candles.flat()))) !== null && _a !== void 0 ? _a : 0);
}
}