# Hierarchical Data FlexDiagram

## Content

**<span id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Data-bound</span>**<span id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation"> diagrams that use hierarchical data allow</span> **<span id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">FlexDiagram</span>**<span id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation"> to automatically generate diagrams based on hierarchical structures. Each object in the data source contains one or more child objects. </span>**<span id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">FlexDiagram</span>**<span id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="48d53c1c-f17d-4fa8-a4ea-8563302a490d" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation"> uses these relationships to build a visual hierarchy. This method is effective for representing organizational charts, category hierarchies, and other nested structures.</span>
![](https://cdn.mescius.io/document-site-files/images/cf228eb8-1f57-4b26-8cf2-edbf0f1f38ee/hierarchicaldata.7786b3.png?width=600)

### Create Hierarchical Data **Flex**Diagram

1. <span id="f555421c-3807-4243-87af-4cf0ea834719" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="f555421c-3807-4243-87af-4cf0ea834719" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Define data model by creating a class that represents the hierarchical structure:</span>

```csharp
public class DataItem 
{
  public string Name { get; set; } 
  public DataItem[] Childs { get; set; }
 }
```

Define the data model by creating a class that represents the hierarchical structure.

2. <span id="73323ebe-ca25-46f0-923a-9e6d0a688a35" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="73323ebe-ca25-46f0-923a-9e6d0a688a35" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Create a data source based on that structure.</span>

```csharp
var dataSource = CreateHierarchicalData();
```

3. <span id="82024125-9f0b-45c6-9316-785927a2ee74" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="82024125-9f0b-45c6-9316-785927a2ee74" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Bind the data to the diagram.</span>

```csharp
diagram.DataSource = dataSource;
diagram.Binding = "Type";
diagram.ChildItemsPath = "Items";
```

#### **Hierarchical <span id="0af5ac8e-983f-467e-83d4-f6f657c2d7c9" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="0af5ac8e-983f-467e-83d4-f6f657c2d7c9" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">FlexDiagram Sample</span>**

```csharp
using C1.Diagram;
using C1.Win.Diagram;

namespace FlexDiagramPoC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeDiagram();
        }

        private void InitializeDiagram()
        {
            // Create FlexDiagram instance
            var diagram = new FlexDiagram();
            diagram.Dock = DockStyle.Fill;
            this.Controls.Add(diagram);

            diagram.Direction = DiagramDirection.LeftRight;

            var dataSource = CreateHierarchicalData();

            diagram.DataSource = dataSource;
            diagram.Binding = "Type";
            diagram.ChildItemsPath = "Items";
        }

        public class SalesDataItem
        {
            public string? Type { get; set; }
            public double Sales { get; set; }

            public SalesDataItem[]? Items { get; set; }
        }

        public SalesDataItem[] CreateHierarchicalData()
        {
            var data = new SalesDataItem[]
            {
              new SalesDataItem
              {
                  Type = "Electronics",
                  Items = new SalesDataItem[]
                  {
                      new SalesDataItem
                      {
                          Type = "Camera",
                          Items = new SalesDataItem[]
                          {
                              new SalesDataItem { Type = "Digital", Sales = 45 },
                              new SalesDataItem { Type = "Film", Sales = 23 }
                          }
                      },
                      new SalesDataItem
                      {
                          Type = "Headphones",
                          Items = new SalesDataItem[]
                          {
                              new SalesDataItem { Type = "Earbud", Sales = 67 },
                              new SalesDataItem { Type = "Over-ear", Sales = 89 },
                              new SalesDataItem { Type = "On-ear", Sales = 34 }
                          }
                      }
                  }
              },
              new SalesDataItem
              {
                  Type = "Computers & Tablets",
                  Items = new SalesDataItem[]
                  {
                      new SalesDataItem
                      {
                          Type = "Desktops",
                          Items = new SalesDataItem[]
                          {
                              new SalesDataItem { Type = "All-in-ones", Sales = 56 },
                              new SalesDataItem { Type = "Minis", Sales = 42 }
                          }
                      },
                      new SalesDataItem
                      {
                          Type = "Laptops",
                          Items = new SalesDataItem[]
                          {
                              new SalesDataItem { Type = "2 in 1", Sales = 78 },
                              new SalesDataItem { Type = "Traditional", Sales = 91 }
                          }
                      }
                  }
              }
            };

            return data;
        }
    }
}
```