ChartDirector 6.2 (.NET Edition)

Texture Donut Chart




This example demonstrates wooden, rocky and marble texture effects by using pattern colors to load image patterns.

In ChartDirector, metallic and pattern colors are just like normal ARGB colors, and are represented as a 32-bit integer. They can be applied to anything that can be colored. In this example, metallic colors are applied to the background, while pattern colors are applied to the donut slices to create texture effects.

Source Code Listing

[Windows Forms - C# version] NetWinCharts\CSharpWinCharts\texturedonut.cs
using System;
using ChartDirector;

namespace CSharpChartExplorer
{
    public class texturedonut : DemoModule
    {
        //Name of demo module
        public string getName() { return "Texture Donut Chart"; }

        //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 = {18, 45, 28};

            // The labels for the pie chart
            string[] labels = {"Marble", "Wood", "Granite"};

            // The icons for the sectors
            string[] texture = {"marble3.png", "wood.png", "rock.png"};

            // Create a PieChart object of size 400 x 330 pixels, with a metallic green (88EE88)
            // background, black border and 1 pixel 3D border effect
            PieChart c = new PieChart(400, 330, Chart.metalColor(0x88ee88), 0x000000, 1);

            // Set donut center at (200, 160), and outer/inner radii as 120/60 pixels
            c.setDonutSize(200, 160, 120, 60);

            // Add a title box using 15pt Times Bold Italic font and metallic deep green (008000)
            // background color
            c.addTitle("Material Composition", "Times New Roman Bold Italic", 15).setBackground(
                Chart.metalColor(0x008000));

            // Set the pie data and the pie labels
            c.setData(data, labels);

            // Set the colors of the sectors to the 3 texture patterns
            c.setColor(Chart.DataColor + 0, c.patternColor2(texture[0]));
            c.setColor(Chart.DataColor + 1, c.patternColor2(texture[1]));
            c.setColor(Chart.DataColor + 2, c.patternColor2(texture[2]));

            // Draw the pie in 3D with a 3D depth of 30 pixels
            c.set3D(30);

            // Use 12pt Arial Bold Italic as the sector label font
            c.setLabelStyle("Arial Bold Italic", 12);

            // Output the chart
            viewer.Chart = c;

            //include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("clickable", "",
                "title='{label}: {value}kg ({percent}%)'");
        }
    }
}

[Windows Forms - VB Version] NetWinCharts\VBNetWinCharts\texturedonut.vb
Imports System
Imports Microsoft.VisualBasic
Imports ChartDirector

Public Class texturedonut
    Implements DemoModule

    'Name of demo module
    Public Function getName() As String Implements DemoModule.getName
        Return "Texture Donut Chart"
    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 = {18, 45, 28}

        ' The labels for the pie chart
        Dim labels() As String = {"Marble", "Wood", "Granite"}

        ' The icons for the sectors
        Dim texture() As String = {"marble3.png", "wood.png", "rock.png"}

        ' Create a PieChart object of size 400 x 330 pixels, with a metallic green (88EE88)
        ' background, black border and 1 pixel 3D border effect
        Dim c As PieChart = New PieChart(400, 330, Chart.metalColor(&H88ee88), &H000000, 1)

        ' Set donut center at (200, 160), and outer/inner radii as 120/60 pixels
        c.setDonutSize(200, 160, 120, 60)

        ' Add a title box using 15pt Times Bold Italic font and metallic deep green (008000)
        ' background color
        c.addTitle("Material Composition", "Times New Roman Bold Italic", 15).setBackground( _
            Chart.metalColor(&H008000))

        ' Set the pie data and the pie labels
        c.setData(data, labels)

        ' Set the colors of the sectors to the 3 texture patterns
        c.setColor(Chart.DataColor + 0, c.patternColor2(texture(0)))
        c.setColor(Chart.DataColor + 1, c.patternColor2(texture(1)))
        c.setColor(Chart.DataColor + 2, c.patternColor2(texture(2)))

        ' Draw the pie in 3D with a 3D depth of 30 pixels
        c.set3D(30)

        ' Use 12pt Arial Bold Italic as the sector label font
        c.setLabelStyle("Arial Bold Italic", 12)

        ' Output the chart
        viewer.Chart = c

        'include tool tip for the chart
        viewer.ImageMap = c.getHTMLImageMap("clickable", "", _
            "title='{label}: {value}kg ({percent}%)'")

    End Sub

End Class

[WPF - C#] NetWPFCharts\texturedonut.cs
using System;
using ChartDirector;

namespace CSharpWPFDemo
{
    public class texturedonut : DemoModule
    {
        //Name of demo module
        public string getName() { return "Texture Donut Chart"; }

        //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 = {18, 45, 28};

            // The labels for the pie chart
            string[] labels = {"Marble", "Wood", "Granite"};

            // The icons for the sectors
            string[] texture = {"marble3.png", "wood.png", "rock.png"};

            // Create a PieChart object of size 400 x 330 pixels, with a metallic green (88EE88)
            // background, black border and 1 pixel 3D border effect
            PieChart c = new PieChart(400, 330, Chart.metalColor(0x88ee88), 0x000000, 1);

            // Set donut center at (200, 160), and outer/inner radii as 120/60 pixels
            c.setDonutSize(200, 160, 120, 60);

            // Add a title box using 15pt Times Bold Italic font and metallic deep green (008000)
            // background color
            c.addTitle("Material Composition", "Times New Roman Bold Italic", 15).setBackground(
                Chart.metalColor(0x008000));

            // Set the pie data and the pie labels
            c.setData(data, labels);

            // Set the colors of the sectors to the 3 texture patterns
            c.setColor(Chart.DataColor + 0, c.patternColor2(texture[0]));
            c.setColor(Chart.DataColor + 1, c.patternColor2(texture[1]));
            c.setColor(Chart.DataColor + 2, c.patternColor2(texture[2]));

            // Draw the pie in 3D with a 3D depth of 30 pixels
            c.set3D(30);

            // Use 12pt Arial Bold Italic as the sector label font
            c.setLabelStyle("Arial Bold Italic", 12);

            // Output the chart
            viewer.Chart = c;

            //include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("clickable", "",
                "title='{label}: {value}kg ({percent}%)'");
        }
    }
}

[ASP.NET Web Forms - C# version] NetWebCharts\CSharpASP\texturedonut.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 = {18, 45, 28};

    // The labels for the pie chart
    string[] labels = {"Marble", "Wood", "Granite"};

    // The icons for the sectors
    string[] texture = {"marble3.png", "wood.png", "rock.png"};

    // Create a PieChart object of size 400 x 330 pixels, with a metallic green (88EE88) background,
    // black border and 1 pixel 3D border effect
    PieChart c = new PieChart(400, 330, Chart.metalColor(0x88ee88), 0x000000, 1);

    //Set default directory for loading images from current script directory
    c.setSearchPath(Server.MapPath("."));

    // Set donut center at (200, 160), and outer/inner radii as 120/60 pixels
    c.setDonutSize(200, 160, 120, 60);

    // Add a title box using 15pt Times Bold Italic font and metallic deep green (008000) background
    // color
    c.addTitle("Material Composition", "Times New Roman Bold Italic", 15).setBackground(
        Chart.metalColor(0x008000));

    // Set the pie data and the pie labels
    c.setData(data, labels);

    // Set the colors of the sectors to the 3 texture patterns
    c.setColor(Chart.DataColor + 0, c.patternColor2(texture[0]));
    c.setColor(Chart.DataColor + 1, c.patternColor2(texture[1]));
    c.setColor(Chart.DataColor + 2, c.patternColor2(texture[2]));

    // Draw the pie in 3D with a 3D depth of 30 pixels
    c.set3D(30);

    // Use 12pt Arial Bold Italic as the sector label font
    c.setLabelStyle("Arial Bold Italic", 12);

    // Output the chart
    WebChartViewer1.Image = c.makeWebImage(Chart.PNG);

    // Include tool tip for the chart
    WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='{label}: {value}kg ({percent}%)'");
}

</script>

<html>
<body>
    <chart:WebChartViewer id="WebChartViewer1" runat="server" />
</body>
</html>

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\texturedonut.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 = {18, 45, 28}

    ' The labels for the pie chart
    Dim labels() As String = {"Marble", "Wood", "Granite"}

    ' The icons for the sectors
    Dim texture() As String = {"marble3.png", "wood.png", "rock.png"}

    ' Create a PieChart object of size 400 x 330 pixels, with a metallic green (88EE88) background,
    ' black border and 1 pixel 3D border effect
    Dim c As PieChart = New PieChart(400, 330, Chart.metalColor(&H88ee88), &H000000, 1)

    ' Set default directory for loading images from current script directory
    Call c.setSearchPath(Server.MapPath("."))

    ' Set donut center at (200, 160), and outer/inner radii as 120/60 pixels
    c.setDonutSize(200, 160, 120, 60)

    ' Add a title box using 15pt Times Bold Italic font and metallic deep green (008000) background
    ' color
    c.addTitle("Material Composition", "Times New Roman Bold Italic", 15).setBackground( _
        Chart.metalColor(&H008000))

    ' Set the pie data and the pie labels
    c.setData(data, labels)

    ' Set the colors of the sectors to the 3 texture patterns
    c.setColor(Chart.DataColor + 0, c.patternColor2(texture(0)))
    c.setColor(Chart.DataColor + 1, c.patternColor2(texture(1)))
    c.setColor(Chart.DataColor + 2, c.patternColor2(texture(2)))

    ' Draw the pie in 3D with a 3D depth of 30 pixels
    c.set3D(30)

    ' Use 12pt Arial Bold Italic as the sector label font
    c.setLabelStyle("Arial Bold Italic", 12)

    ' Output the chart
    WebChartViewer1.Image = c.makeWebImage(Chart.PNG)

    ' Include tool tip for the chart
    WebChartViewer1.ImageMap = c.getHTMLImageMap("", "", "title='{label}: {value}kg ({percent}%)'")

End Sub

</script>

<html>
<body>
    <chart:WebChartViewer id="WebChartViewer1" runat="server" />
</body>
</html>

[ASP.NET MVC - Controller] NetMvcCharts\Controllers\TexturedonutController.cs
using System;
using System.Web.Mvc;
using ChartDirector;

namespace NetMvcCharts.Controllers
{
    public class TexturedonutController : Controller
    {
        //
        // Default Action
        //
        public ActionResult Index()
        {
            ViewBag.Title = "Texture Donut Chart";
            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 = {18, 45, 28};

            // The labels for the pie chart
            string[] labels = {"Marble", "Wood", "Granite"};

            // The icons for the sectors
            string[] texture = {"marble3.png", "wood.png", "rock.png"};

            // Create a PieChart object of size 400 x 330 pixels, with a metallic green (88EE88)
            // background, black border and 1 pixel 3D border effect
            PieChart c = new PieChart(400, 330, Chart.metalColor(0x88ee88), 0x000000, 1);

            //Set default directory for loading images
            c.setSearchPath(Url.Content("~/Content"));

            // Set donut center at (200, 160), and outer/inner radii as 120/60 pixels
            c.setDonutSize(200, 160, 120, 60);

            // Add a title box using 15pt Times Bold Italic font and metallic deep green (008000)
            // background color
            c.addTitle("Material Composition", "Times New Roman Bold Italic", 15).setBackground(
                Chart.metalColor(0x008000));

            // Set the pie data and the pie labels
            c.setData(data, labels);

            // Set the colors of the sectors to the 3 texture patterns
            c.setColor(Chart.DataColor + 0, c.patternColor2(texture[0]));
            c.setColor(Chart.DataColor + 1, c.patternColor2(texture[1]));
            c.setColor(Chart.DataColor + 2, c.patternColor2(texture[2]));

            // Draw the pie in 3D with a 3D depth of 30 pixels
            c.set3D(30);

            // Use 12pt Arial Bold Italic as the sector label font
            c.setLabelStyle("Arial Bold Italic", 12);

            // Output the chart
            viewer.Image = c.makeWebImage(Chart.PNG);

            // Include tool tip for the chart
            viewer.ImageMap = c.getHTMLImageMap("", "", "title='{label}: {value}kg ({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>
</head>
<body style="margin:5px 0px 0px 5px">
    <div style="font:bold 18pt verdana;">
        @ViewBag.Title
    </div>
    <hr style="border:solid 1px #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>