ChartDirector 7.1 (.NET Edition)

Side Label Layout




This example demonstrates the "side label layout" method for positioning the sector labels. It also demonstrates metallic background colors, and sector labels with glass shading effect and rounded corners.

With "side label layout", the sector labels are positioned on the left and right sides of the chart. In contrast, all previous examples use "circular label layout", in which the labels are positioned circularly around the pie.

"Side label layout" has the advantages that the labels will automatically shift up and down to avoid overlapping. In the above chart, there are a number of small sectors at the bottom-right of the pie. Note that the labels are shifted to avoid overlapping.

This example demonstrates a technique for further improving label layout by adjusting the start angle of the first sector using PieChart.setStartAngle.

If the data have a lot of small values and are sorted, the small sectors will be crowded together. For these cases, label layout will be optimal (with the least shifting required) if the small sectors are at the left or right side of the pie, rather than at the top or bottom.

If the data are in descending order (like this example), a start angle of 135 degrees with clockwise sector layout can be used to put the small sectors at approximately 45 - 135 degrees (the right side of the pie). If the data are in ascending order, a start angle of 45 degrees with clockwise sector layout can be used to achieve similar effects.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\sidelabelpie.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class sidelabelpie : DemoModule { //Name of demo module public string getName() { return "Side Label Layout"; } //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 data for the pie chart double[] data = {35, 30, 25, 7, 6, 5, 4, 3, 2, 1}; // The labels for the pie chart string[] labels = {"Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", "Insurance", "Licenses", "Transport", "Interest"}; // Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 // pixel 3D border PieChart c = new PieChart(560, 270, Chart.goldColor(), -1, 1); // Add a title box using 15pt Times Bold Italic font and metallic pink background color c.addTitle("Project Cost Breakdown", "Times New Roman Bold Italic", 15).setBackground( Chart.metalColor(0xff9999)); // Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110); // Draw the pie in 3D with 20 pixels 3D depth c.set3D(20); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label box background color the same as the sector color, with glass effect, // and with 5 pixels rounded corners ChartDirector.TextBox t = c.setLabelStyle(); t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect()); t.setRoundedCorners(5); // Set the border color of the sector the same color as the fill color. Set the line // color of the join line to black (0x0) c.setLineColor(Chart.SameAsMainColor, 0x000000); // Set the start angle to 135 degrees may improve layout when there are many small // sectors at the end of the data array (that is, data sorted in descending order). It // is because this makes the small sectors position near the horizontal axis, where the // text label has the least tendency to overlap. For data sorted in ascending order, a // start angle of 45 degrees can be used instead. c.setStartAngle(135); // Set the pie data and the pie labels c.setData(data, labels); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{label}: US${value}K ({percent}%)'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\sidelabelpie.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class sidelabelpie Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Side Label Layout" 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 data for the pie chart Dim data() As Double = {35, 30, 25, 7, 6, 5, 4, 3, 2, 1} ' The labels for the pie chart Dim labels() As String = {"Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", _ "Insurance", "Licenses", "Transport", "Interest"} ' Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 pixel ' 3D border Dim c As PieChart = New PieChart(560, 270, Chart.goldColor(), -1, 1) ' Add a title box using 15pt Times Bold Italic font and metallic pink background color c.addTitle("Project Cost Breakdown", "Times New Roman Bold Italic", 15).setBackground( _ Chart.metalColor(&Hff9999)) ' Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110) ' Draw the pie in 3D with 20 pixels 3D depth c.set3D(20) ' Use the side label layout method c.setLabelLayout(Chart.SideLayout) ' Set the label box background color the same as the sector color, with glass effect, and ' with 5 pixels rounded corners Dim t As ChartDirector.TextBox = c.setLabelStyle() t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect()) t.setRoundedCorners(5) ' Set the border color of the sector the same color as the fill color. Set the line color of ' the join line to black (0x0) c.setLineColor(Chart.SameAsMainColor, &H000000) ' Set the start angle to 135 degrees may improve layout when there are many small sectors at ' the end of the data array (that is, data sorted in descending order). It is because this ' makes the small sectors position near the horizontal axis, where the text label has the ' least tendency to overlap. For data sorted in ascending order, a start angle of 45 degrees ' can be used instead. c.setStartAngle(135) ' Set the pie data and the pie labels c.setData(data, labels) ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='{label}: US${value}K ({percent}%)'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\sidelabelpie.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class sidelabelpie : DemoModule { //Name of demo module public string getName() { return "Side Label Layout"; } //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 data for the pie chart double[] data = {35, 30, 25, 7, 6, 5, 4, 3, 2, 1}; // The labels for the pie chart string[] labels = {"Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", "Insurance", "Licenses", "Transport", "Interest"}; // Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 // pixel 3D border PieChart c = new PieChart(560, 270, Chart.goldColor(), -1, 1); // Add a title box using 15pt Times Bold Italic font and metallic pink background color c.addTitle("Project Cost Breakdown", "Times New Roman Bold Italic", 15).setBackground( Chart.metalColor(0xff9999)); // Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110); // Draw the pie in 3D with 20 pixels 3D depth c.set3D(20); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label box background color the same as the sector color, with glass effect, // and with 5 pixels rounded corners ChartDirector.TextBox t = c.setLabelStyle(); t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect()); t.setRoundedCorners(5); // Set the border color of the sector the same color as the fill color. Set the line // color of the join line to black (0x0) c.setLineColor(Chart.SameAsMainColor, 0x000000); // Set the start angle to 135 degrees may improve layout when there are many small // sectors at the end of the data array (that is, data sorted in descending order). It // is because this makes the small sectors position near the horizontal axis, where the // text label has the least tendency to overlap. For data sorted in ascending order, a // start angle of 45 degrees can be used instead. c.setStartAngle(135); // Set the pie data and the pie labels c.setData(data, labels); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{label}: US${value}K ({percent}%)'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\sidelabelpie.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 data for the pie chart double[] data = {35, 30, 25, 7, 6, 5, 4, 3, 2, 1}; // The labels for the pie chart string[] labels = {"Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", "Insurance", "Licenses", "Transport", "Interest"}; // Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 pixel 3D // border PieChart c = new PieChart(560, 270, Chart.goldColor(), -1, 1); // Add a title box using 15pt Times Bold Italic font and metallic pink background color c.addTitle("Project Cost Breakdown", "Times New Roman Bold Italic", 15).setBackground( Chart.metalColor(0xff9999)); // Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110); // Draw the pie in 3D with 20 pixels 3D depth c.set3D(20); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label box background color the same as the sector color, with glass effect, and with // 5 pixels rounded corners ChartDirector.TextBox t = c.setLabelStyle(); t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect()); t.setRoundedCorners(5); // Set the border color of the sector the same color as the fill color. Set the line color of // the join line to black (0x0) c.setLineColor(Chart.SameAsMainColor, 0x000000); // Set the start angle to 135 degrees may improve layout when there are many small sectors at // the end of the data array (that is, data sorted in descending order). It is because this // makes the small sectors position near the horizontal axis, where the text label has the least // tendency to overlap. For data sorted in ascending order, a start angle of 45 degrees can be // used instead. c.setStartAngle(135); // Set the pie data and the pie labels c.setData(data, labels); // Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='{label}: US${value}K ({percent}%)'" ); } </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\sidelabelpie.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 data for the pie chart Dim data() As Double = {35, 30, 25, 7, 6, 5, 4, 3, 2, 1} ' The labels for the pie chart Dim labels() As String = {"Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", _ "Insurance", "Licenses", "Transport", "Interest"} ' Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 pixel 3D ' border Dim c As PieChart = New PieChart(560, 270, Chart.goldColor(), -1, 1) ' Add a title box using 15pt Times Bold Italic font and metallic pink background color c.addTitle("Project Cost Breakdown", "Times New Roman Bold Italic", 15).setBackground( _ Chart.metalColor(&Hff9999)) ' Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110) ' Draw the pie in 3D with 20 pixels 3D depth c.set3D(20) ' Use the side label layout method c.setLabelLayout(Chart.SideLayout) ' Set the label box background color the same as the sector color, with glass effect, and with 5 ' pixels rounded corners Dim t As ChartDirector.TextBox = c.setLabelStyle() t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect()) t.setRoundedCorners(5) ' Set the border color of the sector the same color as the fill color. Set the line color of the ' join line to black (0x0) c.setLineColor(Chart.SameAsMainColor, &H000000) ' Set the start angle to 135 degrees may improve layout when there are many small sectors at the ' end of the data array (that is, data sorted in descending order). It is because this makes the ' small sectors position near the horizontal axis, where the text label has the least tendency ' to overlap. For data sorted in ascending order, a start angle of 45 degrees can be used ' instead. c.setStartAngle(135) ' Set the pie data and the pie labels c.setData(data, labels) ' Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", _ "title='{label}: US${value}K ({percent}%)'") 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\SidelabelpieController.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class SidelabelpieController : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Side Label Layout"; createChart(ViewBag.Viewer = new RazorChartViewer(HttpContext, "chart1")); return View("~/Views/Shared/ChartView.cshtml"); } // // Create chart // private void createChart(RazorChartViewer viewer) { // The data for the pie chart double[] data = {35, 30, 25, 7, 6, 5, 4, 3, 2, 1}; // The labels for the pie chart string[] labels = {"Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", "Insurance", "Licenses", "Transport", "Interest"}; // Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 pixel // 3D border PieChart c = new PieChart(560, 270, Chart.goldColor(), -1, 1); // Add a title box using 15pt Times Bold Italic font and metallic pink background color c.addTitle("Project Cost Breakdown", "Times New Roman Bold Italic", 15).setBackground( Chart.metalColor(0xff9999)); // Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110); // Draw the pie in 3D with 20 pixels 3D depth c.set3D(20); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label box background color the same as the sector color, with glass effect, and // with 5 pixels rounded corners ChartDirector.TextBox t = c.setLabelStyle(); t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect()); t.setRoundedCorners(5); // Set the border color of the sector the same color as the fill color. Set the line color of // the join line to black (0x0) c.setLineColor(Chart.SameAsMainColor, 0x000000); // Set the start angle to 135 degrees may improve layout when there are many small sectors at // the end of the data array (that is, data sorted in descending order). It is because this // makes the small sectors position near the horizontal axis, where the text label has the // least tendency to overlap. For data sorted in ascending order, a start angle of 45 degrees // can be used instead. c.setStartAngle(135); // Set the pie data and the pie labels c.setData(data, labels); // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='{label}: US${value}K ({percent}%)'"); } } }

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