@devexperts/dxcharts-lite
Version:
79 lines (78 loc) • 3.95 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 { flat } from '../../utils/array.utils';
import { setLineWidth } from '../data-series.drawer';
export class TrendHistogramDrawer {
constructor() { }
draw(ctx, allPoints, model, hitTestDrawerConfig) {
const zero = model.view.toY(0);
// here I do flat, thinks that there is no more that 1 series
const allPointsFlat = flat(allPoints);
const config = model.getPaintConfig(0);
setLineWidth(ctx, config.lineWidth, model, hitTestDrawerConfig, config.hoveredLineWidth);
allPointsFlat.forEach((point, idx) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
// odd width is crucial to draw histogram without antialiasing
// 2 colors: Negative and Positive
const previousClose = previousValue(allPointsFlat, idx);
const isNegativeTrend = previousClose && point.close < previousClose;
if (((_a = config.multiplyColors) === null || _a === void 0 ? void 0 : _a.length) === 2) {
ctx.strokeStyle =
(_b = hitTestDrawerConfig.color) !== null && _b !== void 0 ? _b : ((isNegativeTrend ? config.multiplyColors[1] : config.multiplyColors[0]) ||
config.color ||
'#FF00FF');
ctx.beginPath();
const x = model.view.toX(point.centerUnit);
const y = model.view.toY(point.close);
ctx.moveTo(x, zero);
ctx.lineTo(x, y);
ctx.stroke();
return;
}
// 4 colors: Negative and Down, Negative and Up, Positive and Down, Positive and Up
if (((_c = config.multiplyColors) === null || _c === void 0 ? void 0 : _c.length) === 4) {
if (isNegativeTrend && point.close < 0) {
ctx.strokeStyle =
(_d = hitTestDrawerConfig.color) !== null && _d !== void 0 ? _d : (config.multiplyColors[0] || config.color || '#FF00FF');
}
if (!isNegativeTrend && point.close < 0) {
ctx.strokeStyle =
(_e = hitTestDrawerConfig.color) !== null && _e !== void 0 ? _e : (config.multiplyColors[1] || config.color || '#FF00FF');
}
if (isNegativeTrend && point.close > 0) {
ctx.strokeStyle =
(_f = hitTestDrawerConfig.color) !== null && _f !== void 0 ? _f : (config.multiplyColors[2] || config.color || '#FF00FF');
}
if (!isNegativeTrend && point.close > 0) {
ctx.strokeStyle =
(_g = hitTestDrawerConfig.color) !== null && _g !== void 0 ? _g : (config.multiplyColors[3] || config.color || '#FF00FF');
}
ctx.beginPath();
const x = model.view.toX(point.centerUnit);
const y = model.view.toY(point.close);
ctx.moveTo(x, zero);
ctx.lineTo(x, y);
ctx.stroke();
return;
}
// 1 color
ctx.strokeStyle = (_h = hitTestDrawerConfig.color) !== null && _h !== void 0 ? _h : (config.color || '#FF00FF');
ctx.beginPath();
const x = model.view.toX(point.centerUnit);
const y = model.view.toY(point.close);
ctx.moveTo(x, zero);
ctx.lineTo(x, y);
ctx.stroke();
});
}
}
// weird but works
export const previousValue = (arr, idx) => {
do {
idx--;
} while (idx >= 0 && !isFinite(arr[idx] && arr[idx].close));
return arr[idx] ? arr[idx].close : undefined;
};