Customize the Diagram Component

This article contains code samples that demonstrate how to customize the Diagram UI component and its items.

Change the Component's Background Color

Override the following CSS rule to change the Diagram component's background color:

styles.css
  • #diagram .dxdi-canvas .grid {
  • fill: lightGray !important;
  • }

Change the Diagram's background color

Change the Color of Connector Points

Add the following CSS rules to change the color of connector points:

styles.css
  • .dx-diagram .dxdi-control .dxdi-canvas .connection-point:not(.selector) {
  • stroke: red;
  • }
  • .dx-diagram .dxdi-control .dxdi-canvas .connection-point:not(.selector).active {
  • fill: red;
  • }

Change the connector points color

Customize the Context Menu's Items

The example below demonstrates how to show default and custom commands in the context menu depending on the selected item:

  • $(function () {
  • var diagram = $("#diagram").dxDiagram({
  • onSelectionChanged: function(e) {
  • // Displays the "showGrid" and "snapToGrid" commands when a user selects no items
  • if (e.items.length === 0)
  • e.component.option("contextMenu.commands", ["showGrid", "snapToGrid"]);
  • else
  • // Displays the "fontName", "fontSize", and "selectShapes" commands when a user selects a shape
  • if (e.items[0].itemType === "shape")
  • e.component.option("contextMenu.commands", ["fontName", "fontSize", {name: "selectShapes", text: "Select All Shapes"}]);
  • // Displays the "connectorLineStart", "connectorLineEnd", and "selectConnectors" commands when a user selects a connector
  • else
  • e.component.option("contextMenu.commands", ["connectorLineStart", "connectorLineEnd", {name: "selectConnectors", text: "Select All Connectors"}]);
  • },
  • onCustomCommand: function(e) {
  • if (e.name == "selectShapes") {
  • var shapes = e.component.getItems().filter(function(item) {
  • return (item.itemType === "shape")
  • });
  • e.component.setSelectedItems(shapes);
  • }
  • if (e.name == "selectConnectors") {
  • var connectors = e.component.getItems().filter(function(item) {
  • return (item.itemType === "connector")
  • });
  • e.component.setSelectedItems(connectors);
  • }
  • },
  • }).dxDiagram("instance");
  • });

Specify a Command's Location on the Main Toolbar

Use the location property to set the location of a command or separator on the main toolbar. This property accepts one of the following values:

  • center
    Places the command in the center of the main toolbar.

  • before
    Places the command before the central element(s).

  • after
    Places the command after the central element(s).

The Diagram UI component displays commands with the same location property value in the order in which you listed them. When the main toolbar cannot accommodate all commands, the component places excess commands in the overflow menu.

The example below demonstrates how to customize the main toolbar:

index.js
  • $(function() {
  • var diagram = $("#diagram").dxDiagram({
  • mainToolbar: {
  • visible: true,
  • commands: [
  • { name: "undo", location: "before" },
  • { name: "redo", location: "before" },
  • { name: "separator", location: "before" },
  • { name: "copy", location: "center" },
  • { name: "paste", location: "center" },
  • { name: "cut", location: "center" },
  • { name: "separator", location: "center" },
  • { name: "clear", icon: "clearsquare", text: "Clear Diagram", location: "after" }
  • ]
  • },
  • toolbox: {
  • visibility: 'hidden',
  • },
  • historyToolbar: {
  • visible: false
  • },
  • viewToolbar: {
  • visible: false
  • },
  • onCustomCommand: function(e) {
  • if(e.name === "clear") {
  • var result = DevExpress.ui.dialog.confirm("Are you sure you want to clear the diagram? This action cannot be undone.", "Warning");
  • result.done(
  • function(dialogResult) {
  • if(dialogResult) {
  • e.component.import("");
  • }
  • }
  • );
  • }
  • }
  • }).dxDiagram("instance");
  •  
  • $.ajax({
  • url: "data/diagram-flow.json",
  • dataType: "text",
  • success: function(data) {
  • diagram.import(data);
  • }
  • });
  • });

Customize main toolbar

Restrict Operations

The Diagram UI component raises the requestEditOperation event every time a user attempts an edit operation. This article contains code samples that demonstrate how to use this event's parameters to prohibit individual edit operations and customize a shape collection in the toolbox and context toolbox.

Refer to the following section for more information on the requestEditOperation event's parameters: Prohibit Individual Operations.

Prohibit Creating Loops

The example below demonstrates how to prevent users from connecting a shape to itself:

  • $(function() {
  • var diagram = $("#diagram").dxDiagram({
  • onRequestEditOperation: function(e) {
  • if (e.operation === "changeConnection")
  • if (e.args.connector && e.args.connector.fromId === e.args.connector.toId)
  • e.allowed = false;
  • },
  • }).dxDiagram("instance");
  • });

Prohibit Adding Shapes Twice

The example below demonstrates how to prevent users from adding more than one shape of each type to a chart:

  • $(function() {
  • var diagram = $("#diagram").dxDiagram({
  • onRequestEditOperation(e) {
  • if (e.operation === 'addShape') {
  • // Gets types of shapes the chart contains
  • var itemsTypes = e.component.getItems().filter(function(item) {
  • return (item.itemType === "shape") && (item.id !== e.args.shape.id);
  • }).map(a => a.type);
  • // Cancels the operation if the chart contains a shape with the same type as the shape that is about to be added
  • if (itemsTypes.indexOf(e.args.shape.type) !== -1) {
  • e.allowed = false;
  • return;
  • }
  • }
  • },
  • }).dxDiagram("instance");
  • });
See Also

Remove Shapes from Toolboxes

In the example below, the Diagram component updates the shape collection in the toolbox and context toolbox as follows:

  • Removes a shape from these toolboxes after a user adds it to a chart
  • Returns a shape to these toolboxes after a user deletes it from a chart
  • $(function () {
  • var shapeCount = 0;
  • var diagram = $("#diagram").dxDiagram({
  • onOptionChanged: function (e) {
  • // Detects changes of the Diagram model
  • if (e.name === "hasChanges" && e.value) {
  • e.component.option("hasChanges", false);
  • var currentShapeCount = e.component.getItems().filter(function(item) {
  • return (item.itemType ==="shape")
  • }).length;
  • // Updates the toolbox and context toolbox if a shape was added or deleted
  • if (shapeCount !== currentShapeCount) {
  • shapeCount = currentShapeCount;
  • window.setTimeout(function() {
  • e.component.updateToolbox();
  • }, 0);
  • }
  • }
  • },
  • onRequestEditOperation(e) {
  • if (e.operation === "addShapeFromToolbox") {
  • e.component.getItems().forEach(function(item) {
  • // Removes a shape from the toolboxes if the chart contains a shape of this type
  • if (item.itemType === "shape" && item.type === e.args.shapeType)
  • e.allowed = false;
  • });
  • }
  • },
  • })
  • .dxDiagram("instance");
  • });
See Also

Prohibit Moving Shapes Between Containers

The example below demonstrates how to prevent users from moving a shape from one container to another:

  • $(function() {
  • var containerIds = {};
  • var diagram = $("#diagram").dxDiagram({
  • onRequestEditOperation: function(e) {
  • if (e.operation === "moveShape")
  • // Cancels the operation if a user moves a shape outside its container.
  • if (containerIds[e.args.shape.id] !== e.args.shape.containerId)
  • e.allowed = false;
  • },
  • onSelectionChanged: function(e) {
  • e.component.getItems().forEach(item => {containerIds[item.id] = item.containerId;});
  • },
  • }).dxDiagram("instance");
  • });

Customize Shape Collection in the Context Toolbox

The following example demonstrates how to hide shapes in the context toolbox depending on the connector's start node type:

  • $(function() {
  • var currentShapeId;
  • var diagram = $("#diagram").dxDiagram({
  • onRequestEditOperation: function(e) {
  • if (e.operation === "changeConnection" && e.args.connector)
  • // Gets the connector's start node identifier
  • this.currentShapeId = e.args.connector.fromId;
  • if (e.operation === "addShapeFromToolbox") {
  • // Gets the connector's start node type
  • var currentShape = e.component.getItemById(this.currentShapeId);
  • if (e.args.shapeType === "terminator")
  • // If the connector's start node type is "decision"
  • if (currentShape && currentShape.type === "decision")
  • // Hides the "terminator" shape in the context toolbox
  • e.allowed = false;
  • }
  • },
  • contextToolbox: {
  • enabled: true,
  • shapeIconsPerRow: 3,
  • shapes: [ "process", "decision", "terminator" ],
  • },
  • })
  • .dxDiagram("instance");
  • });