@devexperts/dxcharts-lite
Version:
95 lines (94 loc) • 4.11 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 { ClearCanvasDrawer } from '../../drawers/clear-canvas.drawer';
import { CompositeDrawer } from '../../drawers/composite.drawer';
import { ChartBaseElement } from '../../model/chart-base-element';
import { CrossToolDrawer } from './cross-tool.drawer';
import { CrossToolModel } from './cross-tool.model';
import { CrossAndLabelsDrawerType } from './types/cross-and-labels.drawer';
import { NoneDrawerType } from './types/none.drawer';
/**
* Default bundled chart cross tool.
*/
export class CrossToolComponent extends ChartBaseElement {
constructor(config, crossToolCanvasModel, canvasBoundsContainer, drawingManager, paneManager, crossEventProducer, hoverProducer, baselineModel, chartModel) {
super();
this.config = config;
this.crossToolCanvasModel = crossToolCanvasModel;
this.canvasBoundsContainer = canvasBoundsContainer;
this.drawingManager = drawingManager;
this.paneManager = paneManager;
this.crossEventProducer = crossEventProducer;
this.hoverProducer = hoverProducer;
this.baselineModel = baselineModel;
this.chartModel = chartModel;
this.crossToolTypeDrawers = {};
this.model = new CrossToolModel(config.components.crossTool, this.crossToolCanvasModel, this.crossEventProducer, this.hoverProducer, this.canvasBoundsContainer, this.baselineModel, this.chartModel);
this.addChildEntity(this.model);
const clearCanvasDrawer = new ClearCanvasDrawer(this.crossToolCanvasModel);
this.registerDefaultDrawerTypes();
const crossToolDrawer = new CrossToolDrawer(this.model, this.config, this.crossToolCanvasModel, this.crossToolTypeDrawers);
const compositeDrawer = new CompositeDrawer();
compositeDrawer.addDrawer(clearCanvasDrawer, 'CLEAR_CANVAS');
compositeDrawer.addDrawer(crossToolDrawer, 'CROSS_TOOL_DRAWER');
this.drawingManager.addDrawer(compositeDrawer, 'CROSS_TOOL');
}
/**
* Registers default drawer types for the chart.
* @private
* @function
* @name registerDefaultDrawerTypes
*
* @returns {void}
*/
registerDefaultDrawerTypes() {
this.registerCrossToolTypeDrawer('cross-and-labels', new CrossAndLabelsDrawerType(this.config, this.canvasBoundsContainer, this.paneManager, () => true));
this.registerCrossToolTypeDrawer('just-labels', new CrossAndLabelsDrawerType(this.config, this.canvasBoundsContainer, this.paneManager, () => true, true));
this.registerCrossToolTypeDrawer('none', new NoneDrawerType());
}
//#region public methods
/**
* Sets the cross tool visibility.
* @param visible
*/
setVisible(visible) {
this.model.setType(visible ? 'cross-and-labels' : 'none');
}
/**
* Sets the crosstool type.
* - cross-and-labels - both the crosshair and X/Y labels
* - only-labels - only the X/Y label
* - none
* @param type
*/
setType(type) {
this.model.setType(type);
}
/**
* Returns an Observable that emits the current hover subject.
*/
observeCrossToolChanged() {
return this.model.currentHoverSubject;
}
/**
* Sets magnet target for cross tool.
* Supported only for 'cross-and-labels' type.
* Default magnet target is none.
* @param target
*/
setMagnetTarget(target) {
this.config.components.crossTool.magnetTarget = target;
}
/**
* Adds a new drawer type for cross tool, so you can add your own implementation of cross tool
* (or override existing)
* @param drawerName - an unique drawer type name
* @param drawerImpl - CrossToolTypeDrawer object
*/
registerCrossToolTypeDrawer(drawerName, drawerImpl) {
this.crossToolTypeDrawers[drawerName] = drawerImpl;
}
}