ChartDirector 7.1 (.NET Edition)

Tree Map Layout


        

The example demonstrates various tree map layout methods configurable using TreeMapNode.setLayoutMethod.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\treemaplayout.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class treemaplayout : DemoModule { //Name of demo module public string getName() { return "Tree Map Layout"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 5; } //Main code for creating charts public void createChart(WinChartViewer viewer, int chartIndex) { // Random data for the tree map RanSeries r = new RanSeries(3); double[] data = r.getSeries(20, 20, 400); // Create a Tree Map object of size 300 x 300 pixels TreeMapChart c = new TreeMapChart(300, 300); c.setPlotArea(20, 20, 260, 260); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. root.setData(data); if (chartIndex == 0) { // Squarity - Layout the cells so that they are as square as possible. c.addTitle("Squarify"); root.setLayoutMethod(Chart.TreeMapSquarify); } else if (chartIndex == 1) { // Strip layout - Cells flow from left to right, top to bottom. The number of cells // in each row is such that they will be as close to a square as possible. (The // setLayoutMethod also supports other flow directions.) c.addTitle("Strip"); root.setLayoutMethod(Chart.TreeMapStrip); } else if (chartIndex == 2) { // Binary Split by Size - Split the cells into left/right groups so that their size // are as close as possible. For each group, split the cells into top/bottom groups // using the same criteria. Continue until each group contains one cell. (The // setLayoutMethod also supports other flow directions.) c.addTitle("Binary Split by Size"); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } else if (chartIndex == 3) { // Binary Split by Count - Same as "Binary Split by Size", except that the cell // count (instead of the size) is used to split the cells. c.addTitle("Binary Split by Count"); root.setLayoutMethod(Chart.TreeMapBinaryByCount); } else if (chartIndex == 4) { // Binary Split by Size (Sorted) - Same as "Binary Split by Size" except the cells // are sorted first. c.addTitle("Binary Split by Size (Sorted)"); root.setSorting(-1); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } // Get the prototype (template) for the first level nodes. TreeMapNode nodeConfig = c.getLevelPrototype(1); // Set the label format for the nodes to show the label and value with 8pt Arial Bold // font in black color (000000) and center aligned in the node. nodeConfig.setLabelFormat("{index}", "Arial", 8, 0x000000, Chart.Center); // Set automatic fill color and white (ffffff) border nodeConfig.setColors(Chart.DataColor, 0xffffff); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='<*cdml*><*font=bold*>[{index}]<*/font*> {value|2}'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\treemaplayout.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class treemaplayout Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Tree Map Layout" End Function 'Number of charts produced in this demo module Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts Return 5 End Function 'Main code for creating charts Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _ Implements DemoModule.createChart ' Random data for the tree map Dim r As RanSeries = New RanSeries(3) Dim data() As Double = r.getSeries(20, 20, 400) ' Create a Tree Map object of size 300 x 300 pixels Dim c As TreeMapChart = New TreeMapChart(300, 300) c.setPlotArea(20, 20, 260, 260) ' Obtain the root of the tree map, which is the entire plot area Dim root As TreeMapNode = c.getRootNode() ' Add first level nodes to the root. root.setData(data) If chartIndex = 0 Then ' Squarity - Layout the cells so that they are as square as possible. c.addTitle("Squarify") root.setLayoutMethod(Chart.TreeMapSquarify) ElseIf chartIndex = 1 Then ' Strip layout - Cells flow from left to right, top to bottom. The number of cells in ' each row is such that they will be as close to a square as possible. (The ' setLayoutMethod also supports other flow directions.) c.addTitle("Strip") root.setLayoutMethod(Chart.TreeMapStrip) ElseIf chartIndex = 2 Then ' Binary Split by Size - Split the cells into left/right groups so that their size are ' as close as possible. For each group, split the cells into top/bottom groups using the ' same criteria. Continue until each group contains one cell. (The setLayoutMethod also ' supports other flow directions.) c.addTitle("Binary Split by Size") root.setLayoutMethod(Chart.TreeMapBinaryBySize) ElseIf chartIndex = 3 Then ' Binary Split by Count - Same as "Binary Split by Size", except that the cell count ' (instead of the size) is used to split the cells. c.addTitle("Binary Split by Count") root.setLayoutMethod(Chart.TreeMapBinaryByCount) ElseIf chartIndex = 4 Then ' Binary Split by Size (Sorted) - Same as "Binary Split by Size" except the cells are ' sorted first. c.addTitle("Binary Split by Size (Sorted)") root.setSorting(-1) root.setLayoutMethod(Chart.TreeMapBinaryBySize) End If ' Get the prototype (template) for the first level nodes. Dim nodeConfig As TreeMapNode = c.getLevelPrototype(1) ' Set the label format for the nodes to show the label and value with 8pt Arial Bold font in ' black color (000000) and center aligned in the node. nodeConfig.setLabelFormat("{index}", "Arial", 8, &H000000, Chart.Center) ' Set automatic fill color and white (ffffff) border nodeConfig.setColors(Chart.DataColor, &Hffffff) ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='<*cdml*><*font=bold*>[{index}]<*/font*> {value|2}'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\treemaplayout.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class treemaplayout : DemoModule { //Name of demo module public string getName() { return "Tree Map Layout"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 5; } //Main code for creating charts public void createChart(WPFChartViewer viewer, int chartIndex) { // Random data for the tree map RanSeries r = new RanSeries(3); double[] data = r.getSeries(20, 20, 400); // Create a Tree Map object of size 300 x 300 pixels TreeMapChart c = new TreeMapChart(300, 300); c.setPlotArea(20, 20, 260, 260); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. root.setData(data); if (chartIndex == 0) { // Squarity - Layout the cells so that they are as square as possible. c.addTitle("Squarify"); root.setLayoutMethod(Chart.TreeMapSquarify); } else if (chartIndex == 1) { // Strip layout - Cells flow from left to right, top to bottom. The number of cells // in each row is such that they will be as close to a square as possible. (The // setLayoutMethod also supports other flow directions.) c.addTitle("Strip"); root.setLayoutMethod(Chart.TreeMapStrip); } else if (chartIndex == 2) { // Binary Split by Size - Split the cells into left/right groups so that their size // are as close as possible. For each group, split the cells into top/bottom groups // using the same criteria. Continue until each group contains one cell. (The // setLayoutMethod also supports other flow directions.) c.addTitle("Binary Split by Size"); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } else if (chartIndex == 3) { // Binary Split by Count - Same as "Binary Split by Size", except that the cell // count (instead of the size) is used to split the cells. c.addTitle("Binary Split by Count"); root.setLayoutMethod(Chart.TreeMapBinaryByCount); } else if (chartIndex == 4) { // Binary Split by Size (Sorted) - Same as "Binary Split by Size" except the cells // are sorted first. c.addTitle("Binary Split by Size (Sorted)"); root.setSorting(-1); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } // Get the prototype (template) for the first level nodes. TreeMapNode nodeConfig = c.getLevelPrototype(1); // Set the label format for the nodes to show the label and value with 8pt Arial Bold // font in black color (000000) and center aligned in the node. nodeConfig.setLabelFormat("{index}", "Arial", 8, 0x000000, Chart.Center); // Set automatic fill color and white (ffffff) border nodeConfig.setColors(Chart.DataColor, 0xffffff); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='<*cdml*><*font=bold*>[{index}]<*/font*> {value|2}'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\treemaplayout.aspx
(Click here on how to convert this code to code-behind style.)
<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="ChartDirector" %> <%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %> <!DOCTYPE html> <script runat="server"> // // Create chart // protected void createChart(WebChartViewer viewer, int chartIndex) { // Random data for the tree map RanSeries r = new RanSeries(3); double[] data = r.getSeries(20, 20, 400); // Create a Tree Map object of size 300 x 300 pixels TreeMapChart c = new TreeMapChart(300, 300); c.setPlotArea(20, 20, 260, 260); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. root.setData(data); if (chartIndex == 0) { // Squarity - Layout the cells so that they are as square as possible. c.addTitle("Squarify"); root.setLayoutMethod(Chart.TreeMapSquarify); } else if (chartIndex == 1) { // Strip layout - Cells flow from left to right, top to bottom. The number of cells in each // row is such that they will be as close to a square as possible. (The setLayoutMethod also // supports other flow directions.) c.addTitle("Strip"); root.setLayoutMethod(Chart.TreeMapStrip); } else if (chartIndex == 2) { // Binary Split by Size - Split the cells into left/right groups so that their size are as // close as possible. For each group, split the cells into top/bottom groups using the same // criteria. Continue until each group contains one cell. (The setLayoutMethod also supports // other flow directions.) c.addTitle("Binary Split by Size"); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } else if (chartIndex == 3) { // Binary Split by Count - Same as "Binary Split by Size", except that the cell count // (instead of the size) is used to split the cells. c.addTitle("Binary Split by Count"); root.setLayoutMethod(Chart.TreeMapBinaryByCount); } else if (chartIndex == 4) { // Binary Split by Size (Sorted) - Same as "Binary Split by Size" except the cells are // sorted first. c.addTitle("Binary Split by Size (Sorted)"); root.setSorting(-1); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } // Get the prototype (template) for the first level nodes. TreeMapNode nodeConfig = c.getLevelPrototype(1); // Set the label format for the nodes to show the label and value with 8pt Arial Bold font in // black color (000000) and center aligned in the node. nodeConfig.setLabelFormat("{index}", "Arial", 8, 0x000000, Chart.Center); // Set automatic fill color and white (ffffff) border nodeConfig.setColors(Chart.DataColor, 0xffffff); // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*><*b*>[{index}]<*/b*> {value|2}'"); } // // Page Load event handler // protected void Page_Load(object sender, EventArgs e) { createChart(WebChartViewer0, 0); createChart(WebChartViewer1, 1); createChart(WebChartViewer2, 2); createChart(WebChartViewer3, 3); createChart(WebChartViewer4, 4); } </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer0" runat="server" /> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> <chart:WebChartViewer id="WebChartViewer2" runat="server" /> <chart:WebChartViewer id="WebChartViewer3" runat="server" /> <chart:WebChartViewer id="WebChartViewer4" runat="server" /> </body> </html>

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\treemaplayout.aspx
(Click here on how to convert this code to code-behind style.)
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="ChartDirector" %> <%@ Register TagPrefix="chart" Namespace="ChartDirector" Assembly="netchartdir" %> <!DOCTYPE html> <script runat="server"> ' ' Create chart ' Protected Sub createChart(viewer As WebChartViewer, chartIndex As Integer) ' Random data for the tree map Dim r As RanSeries = New RanSeries(3) Dim data() As Double = r.getSeries(20, 20, 400) ' Create a Tree Map object of size 300 x 300 pixels Dim c As TreeMapChart = New TreeMapChart(300, 300) c.setPlotArea(20, 20, 260, 260) ' Obtain the root of the tree map, which is the entire plot area Dim root As TreeMapNode = c.getRootNode() ' Add first level nodes to the root. root.setData(data) If chartIndex = 0 Then ' Squarity - Layout the cells so that they are as square as possible. c.addTitle("Squarify") root.setLayoutMethod(Chart.TreeMapSquarify) ElseIf chartIndex = 1 Then ' Strip layout - Cells flow from left to right, top to bottom. The number of cells in each ' row is such that they will be as close to a square as possible. (The setLayoutMethod also ' supports other flow directions.) c.addTitle("Strip") root.setLayoutMethod(Chart.TreeMapStrip) ElseIf chartIndex = 2 Then ' Binary Split by Size - Split the cells into left/right groups so that their size are as ' close as possible. For each group, split the cells into top/bottom groups using the same ' criteria. Continue until each group contains one cell. (The setLayoutMethod also supports ' other flow directions.) c.addTitle("Binary Split by Size") root.setLayoutMethod(Chart.TreeMapBinaryBySize) ElseIf chartIndex = 3 Then ' Binary Split by Count - Same as "Binary Split by Size", except that the cell count ' (instead of the size) is used to split the cells. c.addTitle("Binary Split by Count") root.setLayoutMethod(Chart.TreeMapBinaryByCount) ElseIf chartIndex = 4 Then ' Binary Split by Size (Sorted) - Same as "Binary Split by Size" except the cells are sorted ' first. c.addTitle("Binary Split by Size (Sorted)") root.setSorting(-1) root.setLayoutMethod(Chart.TreeMapBinaryBySize) End If ' Get the prototype (template) for the first level nodes. Dim nodeConfig As TreeMapNode = c.getLevelPrototype(1) ' Set the label format for the nodes to show the label and value with 8pt Arial Bold font in ' black color (000000) and center aligned in the node. nodeConfig.setLabelFormat("{index}", "Arial", 8, &H000000, Chart.Center) ' Set automatic fill color and white (ffffff) border nodeConfig.setColors(Chart.DataColor, &Hffffff) ' Output the chart viewer.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*><*b*>[{index}]<*/b*> {value|2}'") End Sub ' ' Page Load event handler ' Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) createChart(WebChartViewer0, 0) createChart(WebChartViewer1, 1) createChart(WebChartViewer2, 2) createChart(WebChartViewer3, 3) createChart(WebChartViewer4, 4) End Sub </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer0" runat="server" /> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> <chart:WebChartViewer id="WebChartViewer2" runat="server" /> <chart:WebChartViewer id="WebChartViewer3" runat="server" /> <chart:WebChartViewer id="WebChartViewer4" runat="server" /> </body> </html>

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\TreemaplayoutController.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class TreemaplayoutController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Tree Map Layout"; // This example contains 5 charts. ViewBag.Viewer = new RazorChartViewer[5]; for (int i = 0; i < ViewBag.Viewer.Length; ++i) createChart(ViewBag.Viewer[i] = new RazorChartViewer(HttpContext, "chart" + i), i); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer, int chartIndex) { // Random data for the tree map RanSeries r = new RanSeries(3); double[] data = r.getSeries(20, 20, 400); // Create a Tree Map object of size 300 x 300 pixels TreeMapChart c = new TreeMapChart(300, 300); c.setPlotArea(20, 20, 260, 260); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. root.setData(data); if (chartIndex == 0) { // Squarity - Layout the cells so that they are as square as possible. c.addTitle("Squarify"); root.setLayoutMethod(Chart.TreeMapSquarify); } else if (chartIndex == 1) { // Strip layout - Cells flow from left to right, top to bottom. The number of cells in // each row is such that they will be as close to a square as possible. (The // setLayoutMethod also supports other flow directions.) c.addTitle("Strip"); root.setLayoutMethod(Chart.TreeMapStrip); } else if (chartIndex == 2) { // Binary Split by Size - Split the cells into left/right groups so that their size are // as close as possible. For each group, split the cells into top/bottom groups using the // same criteria. Continue until each group contains one cell. (The setLayoutMethod also // supports other flow directions.) c.addTitle("Binary Split by Size"); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } else if (chartIndex == 3) { // Binary Split by Count - Same as "Binary Split by Size", except that the cell count // (instead of the size) is used to split the cells. c.addTitle("Binary Split by Count"); root.setLayoutMethod(Chart.TreeMapBinaryByCount); } else if (chartIndex == 4) { // Binary Split by Size (Sorted) - Same as "Binary Split by Size" except the cells are // sorted first. c.addTitle("Binary Split by Size (Sorted)"); root.setSorting(-1); root.setLayoutMethod(Chart.TreeMapBinaryBySize); } // Get the prototype (template) for the first level nodes. TreeMapNode nodeConfig = c.getLevelPrototype(1); // Set the label format for the nodes to show the label and value with 8pt Arial Bold font in // black color (000000) and center aligned in the node. nodeConfig.setLabelFormat("{index}", "Arial", 8, 0x000000, Chart.Center); // Set automatic fill color and white (ffffff) border nodeConfig.setColors(Chart.DataColor, 0xffffff); // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*><*b*>[{index}]<*/b*> {value|2}'") ; } } }

[ASP.NET MVC - View] NetMvcCharts\Views\Shared\ChartView.cshtml
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> <style> @ViewBag.Style </style> @Scripts.Render("~/Scripts/cdjcv.js") </head> <body style="margin:5px 0px 0px 5px"> <div style="font:bold 18pt verdana;"> @ViewBag.Title </div> <hr style="border:solid 1px #000080; background:#000080" /> <div> @{ if (ViewBag.Viewer is Array) { // Display multiple charts for (int i = 0; i < ViewBag.Viewer.Length; ++i) { @:@Html.Raw(ViewBag.Viewer[i].RenderHTML()) } } else { // Display one chart only @:@Html.Raw(ViewBag.Viewer.RenderHTML()) } } </div> </body> </html>