UNPKG

@devexperts/dxcharts-lite

Version:
59 lines (58 loc) 2.44 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 { clipToBounds } from '../utils/canvas/canvas-drawing-functions.utils'; import { isEmpty } from '../utils/object.utils'; export const transformToTwoDimension = (points) => (Array.isArray(points[0]) ? points : [points]); /** * Basic data series drawer. * Have multiple paint tools: linear, histogram, points, text above/below candle and others. * * (may support multiple layers in future) */ export class DataSeriesDrawer { constructor(paneManager, seriesDrawers) { this.paneManager = paneManager; this.seriesDrawers = seriesDrawers; } draw(canvasModel, model, paneUUID) { const ctx = canvasModel.ctx; const pane = paneUUID && this.paneManager.panes[paneUUID]; if (model) { ctx.save(); pane && clipToBounds(ctx, pane.getBounds()); this.drawSeries(ctx, model); ctx.restore(); } } drawSeries(ctx, series) { const visibilityPredicates = series.config.additionalVisibilityPredicatesMap; if (series.config.visible || (visibilityPredicates && !isEmpty(visibilityPredicates))) { const paintTool = series.config.type; const drawer = this.seriesDrawers[paintTool]; if (drawer) { const viewportSeries = series.getSeriesInViewport(series.scale.xStart - 1, series.scale.xEnd + 1); if (viewportSeries && viewportSeries.length >= 1) { // +- 1 to correctly draw points which are partly inside bounds drawer.draw(ctx, viewportSeries, series, {}); } } else { console.error(`Data series drawer with type ${paintTool} isn't registered!`); } } } } export const setLineWidth = (ctx, lineWidth, dataSeries, hitTestDrawerConfig, seriesSelectedWidth = lineWidth) => { if (hitTestDrawerConfig.hoverWidth) { ctx.lineWidth = hitTestDrawerConfig.hoverWidth; } else if (dataSeries.highlighted) { ctx.lineWidth = lineWidth !== seriesSelectedWidth ? lineWidth + 1 : seriesSelectedWidth; } else { ctx.lineWidth = lineWidth; } };