ChartDirector 7.1 (.NET Edition)

Tree Map Colors




This example demonstrates an alternative color scheme for multi-level tree maps.

In the Multi Level Tree Map example, the colors are based on the first level nodes. This example demonstrates how to color based on second level nodes.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\treemapcolors.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class treemapcolors : DemoModule { //Name of demo module public string getName() { return "Tree Map Colors"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 1; } //Main code for creating chart. //Note: the argument chartIndex is unused because this demo only has 1 chart. public void createChart(WinChartViewer viewer, int chartIndex) { // The first level nodes of the tree map. There are 3 nodes. string[] allRegions = {"Alpha", "Beta", "Gamma"}; // Each first level node branches to become 7 second level nodes. string[] energy_types = {"Coal", "Oil", "Gas", "Nuclear", "Hydro", "Solar", "Wind"}; // Colors for the second level nodes. int[] colors = {0x222222, 0x666666, 0x884488, 0xcc0000, 0x3366cc, 0x33cc33, 0x77bbff}; // The data for the 3 groups of second level nodes double[] region0 = {0, 50, 70, 0, 60, 120, 140}; double[] region1 = {200, 50, 30, 65, 60, 40, 40}; double[] region2 = {0, 100, 70, 100, 60, 35, 40}; // Create a Tree Map object of size 600 x 520 pixels TreeMapChart c = new TreeMapChart(600, 520); // Add a title to the chart c.addTitle("Energy Usage by Region", "Arial Bold Italic", 18); // Set the plotarea at (10, 35) and of size 480 x 480 pixels c.setPlotArea(10, 35, 480, 480); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. We do not need to provide data as they will be // computed as the sum of the second level nodes. root.setData(null, allRegions); // Add second level nodes to each of the first level node root.getNode(0).setData(region0, energy_types, colors); root.getNode(1).setData(region1, energy_types, colors); root.getNode(2).setData(region2, energy_types, colors); // 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 with 8pt Arial Bold font in // semi-transparent black color (0x77000000). Put the text at the top left corner of the // cell. nodeConfig.setLabelFormat("{label}", "Arial Bold", 18, 0x77ffffff, Chart.TopLeft); // Set the border color to white (ffffff). Use 2 pixel thick flat border style. nodeConfig.setColors(-1, 0xffffff, Chart.flatBorder(2)); // Get the prototype (template) for the second level nodes. TreeMapNode nodeConfig2 = c.getLevelPrototype(2); // Set the label format for the nodes to show the label and value with 8pt Arial Bold // font. Put the text at the center of the cell. nodeConfig2.setLabelFormat("{label}<*br*>{value}MW", "Arial Bold", 8, 0xffffff, Chart.Center); // Set the border color to white (ffffff) nodeConfig2.setColors(-1, 0xffffff); // Add a legend box at (500, 35) with 12pt Arial font and transparent background and // border. LegendBox b = c.addLegend(500, 35, true, "Arial", 12); b.setBackground(Chart.Transparent, Chart.Transparent); // Add the legend keys for the colors for(int i = 0; i < energy_types.Length; ++i) { b.addKey(energy_types[i], colors[i]); } // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='<*cdml*>[{parent.label}] {label}: {value}MW'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\treemapcolors.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class treemapcolors Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Tree Map Colors" End Function 'Number of charts produced in this demo module Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts Return 1 End Function 'Main code for creating chart. 'Note: the argument chartIndex is unused because this demo only has 1 chart. Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _ Implements DemoModule.createChart ' The first level nodes of the tree map. There are 3 nodes. Dim allRegions() As String = {"Alpha", "Beta", "Gamma"} ' Each first level node branches to become 7 second level nodes. Dim energy_types() As String = {"Coal", "Oil", "Gas", "Nuclear", "Hydro", "Solar", "Wind"} ' Colors for the second level nodes. Dim colors() As Integer = {&H222222, &H666666, &H884488, &Hcc0000, &H3366cc, &H33cc33, _ &H77bbff} ' The data for the 3 groups of second level nodes Dim region0() As Double = {0, 50, 70, 0, 60, 120, 140} Dim region1() As Double = {200, 50, 30, 65, 60, 40, 40} Dim region2() As Double = {0, 100, 70, 100, 60, 35, 40} ' Create a Tree Map object of size 600 x 520 pixels Dim c As TreeMapChart = New TreeMapChart(600, 520) ' Add a title to the chart c.addTitle("Energy Usage by Region", "Arial Bold Italic", 18) ' Set the plotarea at (10, 35) and of size 480 x 480 pixels c.setPlotArea(10, 35, 480, 480) ' 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. We do not need to provide data as they will be computed ' as the sum of the second level nodes. root.setData(Nothing, allRegions) ' Add second level nodes to each of the first level node root.getNode(0).setData(region0, energy_types, colors) root.getNode(1).setData(region1, energy_types, colors) root.getNode(2).setData(region2, energy_types, colors) ' 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 with 8pt Arial Bold font in ' semi-transparent black color (0x77000000). Put the text at the top left corner of the ' cell. nodeConfig.setLabelFormat("{label}", "Arial Bold", 18, &H77ffffff, Chart.TopLeft) ' Set the border color to white (ffffff). Use 2 pixel thick flat border style. nodeConfig.setColors(-1, &Hffffff, Chart.flatBorder(2)) ' Get the prototype (template) for the second level nodes. Dim nodeConfig2 As TreeMapNode = c.getLevelPrototype(2) ' Set the label format for the nodes to show the label and value with 8pt Arial Bold font. ' Put the text at the center of the cell. nodeConfig2.setLabelFormat("{label}<*br*>{value}MW", "Arial Bold", 8, &Hffffff, _ Chart.Center) ' Set the border color to white (ffffff) nodeConfig2.setColors(-1, &Hffffff) ' Add a legend box at (500, 35) with 12pt Arial font and transparent background and border. Dim b As LegendBox = c.addLegend(500, 35, True, "Arial", 12) b.setBackground(Chart.Transparent, Chart.Transparent) ' Add the legend keys for the colors For i As Integer = 0 To UBound(energy_types) b.addKey(energy_types(i), colors(i)) Next ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='<*cdml*>[{parent.label}] {label}: {value}MW'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\treemapcolors.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class treemapcolors : DemoModule { //Name of demo module public string getName() { return "Tree Map Colors"; } //Number of charts produced in this demo module public int getNoOfCharts() { return 1; } //Main code for creating chart. //Note: the argument chartIndex is unused because this demo only has 1 chart. public void createChart(WPFChartViewer viewer, int chartIndex) { // The first level nodes of the tree map. There are 3 nodes. string[] allRegions = {"Alpha", "Beta", "Gamma"}; // Each first level node branches to become 7 second level nodes. string[] energy_types = {"Coal", "Oil", "Gas", "Nuclear", "Hydro", "Solar", "Wind"}; // Colors for the second level nodes. int[] colors = {0x222222, 0x666666, 0x884488, 0xcc0000, 0x3366cc, 0x33cc33, 0x77bbff}; // The data for the 3 groups of second level nodes double[] region0 = {0, 50, 70, 0, 60, 120, 140}; double[] region1 = {200, 50, 30, 65, 60, 40, 40}; double[] region2 = {0, 100, 70, 100, 60, 35, 40}; // Create a Tree Map object of size 600 x 520 pixels TreeMapChart c = new TreeMapChart(600, 520); // Add a title to the chart c.addTitle("Energy Usage by Region", "Arial Bold Italic", 18); // Set the plotarea at (10, 35) and of size 480 x 480 pixels c.setPlotArea(10, 35, 480, 480); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. We do not need to provide data as they will be // computed as the sum of the second level nodes. root.setData(null, allRegions); // Add second level nodes to each of the first level node root.getNode(0).setData(region0, energy_types, colors); root.getNode(1).setData(region1, energy_types, colors); root.getNode(2).setData(region2, energy_types, colors); // 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 with 8pt Arial Bold font in // semi-transparent black color (0x77000000). Put the text at the top left corner of the // cell. nodeConfig.setLabelFormat("{label}", "Arial Bold", 18, 0x77ffffff, Chart.TopLeft); // Set the border color to white (ffffff). Use 2 pixel thick flat border style. nodeConfig.setColors(-1, 0xffffff, Chart.flatBorder(2)); // Get the prototype (template) for the second level nodes. TreeMapNode nodeConfig2 = c.getLevelPrototype(2); // Set the label format for the nodes to show the label and value with 8pt Arial Bold // font. Put the text at the center of the cell. nodeConfig2.setLabelFormat("{label}<*br*>{value}MW", "Arial Bold", 8, 0xffffff, Chart.Center); // Set the border color to white (ffffff) nodeConfig2.setColors(-1, 0xffffff); // Add a legend box at (500, 35) with 12pt Arial font and transparent background and // border. LegendBox b = c.addLegend(500, 35, true, "Arial", 12); b.setBackground(Chart.Transparent, Chart.Transparent); // Add the legend keys for the colors for(int i = 0; i < energy_types.Length; ++i) { b.addKey(energy_types[i], colors[i]); } // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='<*cdml*>[{parent.label}] {label}: {value}MW'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\treemapcolors.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"> // // Page Load event handler // protected void Page_Load(object sender, EventArgs e) { // The first level nodes of the tree map. There are 3 nodes. string[] allRegions = {"Alpha", "Beta", "Gamma"}; // Each first level node branches to become 7 second level nodes. string[] energy_types = {"Coal", "Oil", "Gas", "Nuclear", "Hydro", "Solar", "Wind"}; // Colors for the second level nodes. int[] colors = {0x222222, 0x666666, 0x884488, 0xcc0000, 0x3366cc, 0x33cc33, 0x77bbff}; // The data for the 3 groups of second level nodes double[] region0 = {0, 50, 70, 0, 60, 120, 140}; double[] region1 = {200, 50, 30, 65, 60, 40, 40}; double[] region2 = {0, 100, 70, 100, 60, 35, 40}; // Create a Tree Map object of size 600 x 520 pixels TreeMapChart c = new TreeMapChart(600, 520); // Add a title to the chart c.addTitle("Energy Usage by Region", "Arial Bold Italic", 18); // Set the plotarea at (10, 35) and of size 480 x 480 pixels c.setPlotArea(10, 35, 480, 480); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. We do not need to provide data as they will be computed as // the sum of the second level nodes. root.setData(null, allRegions); // Add second level nodes to each of the first level node root.getNode(0).setData(region0, energy_types, colors); root.getNode(1).setData(region1, energy_types, colors); root.getNode(2).setData(region2, energy_types, colors); // 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 with 8pt Arial Bold font in // semi-transparent black color (0x77000000). Put the text at the top left corner of the cell. nodeConfig.setLabelFormat("{label}", "Arial Bold", 18, 0x77ffffff, Chart.TopLeft); // Set the border color to white (ffffff). Use 2 pixel thick flat border style. nodeConfig.setColors(-1, 0xffffff, Chart.flatBorder(2)); // Get the prototype (template) for the second level nodes. TreeMapNode nodeConfig2 = c.getLevelPrototype(2); // Set the label format for the nodes to show the label and value with 8pt Arial Bold font. Put // the text at the center of the cell. nodeConfig2.setLabelFormat("{label}<*br*>{value}MW", "Arial Bold", 8, 0xffffff, Chart.Center); // Set the border color to white (ffffff) nodeConfig2.setColors(-1, 0xffffff); // Add a legend box at (500, 35) with 12pt Arial font and transparent background and border. LegendBox b = c.addLegend(500, 35, true, "Arial", 12); b.setBackground(Chart.Transparent, Chart.Transparent); // Add the legend keys for the colors for(int i = 0; i < energy_types.Length; ++i) { b.addKey(energy_types[i], colors[i]); } // Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*>[{parent.label}] {label}: {value}MW'"); } </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> </body> </html>

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\treemapcolors.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"> ' ' Page Load event handler ' Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) ' The first level nodes of the tree map. There are 3 nodes. Dim allRegions() As String = {"Alpha", "Beta", "Gamma"} ' Each first level node branches to become 7 second level nodes. Dim energy_types() As String = {"Coal", "Oil", "Gas", "Nuclear", "Hydro", "Solar", "Wind"} ' Colors for the second level nodes. Dim colors() As Integer = {&H222222, &H666666, &H884488, &Hcc0000, &H3366cc, &H33cc33, _ &H77bbff} ' The data for the 3 groups of second level nodes Dim region0() As Double = {0, 50, 70, 0, 60, 120, 140} Dim region1() As Double = {200, 50, 30, 65, 60, 40, 40} Dim region2() As Double = {0, 100, 70, 100, 60, 35, 40} ' Create a Tree Map object of size 600 x 520 pixels Dim c As TreeMapChart = New TreeMapChart(600, 520) ' Add a title to the chart c.addTitle("Energy Usage by Region", "Arial Bold Italic", 18) ' Set the plotarea at (10, 35) and of size 480 x 480 pixels c.setPlotArea(10, 35, 480, 480) ' 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. We do not need to provide data as they will be computed as ' the sum of the second level nodes. root.setData(Nothing, allRegions) ' Add second level nodes to each of the first level node root.getNode(0).setData(region0, energy_types, colors) root.getNode(1).setData(region1, energy_types, colors) root.getNode(2).setData(region2, energy_types, colors) ' 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 with 8pt Arial Bold font in ' semi-transparent black color (0x77000000). Put the text at the top left corner of the cell. nodeConfig.setLabelFormat("{label}", "Arial Bold", 18, &H77ffffff, Chart.TopLeft) ' Set the border color to white (ffffff). Use 2 pixel thick flat border style. nodeConfig.setColors(-1, &Hffffff, Chart.flatBorder(2)) ' Get the prototype (template) for the second level nodes. Dim nodeConfig2 As TreeMapNode = c.getLevelPrototype(2) ' Set the label format for the nodes to show the label and value with 8pt Arial Bold font. Put ' the text at the center of the cell. nodeConfig2.setLabelFormat("{label}<*br*>{value}MW", "Arial Bold", 8, &Hffffff, Chart.Center) ' Set the border color to white (ffffff) nodeConfig2.setColors(-1, &Hffffff) ' Add a legend box at (500, 35) with 12pt Arial font and transparent background and border. Dim b As LegendBox = c.addLegend(500, 35, True, "Arial", 12) b.setBackground(Chart.Transparent, Chart.Transparent) ' Add the legend keys for the colors For i As Integer = 0 To UBound(energy_types) b.addKey(energy_types(i), colors(i)) Next ' Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", _ "title='<*cdml*>[{parent.label}] {label}: {value}MW'") End Sub </script> <html> <head> <script type="text/javascript" src="cdjcv.js"></script> </head> <body> <chart:WebChartViewer id="WebChartViewer1" runat="server" /> </body> </html>

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\TreemapcolorsController.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class TreemapcolorsController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Tree Map Colors"; createChart(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1")); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer) { // The first level nodes of the tree map. There are 3 nodes. string[] allRegions = {"Alpha", "Beta", "Gamma"}; // Each first level node branches to become 7 second level nodes. string[] energy_types = {"Coal", "Oil", "Gas", "Nuclear", "Hydro", "Solar", "Wind"}; // Colors for the second level nodes. int[] colors = {0x222222, 0x666666, 0x884488, 0xcc0000, 0x3366cc, 0x33cc33, 0x77bbff}; // The data for the 3 groups of second level nodes double[] region0 = {0, 50, 70, 0, 60, 120, 140}; double[] region1 = {200, 50, 30, 65, 60, 40, 40}; double[] region2 = {0, 100, 70, 100, 60, 35, 40}; // Create a Tree Map object of size 600 x 520 pixels TreeMapChart c = new TreeMapChart(600, 520); // Add a title to the chart c.addTitle("Energy Usage by Region", "Arial Bold Italic", 18); // Set the plotarea at (10, 35) and of size 480 x 480 pixels c.setPlotArea(10, 35, 480, 480); // Obtain the root of the tree map, which is the entire plot area TreeMapNode root = c.getRootNode(); // Add first level nodes to the root. We do not need to provide data as they will be computed // as the sum of the second level nodes. root.setData(null, allRegions); // Add second level nodes to each of the first level node root.getNode(0).setData(region0, energy_types, colors); root.getNode(1).setData(region1, energy_types, colors); root.getNode(2).setData(region2, energy_types, colors); // 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 with 8pt Arial Bold font in // semi-transparent black color (0x77000000). Put the text at the top left corner of the // cell. nodeConfig.setLabelFormat("{label}", "Arial Bold", 18, 0x77ffffff, Chart.TopLeft); // Set the border color to white (ffffff). Use 2 pixel thick flat border style. nodeConfig.setColors(-1, 0xffffff, Chart.flatBorder(2)); // Get the prototype (template) for the second level nodes. TreeMapNode nodeConfig2 = c.getLevelPrototype(2); // Set the label format for the nodes to show the label and value with 8pt Arial Bold font. // Put the text at the center of the cell. nodeConfig2.setLabelFormat("{label}<*br*>{value}MW", "Arial Bold", 8, 0xffffff, Chart.Center) ; // Set the border color to white (ffffff) nodeConfig2.setColors(-1, 0xffffff); // Add a legend box at (500, 35) with 12pt Arial font and transparent background and border. LegendBox b = c.addLegend(500, 35, true, "Arial", 12); b.setBackground(Chart.Transparent, Chart.Transparent); // Add the legend keys for the colors for(int i = 0; i < energy_types.Length; ++i) { b.addKey(energy_types[i], colors[i]); } // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='<*cdml*>[{parent.label}] {label}: {value}MW'"); } } }

[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>