Skip to main content

Generate Diagrams from a Data Source

  • 3 minutes to read

The DevExpress Diagram Control allows you to generate diagrams based on a collection of data items. You can generate flat or hierarchical (organizational chart) diagrams.

The DiagramControl uses behavior objects to generate diagram items (shapes, containers, and connectors):

These objects allow you to use the Item Template Designer or Runtime Item Generation to define and configure generated items.

Refer to the following topics for additional information, code samples, and related limitations:

Specify Positions of Generated Items

DiagramDataBindingController and DiagramOrgChartController arrange items based on the LayoutKind property value. The following layout algorithms are available:

You can arrange items manually if none of the algorithms above suits your requirements. To do this, handle the CustomLayoutItems event. In the event handler, set the e.Handled property to true (to disable the default layout algorithm) and arrange items based on custom logic.

Bind Item Positions to Source Properties

If your data source contains information about item locations, you can position generated items according to this information.

View Example: Bind Item Positions to Source Objects

  1. Create a DiagramBinding object in the GenerateItem event handler, bind DiagramItem.Position to a source property, and add this object to the template item’s Bindings collection:

    void DiagramDataBindingController1_GenerateItem(object sender, DiagramGenerateItemEventArgs e) {
        var item = new DiagramShape {
            // ...
        };
        item.Bindings.Add(new DiagramBinding("Content", "PositionString"));
        item.Bindings.Add(new DiagramBinding("Position", "Position", DiagramBindingMode.TwoWay));
        e.Item = item;
    }
    
  2. Handle the CustomLayoutItems event to disable the default layout algorithm.

    void DiagramDataBindingController1_CustomLayoutItems(object sender, DiagramCustomLayoutItemsEventArgs e) {
        e.Handled = true;
    }
    

Fetch and Post Changes at Runtime

You can synchronize diagram items between the diagram UI and the bound collection bidirectionally:

Fetch New Items
Call the Refresh() method to generate new diagram items and rearrange them after you add data items to the source collection.
Post Created Items

Handle the DiagramItemAdding event to add data items to the bound collection when a user creates new items in the UI. The event handler allows you to create a new data item instance or cancel the action based on custom logic:

void DiagramDataBindingBehavior_DiagramItemAdding(object sender, DevExpress.XtraDiagram.DiagramDiagramItemAddingEventArgs e) {
    if (e.Item is DiagramShape)
        e.DataItem = new ClassData();
    if (e.Item is DiagramContainer)
        e.Cancel = true;
}

Note

Do not add items directly to the DiagramControl.Items collection.

Examples