ChartDirector 7.1 (.NET Edition)

Icon Pie Chart (2)




This example extends the Icon Pie Chart (1) example to demonstrate using CDML to include multiple fonts and colors in chart title, sector labels and to draw logo. It also demonstrates silver background color, rounded corners, and glass shading effect for title.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\iconpie2.cs
using System; using ChartDirector; namespace CSharpChartExplorer { public class iconpie2 : DemoModule { //Name of demo module public string getName() { return "Icon Pie Chart (2)"; } //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 = {28, 45, 5, 1, 12}; // The labels for the pie chart string[] labels = {"Excellent", "Good", "Bad", "Very Bad", "Neutral"}; // The icons for the sectors string[] icons = {"@/images/laugh.png", "@/images/smile.png", "@/images/sad.png", "@/images/angry.png", "@/images/nocomment.png"}; // Create a PieChart object of size 560 x 300 pixels, with a silver background, black // border, 1 pxiel 3D border effect and rounded corners PieChart c = new PieChart(560, 300, Chart.silverColor(), 0x000000, 1); c.setRoundedFrame(); // Set the center of the pie at (280, 150) and the radius to 120 pixels c.setPieSize(280, 150, 120); // Add a title box with title written in CDML, on a sky blue (A0C8FF) background with // glass effect c.addTitle( "<*block,valign=absmiddle*><*img=@/images/doc.png*> Customer Survey: " + "<*font=Times New Roman Italic,color=000000*>Do you like our " + "<*font,color=dd0000*>Hyper<*super*>TM<*/font*> molecules?", "Times New Roman Bold Italic", 15, 0x000080).setBackground(0xa0c8ff, 0x000000, Chart.glassEffect()); // Add a logo to the chart written in CDML as the bottom title aligned to the bottom // right c.addTitle2(Chart.BottomRight, "<*block,valign=absmiddle*><*img=@/images/molecule.png*> <*block*><*color=FF*>" + "<*font=Times New Roman Bold Italic,size=12*>Molecular Engineering\n" + "<*font=Arial,size=10*>Creating better molecules"); // Set the pie data and the pie labels c.setData(data, labels); // Set 3D style c.set3D(); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label background color to transparent c.setLabelStyle().setBackground(Chart.Transparent); // Add icons to the chart as a custom field c.addExtraField(icons); // Configure the sector labels using CDML to include the icon images c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)"); // Explode the 3rd and 4th sectors as a group (index = 2 and 3) c.setExplodeGroup(2, 3); // 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); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{label}: {value} responses ({percent}%)'"); } } }

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\iconpie2.vb
Imports System Imports Microsoft.VisualBasic Imports ChartDirector Public Class iconpie2 Implements DemoModule 'Name of demo module Public Function getName() As String Implements DemoModule.getName Return "Icon Pie Chart (2)" 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 = {28, 45, 5, 1, 12} ' The labels for the pie chart Dim labels() As String = {"Excellent", "Good", "Bad", "Very Bad", "Neutral"} ' The icons for the sectors Dim icons() As String = {"@/laugh.png", "@/smile.png", "@/sad.png", "@/angry.png", _ "@/nocomment.png"} ' Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, ' 1 pxiel 3D border effect and rounded corners Dim c As PieChart = New PieChart(560, 300, Chart.silverColor(), &H000000, 1) c.setRoundedFrame() ' Set the center of the pie at (280, 150) and the radius to 120 pixels c.setPieSize(280, 150, 120) ' Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass ' effect c.addTitle( _ "<*block,valign=absmiddle*><*img=@/doc.png*> Customer Survey: <*font=Times New " & _ "Roman Italic,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM" & _ "<*/font*> molecules?", "Times New Roman Bold Italic", 15, &H000080).setBackground( _ &Ha0c8ff, &H000000, Chart.glassEffect()) ' Add a logo to the chart written in CDML as the bottom title aligned to the bottom right c.addTitle2(Chart.BottomRight, _ "<*block,valign=absmiddle*><*img=@/molecule.png*> <*block*><*color=FF*>" & _ "<*font=Times New Roman Bold Italic,size=12*>Molecular Engineering<*br*>" & _ "<*font=Arial,size=10*>Creating better molecules") ' Set the pie data and the pie labels c.setData(data, labels) ' Set 3D style c.set3D() ' Use the side label layout method c.setLabelLayout(Chart.SideLayout) ' Set the label background color to transparent c.setLabelStyle().setBackground(Chart.Transparent) ' Add icons to the chart as a custom field c.addExtraField(icons) ' Configure the sector labels using CDML to include the icon images c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)") ' Explode the 3rd and 4th sectors as a group (index = 2 and 3) c.setExplodeGroup(2, 3) ' 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) ' Output the chart viewer.Chart = c 'include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", _ "title='{label}: {value} responses ({percent}%)'") End Sub End Class

[WPF - C#] NetWPFCharts\CSharpWPFCharts\iconpie2.cs
using System; using ChartDirector; namespace CSharpWPFCharts { public class iconpie2 : DemoModule { //Name of demo module public string getName() { return "Icon Pie Chart (2)"; } //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 = {28, 45, 5, 1, 12}; // The labels for the pie chart string[] labels = {"Excellent", "Good", "Bad", "Very Bad", "Neutral"}; // The icons for the sectors string[] icons = {"@/images/laugh.png", "@/images/smile.png", "@/images/sad.png", "@/images/angry.png", "@/images/nocomment.png"}; // Create a PieChart object of size 560 x 300 pixels, with a silver background, black // border, 1 pxiel 3D border effect and rounded corners PieChart c = new PieChart(560, 300, Chart.silverColor(), 0x000000, 1); c.setRoundedFrame(); // Set the center of the pie at (280, 150) and the radius to 120 pixels c.setPieSize(280, 150, 120); // Add a title box with title written in CDML, on a sky blue (A0C8FF) background with // glass effect c.addTitle( "<*block,valign=absmiddle*><*img=@/images/doc.png*> Customer Survey: " + "<*font=Times New Roman Italic,color=000000*>Do you like our " + "<*font,color=dd0000*>Hyper<*super*>TM<*/font*> molecules?", "Times New Roman Bold Italic", 15, 0x000080).setBackground(0xa0c8ff, 0x000000, Chart.glassEffect()); // Add a logo to the chart written in CDML as the bottom title aligned to the bottom // right c.addTitle2(Chart.BottomRight, "<*block,valign=absmiddle*><*img=@/images/molecule.png*> <*block*><*color=FF*>" + "<*font=Times New Roman Bold Italic,size=12*>Molecular Engineering\n" + "<*font=Arial,size=10*>Creating better molecules"); // Set the pie data and the pie labels c.setData(data, labels); // Set 3D style c.set3D(); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label background color to transparent c.setLabelStyle().setBackground(Chart.Transparent); // Add icons to the chart as a custom field c.addExtraField(icons); // Configure the sector labels using CDML to include the icon images c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)"); // Explode the 3rd and 4th sectors as a group (index = 2 and 3) c.setExplodeGroup(2, 3); // 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); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{label}: {value} responses ({percent}%)'"); } } }

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\iconpie2.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 = {28, 45, 5, 1, 12}; // The labels for the pie chart string[] labels = {"Excellent", "Good", "Bad", "Very Bad", "Neutral"}; // The icons for the sectors string[] icons = {"laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png"}; // Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, 1 // pxiel 3D border effect and rounded corners PieChart c = new PieChart(560, 300, Chart.silverColor(), 0x000000, 1); c.setRoundedFrame(); //Set default directory for loading images from current script directory c.setSearchPath(Server.MapPath(".")); // Set the center of the pie at (280, 150) and the radius to 120 pixels c.setPieSize(280, 150, 120); // Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass // effect c.addTitle( "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: <*font=Times New Roman " + "Italic,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM<*/font*> " + "molecules?", "Times New Roman Bold Italic", 15, 0x000080).setBackground(0xa0c8ff, 0x000000, Chart.glassEffect()); // Add a logo to the chart written in CDML as the bottom title aligned to the bottom right c.addTitle2(Chart.BottomRight, "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*><*font=Times New " + "Roman Bold Italic,size=12*>Molecular Engineering\n<*font=Arial,size=10*>Creating better " + "molecules"); // Set the pie data and the pie labels c.setData(data, labels); // Set 3D style c.set3D(); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label background color to transparent c.setLabelStyle().setBackground(Chart.Transparent); // Add icons to the chart as a custom field c.addExtraField(icons); // Configure the sector labels using CDML to include the icon images c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)"); // Explode the 3rd and 4th sectors as a group (index = 2 and 3) c.setExplodeGroup(2, 3); // 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); // Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='{label}: {value} responses ({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\iconpie2.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 = {28, 45, 5, 1, 12} ' The labels for the pie chart Dim labels() As String = {"Excellent", "Good", "Bad", "Very Bad", "Neutral"} ' The icons for the sectors Dim icons() As String = {"laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png"} ' Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, 1 ' pxiel 3D border effect and rounded corners Dim c As PieChart = New PieChart(560, 300, Chart.silverColor(), &H000000, 1) c.setRoundedFrame() ' Set default directory for loading images from current script directory Call c.setSearchPath(Server.MapPath(".")) ' Set the center of the pie at (280, 150) and the radius to 120 pixels c.setPieSize(280, 150, 120) ' Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass ' effect c.addTitle( _ "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: <*font=Times New Roman " & _ "Italic,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM<*/font*> " & _ "molecules?", "Times New Roman Bold Italic", 15, &H000080).setBackground(&Ha0c8ff, _ &H000000, Chart.glassEffect()) ' Add a logo to the chart written in CDML as the bottom title aligned to the bottom right c.addTitle2(Chart.BottomRight, _ "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*><*font=Times New " & _ "Roman Bold Italic,size=12*>Molecular Engineering<*br*><*font=Arial,size=10*>Creating " & _ "better molecules") ' Set the pie data and the pie labels c.setData(data, labels) ' Set 3D style c.set3D() ' Use the side label layout method c.setLabelLayout(Chart.SideLayout) ' Set the label background color to transparent c.setLabelStyle().setBackground(Chart.Transparent) ' Add icons to the chart as a custom field c.addExtraField(icons) ' Configure the sector labels using CDML to include the icon images c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)") ' Explode the 3rd and 4th sectors as a group (index = 2 and 3) c.setExplodeGroup(2, 3) ' 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) ' Output the chart WebChartViewer1.Image = c.makeWebImage(Chart.SVG) ' Include tool tip for the chart WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", _ "title='{label}: {value} responses ({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\Iconpie2Controller.cs
using System; using System.Web.Mvc; using ChartDirector; namespace NetMvcCharts.Controllers { public class Iconpie2Controller : Controller { // // Default Action // public ActionResult Index() { ViewBag.Title = "Icon Pie Chart (2)"; 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 = {28, 45, 5, 1, 12}; // The labels for the pie chart string[] labels = {"Excellent", "Good", "Bad", "Very Bad", "Neutral"}; // The icons for the sectors string[] icons = {"laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png"}; // Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, // 1 pxiel 3D border effect and rounded corners PieChart c = new PieChart(560, 300, Chart.silverColor(), 0x000000, 1); c.setRoundedFrame(); //Set default directory for loading images c.setSearchPath(Url.Content("~/Content")); // Set the center of the pie at (280, 150) and the radius to 120 pixels c.setPieSize(280, 150, 120); // Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass // effect c.addTitle( "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: <*font=Times New Roman " + "Italic,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM<*/font*> " + "molecules?", "Times New Roman Bold Italic", 15, 0x000080).setBackground(0xa0c8ff, 0x000000, Chart.glassEffect()); // Add a logo to the chart written in CDML as the bottom title aligned to the bottom right c.addTitle2(Chart.BottomRight, "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*><*font=Times New " + "Roman Bold Italic,size=12*>Molecular Engineering\n<*font=Arial,size=10*>Creating " + "better molecules"); // Set the pie data and the pie labels c.setData(data, labels); // Set 3D style c.set3D(); // Use the side label layout method c.setLabelLayout(Chart.SideLayout); // Set the label background color to transparent c.setLabelStyle().setBackground(Chart.Transparent); // Add icons to the chart as a custom field c.addExtraField(icons); // Configure the sector labels using CDML to include the icon images c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)"); // Explode the 3rd and 4th sectors as a group (index = 2 and 3) c.setExplodeGroup(2, 3); // 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); // Output the chart viewer.Image = c.makeWebImage(Chart.SVG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='{label}: {value} responses ({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>