Back to Devexpress

How to: Customize a Layout Algorithm

windowsforms-115759-controls-and-libraries-treemap-layout-algorithms-examples-how-to-customize-a-layout-algorithm.md

latest5.5 KB
Original Source

How to: Customize a Layout Algorithm

  • Nov 13, 2018
  • 2 minutes to read

To change the layout algorithm used by TreeMapControl, specify the TreeMapControl.LayoutAlgorithm property. For all default algorithms, you can configure the fill direction using the TreeMapLayoutAlgorithmBase.Direction.

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LayoutAlgorithmCustomization {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
csharp
using DevExpress.XtraTreeMap;
using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e) {
            InitializeLayoutAlgorithmListBox();
            InitializeLayoutDirectionListBox();
        }

        private void InitializeLayoutAlgorithmListBox() {
            lbLayoutAlgorithm.DataSource = new ITreeMapLayoutAlgorithm[] {
                new TreeMapSliceAndDiceLayoutAlgorithm(),
                new TreeMapSquarifiedLayoutAlgorithm(),
                new TreeMapStripedLayoutAlgorithm()
            };
        }

        private void InitializeLayoutDirectionListBox() {
            lbLayoutDirection.DataSource = Enum.GetValues(typeof(TreeMapLayoutDirection));
        }

        private void SetLayoutAlgorithm() {
            if(lbLayoutAlgorithm.SelectedValue == null) return;
            treeMap.LayoutAlgorithm = lbLayoutAlgorithm.SelectedValue as ITreeMapLayoutAlgorithm;
        }

        private void SetLayoutDirection() {
            if(lbLayoutDirection.SelectedValue == null) return;
            TreeMapLayoutAlgorithmBase layoutAlgorithm = treeMap.LayoutAlgorithm as TreeMapLayoutAlgorithmBase;
            layoutAlgorithm.Direction = (TreeMapLayoutDirection)lbLayoutDirection.SelectedValue;
        }

        private void lbLayoutAlgorithm_SelectedIndexChanged(object sender, EventArgs e) {
            SetLayoutAlgorithm();
            SetLayoutDirection();
        }

        private void lbLayoutDirection_SelectedIndexChanged(object sender, EventArgs e) {
            SetLayoutDirection();
        }
    }
}
vb
Imports DevExpress.XtraTreeMap
Imports System
Imports System.Windows.Forms

Namespace LayoutAlgorithmCustomization
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            InitializeLayoutAlgorithmListBox()
            InitializeLayoutDirectionListBox()
        End Sub

        Private Sub InitializeLayoutAlgorithmListBox()
            lbLayoutAlgorithm.DataSource = New ITreeMapLayoutAlgorithm() { _
                New TreeMapSliceAndDiceLayoutAlgorithm(), _
                New TreeMapSquarifiedLayoutAlgorithm(), _
                New TreeMapStripedLayoutAlgorithm() _
            }
        End Sub

        Private Sub InitializeLayoutDirectionListBox()
            lbLayoutDirection.DataSource = System.Enum.GetValues(GetType(TreeMapLayoutDirection))
        End Sub

        Private Sub SetLayoutAlgorithm()
            If lbLayoutAlgorithm.SelectedValue Is Nothing Then
                Return
            End If
            treeMap.LayoutAlgorithm = TryCast(lbLayoutAlgorithm.SelectedValue, ITreeMapLayoutAlgorithm)
        End Sub

        Private Sub SetLayoutDirection()
            If lbLayoutDirection.SelectedValue Is Nothing Then
                Return
            End If
            Dim layoutAlgorithm As TreeMapLayoutAlgorithmBase = TryCast(treeMap.LayoutAlgorithm, TreeMapLayoutAlgorithmBase)
            layoutAlgorithm.Direction = CType(lbLayoutDirection.SelectedValue, TreeMapLayoutDirection)
        End Sub

        Private Sub lbLayoutAlgorithm_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbLayoutAlgorithm.SelectedIndexChanged
            SetLayoutAlgorithm()
            SetLayoutDirection()
        End Sub

        Private Sub lbLayoutDirection_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbLayoutDirection.SelectedIndexChanged
            SetLayoutDirection()
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace LayoutAlgorithmCustomization
    Friend NotInheritable Class Program

        Private Sub New()
        End Sub

        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace