Custom drawings
Custom drawings are interactive drawing tools defined by the host application. They can have key points, respond to selection and dragging, expose settings, and be saved with chart state.
Its built-in drawing type list. Supply the figure at runtime with isCustom: true.
Implement AbstractFigure
All drawing figures extend AbstractFigure:
export abstract class AbstractFigure<T> {// number of points being used in the process of drawing creationabstract points: number;public data: unknown;// type of drawing, usually each drawing has it's own typeabstract readonly type: DrawingType;// hide the drawinghidden?: StringTMap<number>;// add drawing after clicking 'Enter' buttonpublic readonly commitOnEnter: boolean = false;// additional vector to drawings which contain text (TextNote, Callout)textToolPoint?: Vector;// edit mode turns off text container to drawings which contain text (TextNote, Callout)isEditing?: boolean;// update props when drawing is hidden and can't be reachedupdateHiddenProps?: (points: Vector[]) => void;// returns labels of Y axisgetYAxisLabels?: (viewModel: DrawingViewModel,ctx: CanvasRenderingContext2D,chartBootstrap: ChartWithDrawings,) => YAxisLabelDrawProps[];// returns labels of X axisgetXAxisLabels?: (chart: ChartWithDrawings,viewModel: DrawingViewModel,properties: CommonFigureProps,points: Vector[],) => Array<XAxisLabel>;constraint?: FigureConstraint;// returns key viewpoints valuescalculateKeyViewPoints?: (points: ViewPoint[]) => ViewPoint[];resize?: (viewModel: DrawingViewModel, movedPoint: PointEvent) => void;rotate?: (viewModel: DrawingViewModel, movedPoint: PointEvent, match: DragDrawingInfo) => void;borderMatchFunction?: (viewModel: DrawingViewModel,initialPoint: IndexValuePoint,chartBootstrap: ChartWithDrawings,) => boolean;// setup some custom action when figure is being movedcustomMoveFigureAction?: (viewModel: DrawingViewModel,chartBootstrap: ChartWithDrawings,drawings: DrawingsModel,initial: IndexValuePoint,initialValues: IndexValuePoint[],point: PointEvent,) => void;wheelFigureAction?: (increment: number) => void;// setup some custom action when mouse up event is triggered for current figureonUp?: (viewModel: DrawingViewModel, drawings: DrawingsModel) => void;disablePointDrag?: boolean;showPoints?: (viewModel: DrawingViewModel) => boolean;// return value indicates whether we should commit figure or notbeforeCommit?: (model: DrawingViewModel, drawings: DrawingsModel) => boolean;// return value indicates whether we should cancel figure or notbeforeCancel?: (model: DrawingModel<DrawingType>, drawings: DrawingsModel) => boolean;// used in drawing mode to start a new drawing after the old one is finishedgetClone(model: DrawingModel<DrawingType>): NullableDrawingModel {return cloneUnsafe(model);}pointConstraint?: (point: PointEvent, viewModel: DrawingViewModel, chartBootstrap: ChartWithDrawings) => boolean;downMoveHandler?: (point: PointEvent, viewModel: DrawingViewModel, drawings: DrawingsModel) => void;isDisallowed?: (model: DrawingModel<DrawingType>, pointEvent: PointEvent) => boolean;// set a cursor appearancegetCursor?: (point: ViewPoint, viewModel: DrawingViewModel, chart: ChartWithDrawings) => CursorType | undefined;// implementation of drawing logicabstract draw(viewModel: DrawingViewModel,ctx: ExtendedContext,properties: T,chartBootstrap: ChartWithDrawings,paneComponent: PaneComponent,drawings: DrawingsModel,): void;drawPoints?(vm: DrawingViewModel,ctx: ExtendedContext,chartConfig: FullChartConfigDrawings,drawings: DrawingsModel,radiusExtension: number,): void;toString() {return this.type;}}
Implement these members:
points— the number of key points needed to complete the drawing;type— the figure's rendering primitive identifier;draw()— canvas rendering logic.
Triangle example
The following figure renders a three-point triangle. It can live anywhere in the host application.
export class Triangle extends AbstractFigure<unknown> {public points: number = 3;public type: DrawingType = 'trend';draw(viewModel: DrawingViewModel, ctx: ExtendedContext, properties: unknown): void {if (!isTriangleProperties(properties)) {return;}const points = viewModel.keyViewPoints;const lineProperties = getLineProperties(viewModel.model, properties);if (points.length === 2) {ctx.prepareStroke(lineProperties);ctx.beginPath();ctx.context.moveTo(points[0].x, points[0].y);ctx.context.lineTo(points[1].x, points[1].y);ctx.stroke();}if (points.length === 3) {if (getProperty(properties.style, canvasPropertiesMap.PROP_FILL_BACKGROUND, true)) {ctx.prepareFill(lineProperties);ctx.beginPath();this.addTrianglePath(points[0], points[1], points[2], ctx);ctx.fill();}ctx.prepareStroke(lineProperties);ctx.beginPath();this.addTrianglePath(points[0], points[1], points[2], ctx);ctx.stroke();}}addTrianglePath = (a: Vector, b: Vector, c: Vector, context: ExtendedContext): void => {context.context.moveTo(a.x, a.y);context.context.lineTo(b.x, b.y);context.context.lineTo(c.x, c.y);context.context.lineTo(a.x, a.y);context.closePath();};}
Start a custom drawing
Call DrawingsComponent.startDrawing() with the custom type ID, properties, figure instance, and isCustom: true. The type ID is application-defined and does not need to appear in the built-in DrawingType list.
export const startTriangleDrawing = (chart: ChartWithDrawings): void => {const triangleDrawingConfig: CustomNewDrawingConfig = {properties: {line: {lineColor: '#FFAA00',lineWidth: 1,lineDash: [],},style: {fillBackground: true,},showTime: true,showPrice: true,},type: 'triangle',figure: new Triangle(),isCustom: true,};chart.drawings.startDrawing(triangleDrawingConfig);}
For a completed drawing loaded from storage, use the equivalent custom DrawingConfig shape with keyPoints.
Optional figure behavior
AbstractFigure also provides optional hooks for interaction:
constraintandpointConstraintrestrict point placement;getCursorcontrols the pointer over the figure;customMoveFigureActioncustomizes dragging;onUpruns when pointer interaction ends;getXAxisLabelsandgetYAxisLabelsadd axis labels.
DXcharts React
DXcharts React adds runtime registration, toolbar metadata, settings UI, persistence providers, and APIs for adding or removing instances. See Custom Drawings.
Custom drawing or custom drawer?
Use a custom drawing when users need to create, select, move, configure, or persist an object. If you only need to render application-owned graphics on a canvas, use a Custom drawer instead.