How to customize the WinForms TreeMap label font style and label text alignment?
Customize the leaf nodes in treemap
To customize the WinForms 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"; } }
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.
Figure 1: Treemap label with customized font style and label text alignment
Sample: https://www.syncfusion.com/downloads/support/directtrac/138801/TreeMapSample-549397978.zip
Reference link: https://help.syncfusion.com/windowsforms/treemap/customizing-leaf-nodes
Conclusion
I hope you enjoyed learning about how to customize the WinForms TreeMap label font style and label text alignment.
You can refer to our WinForms TreeMap’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms TreeMap documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms TreeMap and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!