JavaScript/jQuery Diagram - Custom Shapes

The Diagram UI component provides a collection of built-in shapes. Use the customShapes property to extend this collection with custom shapes.

The toolbox property allows you to add the custom shapes to the toolbox.

A custom shape can be created based on a default shape type or with a custom background image. The type property identifies custom shapes and should be unique.

Shapes with Base Type

Shapes with Base Type

Use the baseType property to specify a base type for a shape. The built-in shape types are shown in the Shape Types section.

View Demo

Diagram control custom shapes

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxi-custom-shape *ngFor="let emp of employees"
  • category="employees"
  • [type]='"employee" + emp.ID'
  • baseType="rectangle"
  • [defaultText]="emp.Full_Name"
  • [allowEditText]="false">
  • </dxi-custom-shape>
  • <dxo-toolbox>
  • <dxi-group category="employees" title="Employees" displayMode="texts"></dxi-group>
  • </dxo-toolbox>
  • </dx-diagram>

You can use the style and textStyle properties to specify the default style settings for a shape and a shape’s text.

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxo-default-item-properties
  • style="fill: yellow;"
  • textStyle="font-size: 14pt;">
  • </dxo-default-item-properties>
  • <!-- ... -->
  • </dx-diagram>

Diagram custom shape styles

Shapes with Custom Background Images

Use the backgroundImageUrl property to specify the custom shape's background image in SVG format.

View Demo

roundedRectangle.svg
  • <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 48 26">
  • <g><rect rx="5" ry="5" x="0.5" y="0.5" width="47" height="25"
  • style="fill:#FFFFFF;stroke:#000000;stroke-width:1px;"/></g>
  • </svg>
app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxi-custom-shape
  • type="Rounded Rectangle"
  • backgroundImageUrl="images/shapes/roundedRectangle.svg"
  • </dxi-custom-shape>
  • </dx-diagram>

The image below shows the result:

Custom Shape Background Image

The following properties allow you to customize the image size and position:

Shape Size

Use the following properties to specify the shape size settings:

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxi-custom-shape
  • <!-- ... -->
  • [defaultWidth]="2"
  • [defaultHeight]="1"
  • </dxi-custom-shape>
  • </dx-diagram>

The image below shows the result:

Custom Shape Size

Shape Inner Image

A custom shape can display an inner image. Use the defaultImageUrl property to specify the image URL.

The following properties allow you to specify the image size and position:

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxi-custom-shape
  • <!-- ... -->
  • defaultImageUrl="images/photo.png"
  • [imageHeight]="0.8"
  • [imageWidth]="0.3"
  • [imageTop]="0.1"
  • [imageLeft]="0.1"
  • </dxi-custom-shape>
  • </dx-diagram>

The image below shows the result:

Custom Shape Inner Image

If the allowEditImage property is set to true, the Diagram context menu displays commands that allow users to change the image.

Custom Shape Image Context Menu

Shape Text

The defaultText property specifies the shape text. Users can change the text if the allowEditText property is set to true.

Use the following properties to specify the size and position of the text container:

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxi-custom-shape
  • <!-- ... -->
  • defaultText="Employee"
  • [textLeft]="0.4"
  • [textWidth]="0.6">
  • </dxi-custom-shape>
  • </dx-diagram>

The image below shows the result:

Custom Shape Text

You can use the textStyle property to specify the default style settings for a shape's text.

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxo-default-item-properties
  • textStyle="font-size: 14pt;">
  • </dxo-default-item-properties>
  • <!-- ... -->
  • </dx-diagram>

The image below shows the result:

Custom Shape Text Size

Connection Points

Use the connectionPoints property to specify a collection of custom connection points for a shape. If the property is not specified, the shape displays the default connection points.

app.component.html
  • <dx-diagram #diagram id="diagram">
  • <dxi-custom-shape
  • <!-- ... -->
  • <dxi-connection-point [x]="0.5" [y]="0"></dxi-connection-point>
  • <dxi-connection-point [x]="0.5" [y]="1"></dxi-connection-point>
  • </dxi-custom-shape>
  • </dx-diagram>

The image below shows the result:

Custom Shape Connection Points