React Diagram Methods

This section describes members used to manipulate the UI component.

See Also

beginUpdate()

Prevents the UI component from refreshing until the endUpdate() method is called.

The beginUpdate() and endUpdate() methods prevent the UI component from excessive updates when you are changing multiple UI component settings at once. After the beginUpdate() method is called, the UI component does not update its UI until the endUpdate() method is called.

See Also

defaultOptions(rule)

Specifies the device-dependent default configuration properties for this component.

Parameters:
rule:

Object

The component's default device properties.

Object structure:
Name Type Description
device

Device

|

Function

Device parameters.
When you specify a function, get information about the current device from the argument. Return true if the properties should be applied to the device.

options

Object

Options to be applied.

defaultOptions is a static method that the UI component class supports. The following code demonstrates how to specify default properties for all instances of the Diagram UI component in an application executed on the desktop.

jQuery
JavaScript
DevExpress.ui.dxDiagram.defaultOptions({ 
    device: { deviceType: "desktop" },
    options: {
        // Here go the Diagram properties
    }
});
Angular
TypeScript
import Diagram, { Properties } from "devextreme/ui/diagram";
// ...
export class AppComponent {
    constructor () {
        Diagram.defaultOptions<Properties>({
            device: { deviceType: "desktop" },
            options: {
                // Here go the Diagram properties
            }
        });
    }
}
Vue
<template>
    <div>
        <DxDiagram id="diagram1" />
        <DxDiagram id="diagram2" />
    </div>
</template>
<script>
import DxDiagram from "devextreme-vue/diagram";
import Diagram from "devextreme/ui/diagram";

Diagram.defaultOptions({
    device: { deviceType: "desktop" },
    options: {
        // Here go the Diagram properties
    }
});

export default {
    components: {
        DxDiagram
    }
}
</script>
React
import dxDiagram from "devextreme/ui/diagram";
import Diagram from "devextreme-react/diagram";

dxDiagram.defaultOptions({
    device: { deviceType: "desktop" },
    options: {
        // Here go the Diagram properties
    }
});

export default function App() {
    return (
        <div>
            <Diagram id="diagram1" />
            <Diagram id="diagram2" />
        </div>
    )
}

dispose()

Disposes of all the resources allocated to the Diagram instance.

jQuery

After calling this method, remove the DOM element associated with the UI component:

JavaScript
$("#myDiagram").dxDiagram("dispose");
$("#myDiagram").remove();
Angular

Use conditional rendering instead of this method:

app.component.html
<dx-diagram ...
    *ngIf="condition">
</dx-diagram>
Vue

Use conditional rendering instead of this method:

App.vue
<template>
    <DxDiagram ...
        v-if="condition">
    </DxDiagram>
</template>

<script>
import DxDiagram from 'devextreme-vue/diagram';

export default {
    components: {
        DxDiagram
    }
}
</script>
React

Use conditional rendering instead of this method:

App.js
import React from 'react';

import Diagram from 'devextreme-react/diagram';

function DxDiagram(props) {
    if (!props.shouldRender) {
        return null;
    }

    return (
        <Diagram ... >    
        </Diagram>
    );
}

class App extends React.Component {
    render() {
        return (
            <DxDiagram shouldRender="condition" />
        );
    }
}
export default App;

element()

Gets the root UI component element.

Return Value:

HTMLElement | jQuery

An HTML element or a jQuery element when you use jQuery.

See Also

endUpdate()

Refreshes the UI component after a call of the beginUpdate() method.

Main article: beginUpdate()

See Also

export()

Exports the diagram data to a JSON object.

Return Value:

String

The diagram data in JSON format.

The export method allows you to save information about a diagram's layout, appearance, and the diagram's shape positions.

The component saves the diagram's data in its own text format. The resulting JSON object contains measurements in twips, regardless of the units or viewUnits property values. Do not modify this JSON object because modified data can load incorrectly.

Use the import method to load this data on demand.

exportTo(format, callback)

Exports the diagram to an image format.

Parameters:
format: 'svg' | 'png' | 'jpg'

The image format.

callback:

Function

A function to be executed after a diagram image is prepared and allows you to save the image.

fitToContent()

Fits the diagram content into the work area. The maximum scale is 100%.

fitToWidth()

Fits the diagram content's width into the work area width. The maximum scale is 100%.

focus()

Sets focus on the UI component.

See Also

getEdgeDataSource()

Returns the DataSource instance.

Return Value:

DataSource

The DataSource instance.

NOTE
This method returns the DataSource instance even if the dataSource property of the layer was given a simple array.
See Also

getInstance(element)

Gets the instance of a UI component found using its DOM node.

Parameters:
element:

Element

|

jQuery

The UI component's container.

Return Value:

Object

The UI component's instance.

getInstance is a static method that the UI component class supports. The following code demonstrates how to get the Diagram instance found in an element with the myDiagram ID:

// Modular approach
import Diagram from "devextreme/ui/diagram";
...
let element = document.getElementById("myDiagram");
let instance = Diagram.getInstance(element) as Diagram;

// Non-modular approach
let element = document.getElementById("myDiagram");
let instance = DevExpress.ui.dxDiagram.getInstance(element);
See Also

getItemById(id)

Returns a shape or connector object specified by its internal identifier.

Parameters:
id:

String

The item identifier.

Return Value:

dxDiagramItem

A dxDiagramItem object descendant (dxDiagramShape or dxDiagramConnector).

When a user pastes or clones several items in a diagram, the control adds the items to the model one by one. For each added item, the requestEditOperation event fires. In the event handler, you can access the processed item. However, if you call the getItemById method to access an attached connector (see the attachedConnectorIds property) or a container's child item (see the containerChildItemIds property), you can get the undefinedresult if the item is not added to the model yet.

getItemByKey(key)

Returns a shape or connector object specified by its key.

Parameters:
key:

Object

The item key.

Return Value:

dxDiagramItem

A dxDiagramItem object descendant (dxDiagramShape or dxDiagramConnector).

An item key specifies the item's identifier in a data source. If a diagram is not bound to a data source, the key value is empty. In this instance, use the GetItemById(id) method to get an item by its internal identifier. An item always has an identifier.

getItems()

Returns an array of diagram items.

Return Value:

Array<dxDiagramItem>

An array of items (shapes and connectors).

The code sample below demonstrates how to get the full collection of diagram items and filter the items that are directly connected to the clicked item.

jQuery
index.js
$(() => {
    $('#diagram').dxDiagram({
        nodes: {
            dataSource: new DevExpress.data.ArrayStore({
                key: 'ID',
                data: employees,
            }),
            keyExpr: 'ID',
            textExpr: 'Full_Name',
            parentKeyExpr: 'Head_ID',
        },
        onItemClick(e) {
            var itemKey = e.item.key;
            var connectors = e.component.getItems().filter(function (item) {
                return item.itemType === "connector" && (item.fromKey === itemKey || item.toKey === itemKey);
            });
            var relativeShapes = [];
            connectors.forEach(element => {
                if (element.fromKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.toKey)); // all child shapes 
                if (element.toKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.fromKey)); // all parent shapes
            });
        },
    }).dxDiagram('instance');
});
Angular
app.component.html
app.component.ts
<dx-diagram #diagram id="diagram" (onItemClick)="itemClickHandler($event)">
    <dxo-nodes [dataSource]="dataSource" keyExpr="ID" textExpr="Full_Name" parentKeyExpr="Head_ID" />
</dx-diagram>
// ...
itemClickHandler(e) {
    var itemKey = e.item.key;
    var connectors = e.component.getItems().filter(function (item) {
        return item.itemType === "connector" && (item.fromKey === itemKey || item.toKey === itemKey);
    });
    var relativeShapes = [];
    connectors.forEach(element => {
        if (element.fromKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.toKey)); // all subsequent shapes 
        if (element.toKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.fromKey)); // all previous shapes
    });
}
// ...
Vue
App.vue
<template>
    <DxDiagram
        id="diagram"
        ref="diagram"
        @item-click="onItemClick"
    >
        <DxNodes
        :data-source="dataSource"
        :key-expr="'ID'"
        :text-expr="'Full_Name'"
        :parent-key-expr="'Head_ID'"
        >
        <DxAutoLayout
            :type="'tree'"
        />
        </DxNodes>
    </DxDiagram>
</template>
<script>
    // ...
    onItemClick(e) {
        var itemKey = e.item.key;
        var connectors = e.component.getItems().filter(function (item) {
            return item.itemType === "connector" && (item.fromKey === itemKey || item.toKey === itemKey);
        });
        var relativeShapes = [];
        connectors.forEach(element => {
            if (element.fromKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.toKey)); // all subsequent shapes 
            if (element.toKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.fromKey)); // all previous shapes
        });
        console.log(relativeShapes);
    },
    // ...
</script>
React
App.js
class App extends React.Component {
    constructor(props) {
        // ...
        onItemClick(e) {
        var itemKey = e.item.key;
        var connectors = e.component.getItems().filter(function (item) {
            return item.itemType === "connector" && (item.fromKey === itemKey || item.toKey === itemKey);
        });
        var relativeShapes = [];
        connectors.forEach(element => {
            if (element.fromKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.toKey)); // all subsequent shapes 
            if (element.toKey === itemKey) relativeShapes.push(e.component.getItemByKey(element.fromKey)); // all previous shapes
        });
        },
        // ...
    }

    render() {
        return (
            <Diagram id="diagram" onItemClick={this.onItemClick}>
                <Nodes dataSource={this.dataSource} keyExpr="ID" textExpr="Full_Name" parentKeyExpr="Head_ID" />
            </Diagram>
        );
    }
}

getNodeDataSource()

Gets the DataSource instance.

Return Value:

DataSource

The DataSource instance.

NOTE
This method returns the DataSource instance even if the dataSource property of the layer was given a simple array.
See Also

getSelectedItems()

Returns an array of selected diagram items.

Return Value:

Array<dxDiagramItem>

An array of items (shapes and connectors).

import(data, updateExistingItemsOnly)

Imports the diagram data.

Parameters:
data:

String

The diagram data in JSON format.

updateExistingItemsOnly?:

Boolean

true to apply diagram data without rewriting diagram items; false to apply diagram data and replace diagram items with items from the data parameter.

The export method allows you to save information about a diagram's layout, appearance, and shape positions. Use the import method to load this data on demand.

The diagram import updates the bound data source's field values. If the values are not changed, the corresponding events (for instance, onInserted and onUpdated) do not fire.

NOTE
The Diagram stores an opened diagram's data in its own text format. Do not modify the data the export method returns because a modified document can be loaded incorrectly.

View Demo

instance()

Gets the UI component's instance. Use it to access other methods of the UI component.

Return Value:

Diagram

This UI component's instance.

See Also

off(eventName)

Detaches all event handlers from a single event.

Parameters:
eventName:

String

The event's name.

Return Value:

Diagram

The object for which this method is called.

See Also

off(eventName, eventHandler)

Detaches a particular event handler from a single event.

Parameters:
eventName:

String

The event's name.

eventHandler:

Function

The event's handler.

Return Value:

Diagram

The object for which this method is called.

See Also

on(eventName, eventHandler)

Subscribes to an event.

Parameters:
eventName:

String

The event's name.

eventHandler:

Function

The event's handler.

Return Value:

Diagram

The object for which this method is called.

Use this method to subscribe to one of the events listed in the Events section.

See Also

on(events)

Subscribes to events.

Parameters:
events:

Object

Events with their handlers: { "eventName1": handler1, "eventName2": handler2, ...}

Return Value:

Diagram

The object for which this method is called.

Use this method to subscribe to several events with one method call. Available events are listed in the Events section.

See Also

option()

Return Value:

Object

The UI component's properties.

option(optionName)

Gets the value of a single property.

Parameters:
optionName:

String

The property's name or full path.

Return Value: any

This property's value.

option(optionName, optionValue)

Updates the value of a single property.

Parameters:
optionName:

String

The property's name or full path.

optionValue: any

This property's new value.

option(options)

Updates the values of several properties.

Parameters:
options:

Object

Options with their new values.

repaint()

Repaints the UI component without reloading data. Call it to update the UI component's markup.

The method repaints Toolbar and re-initializes all its items.

See Also

resetOption(optionName)

Resets a property to its default value.

Parameters:
optionName:

String

A property's name.

See Also

scrollToItem(item)

Scrolls the view area to the specified item.

Parameters:

A diagram item (shape or connector).

setSelectedItems(items)

Selects the specified items.

Parameters:

An array of items to select.

updateToolbox()

Updates the diagram toolboxes.