UNPKG

@devexperts/dxcharts-lite

Version:
43 lines (42 loc) 1.97 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 { flat } from '../../utils/array.utils'; import { setLineWidth } from '../data-series.drawer'; export class LinearTrendDrawer { constructor() { } draw(ctx, allPoints, model, hitTestDrawerConfig) { const allPointsFlat = flat(allPoints); const config = model.getPaintConfig(0); setLineWidth(ctx, config.lineWidth, model, hitTestDrawerConfig, config.hoveredLineWidth); ctx.lineCap = 'round'; allPointsFlat.forEach((point, idx) => { var _a, _b; const previousValue = getPreviousValue(allPointsFlat, idx) || allPointsFlat[0]; const isNegativeTrend = previousValue && point.close < previousValue.close; 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'); const x = model.view.toX(point.centerUnit); const y = model.view.toY(point.close); const prevX = model.view.toX(previousValue.centerUnit); const prevY = model.view.toY(previousValue.close); ctx.beginPath(); ctx.moveTo(prevX, prevY); ctx.lineTo(x, y); ctx.stroke(); return; } }); } } export const getPreviousValue = (arr, idx) => { do { idx--; } while (idx >= 0 && !isFinite(arr[idx] && arr[idx].close)); return arr[idx] ? arr[idx] : undefined; };