Skip to main content

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 creation
abstract points: number;
public data: unknown;
// type of drawing, usually each drawing has it's own type
abstract readonly type: DrawingType;
// hide the drawing
hidden?: StringTMap<number>;
// add drawing after clicking 'Enter' button
public 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 reached
updateHiddenProps?: (points: Vector[]) => void;
// returns labels of Y axis
getYAxisLabels?: (
viewModel: DrawingViewModel,
ctx: CanvasRenderingContext2D,
chartBootstrap: ChartWithDrawings,
) => YAxisLabelDrawProps[];
// returns labels of X axis
getXAxisLabels?: (
chart: ChartWithDrawings,
viewModel: DrawingViewModel,
properties: CommonFigureProps,
points: Vector[],
) => Array<XAxisLabel>;
constraint?: FigureConstraint;
// returns key viewpoints values
calculateKeyViewPoints?: (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 moved
customMoveFigureAction?: (
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 figure
onUp?: (viewModel: DrawingViewModel, drawings: DrawingsModel) => void;
disablePointDrag?: boolean;
showPoints?: (viewModel: DrawingViewModel) => boolean;
// return value indicates whether we should commit figure or not
beforeCommit?: (model: DrawingViewModel, drawings: DrawingsModel) => boolean;
// return value indicates whether we should cancel figure or not
beforeCancel?: (model: DrawingModel<DrawingType>, drawings: DrawingsModel) => boolean;
// used in drawing mode to start a new drawing after the old one is finished
getClone(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 appearance
getCursor?: (point: ViewPoint, viewModel: DrawingViewModel, chart: ChartWithDrawings) => CursorType | undefined;
// implementation of drawing logic
abstract 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:

  • constraint and pointConstraint restrict point placement;
  • getCursor controls the pointer over the figure;
  • customMoveFigureAction customizes dragging;
  • onUp runs when pointer interaction ends;
  • getXAxisLabels and getYAxisLabels add 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.