)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize the WinForms TreeMap label font style and label text alignment?

Platform: WinForms |
Control: TreeMap |
Published Date: May 12, 2015 |
Last Revised Date: February 4, 2020

Customize the leaf nodes in treemap

To customize the TreeMap label font style and label text alignment, refer to the following steps.

Step 1:

Add ItemsSource and levels in order to populate the TreeMap.

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        TreeMap1.ItemsSource = data.PopulationDetails;
        TreeMapFlatLevel treeMapFlatLevel1 = new TreeMapFlatLevel();
        treeMapFlatLevel1.GroupPath = "Continent";
        TreeMap1.Levels.Add(treeMapFlatLevel1);
        TreeMap1.LeafItemSettings.LabelPath = "Country";
    }
}

 

Note:

Refer to the sample for PopulationDetails provided for ItemsSource.

Step 2:

You can customize TreeMap label font style and label text alignment by using LeafItemDrawing event. Set e.cancel as true inside the event in order to redefine TreeMap label font style and text alignment.

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        TreeMap1.ItemsSource = data.PopulationDetails;
        TreeMap1.LeafItemDrawing += TreeMap1_LeafItemDrawing;
    }
    void TreeMap1_LeafItemDrawing(object sender, LeafItemDrawingEventArgs e)
    {
        e.Cancel = true;
        StringFormat drawFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
        e.Graphics.FillRectangle(e.Color, e.RectSize);
        e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.White), 1), e.RectSize);
        var font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Pixel, ((byte)(0)));
        e.Graphics.DrawString(e.Label, font, TreeMap1.LabelBrush, e.RectSize, drawFormat);
    } 
}

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        TreeMap1.ItemsSource = data.PopulationDetails;
        TreeMapFlatLevel treeMapFlatLevel1 = new TreeMapFlatLevel();
        treeMapFlatLevel1.GroupPath = "Continent";
        TreeMap1.Levels.Add(treeMapFlatLevel1);
        TreeMap1.LeafItemSettings.LabelPath = "Country";
        TreeMap1.LeafItemDrawing += TreeMap1_LeafItemDrawing;
    }
    void TreeMap1_LeafItemDrawing(object sender, LeafItemDrawingEventArgs e)
    {
        e.Cancel = true;
        StringFormat drawFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
        e.Graphics.FillRectangle(e.Color, e.RectSize);
        e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.White), 1), e.RectSize);
        var font = new Font("Segoe UI", 18F, FontStyle.Regular, GraphicsUnit.Pixel, ((byte)(0)));
        e.Graphics.DrawString(e.Label, font, TreeMap1.LabelBrush, e.RectSize, drawFormat);
    } 
}

The following screenshot illustrates the output.

Treemap label with customized font style and label text alignment

Figure 1: Treemap label with customized font style and label text alignment

Sample: http://www.syncfusion.com/downloads/support/directtrac/138801/TreeMapSample-549397978.zip

Reference link: https://help.syncfusion.com/windowsforms/treemap/customizing-leaf-nodes

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
MyatMin
Jan 02, 2016
chance in VB? Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon