@devexperts/dxcharts-lite
Version:
76 lines (75 loc) • 2.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/.
*/
class CandleHoverImpl {
constructor(visualCandle, price, priceFormatter, toY) {
this.visualCandle = visualCandle;
this.price = price;
this.priceFormatter = priceFormatter;
this.toY = toY;
}
get high() {
return this.visualCandle.high;
}
get low() {
return this.visualCandle.low;
}
get open() {
return this.visualCandle.open;
}
get close() {
return this.visualCandle.close;
}
get volume() {
return this.visualCandle.candle.volume;
}
get openY() {
return this.toY(this.open);
}
get closeY() {
return this.toY(this.close);
}
get highY() {
return this.toY(this.high);
}
get lowY() {
return this.toY(this.low);
}
get openFormatted() {
return this.priceFormatter(this.open);
}
get closeFormatted() {
return this.priceFormatter(this.close);
}
get highFormatted() {
return this.priceFormatter(this.high);
}
get lowFormatted() {
return this.priceFormatter(this.low);
}
get closestOHLCY() {
return this.toY([this.close, this.open, this.high, this.low].sort((a, b) => Math.abs(a - this.price) - Math.abs(b - this.price))[0]);
}
}
export class CandleHoverProducerPart {
constructor(chartModel) {
this.chartModel = chartModel;
}
/**
* Returns a CandleHover object or undefined based on the provided BaseHover object.
* @param {BaseHover} hover - The BaseHover object containing the x and y coordinates.
* @returns {CandleHover | undefined} - The CandleHover object or undefined if mainSeriesCandle is falsy.
*/
getData(hover) {
const { x, y } = hover;
const candle = this.chartModel.candleFromX(x);
const idx = candle.idx || 0;
const mainSeriesCandle = this.chartModel.getVisualCandle(idx);
const price = this.chartModel.priceFromY(y);
const candleHover = mainSeriesCandle &&
new CandleHoverImpl(mainSeriesCandle, price, this.chartModel.pane.regularFormatter, this.chartModel.toY);
return candleHover;
}
}