ChartDirector 6.2 (.NET Edition)

Circular Label Layout


  

This example demonstrates label positioning in the "circular label layout" method.

By default, in "circular label layout", the sector labels will be external and close to the pie perimeter.

The PieChart.setLabelPos method can be used to control the distance between the labels and the pie perimeter, and add join lines to connect the labels to the sectors. The join lines are useful if the labels are far away from the pie perimeter.

The distance between the sector labels and the pie perimeter can be negative, in which case the labels will be internal to the pie.

Source Code Listing

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

namespace CSharpChartExplorer
{
    public class circlelabelpie : DemoModule
    {
        //Name of demo module
        public string getName() { return "Circular Label Layout"; }

        //Number of charts produced in this demo module
        public int getNoOfCharts() { return 2; }

        //Main code for creating charts
        public void createChart(WinChartViewer viewer, int chartIndex)
        {
            // The data for the pie chart
            double[] data = {42, 18, 8};

            // The labels for the pie chart
            string[] labels = {"Agree", "Disagree", "Not Sure"};

            // The colors to use for the sectors
            int[] colors = {0x66ff66, 0xff6666, 0xffff00};

            // Create a PieChart object of size 300 x 300 pixels. Set the background to a gradient
            // color from blue (aaccff) to sky blue (ffffff), with a grey (888888) border. Use
            // rounded corners and soft drop shadow.
            PieChart c = new PieChart(300, 300);
            c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xaaccff, 0xffffff),
                0x888888);
            c.setRoundedFrame();
            c.setDropShadow();

            if (chartIndex == 0) {
            //============================================================
            //    Draw a pie chart where the label is on top of the pie
            //============================================================

                // Set the center of the pie at (150, 150) and the radius to 120 pixels
                c.setPieSize(150, 150, 120);

                // Set the label position to -40 pixels from the perimeter of the pie (-ve means
                // label is inside the pie)
                c.setLabelPos(-40);

            } else {
            //============================================================
            //    Draw a pie chart where the label is outside the pie
            //============================================================

                // Set the center of the pie at (150, 150) and the radius to 80 pixels
                c.setPieSize(150, 150, 80);

                // Set the sector label position to be 20 pixels from the pie. Use a join line to
                // connect the labels to the sectors.
                c.setLabelPos(20, Chart.LineColor);

            }

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

            // Set the sector colors
            c.setColors2(Chart.DataColor, colors);

            // Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) border
            c.setSectorStyle(Chart.LocalGradientShading, unchecked((int)0xbb000000), 1);

            // 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\circlelabelpie.vb
Imports System
Imports Microsoft.VisualBasic
Imports ChartDirector

Public Class circlelabelpie
    Implements DemoModule

    'Name of demo module
    Public Function getName() As String Implements DemoModule.getName
        Return "Circular Label Layout"
    End Function

    'Number of charts produced in this demo module
    Public Function getNoOfCharts() As Integer Implements DemoModule.getNoOfCharts
        Return 2
    End Function

    'Main code for creating charts
    Public Sub createChart(viewer As WinChartViewer, chartIndex As Integer) _
        Implements DemoModule.createChart

        ' The data for the pie chart
        Dim data() As Double = {42, 18, 8}

        ' The labels for the pie chart
        Dim labels() As String = {"Agree", "Disagree", "Not Sure"}

        ' The colors to use for the sectors
        Dim colors() As Integer = {&H66ff66, &Hff6666, &Hffff00}

        ' Create a PieChart object of size 300 x 300 pixels. Set the background to a gradient color
        ' from blue (aaccff) to sky blue (ffffff), with a grey (888888) border. Use rounded corners
        ' and soft drop shadow.
        Dim c As PieChart = New PieChart(300, 300)
        c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, &Haaccff, &Hffffff), _
            &H888888)
        c.setRoundedFrame()
        c.setDropShadow()

        If chartIndex = 0 Then
        '============================================================
        '    Draw a pie chart where the label is on top of the pie
        '============================================================

            ' Set the center of the pie at (150, 150) and the radius to 120 pixels
            c.setPieSize(150, 150, 120)

            ' Set the label position to -40 pixels from the perimeter of the pie (-ve means label is
            ' inside the pie)
            c.setLabelPos(-40)

        Else
        '============================================================
        '    Draw a pie chart where the label is outside the pie
        '============================================================

            ' Set the center of the pie at (150, 150) and the radius to 80 pixels
            c.setPieSize(150, 150, 80)

            ' Set the sector label position to be 20 pixels from the pie. Use a join line to connect
            ' the labels to the sectors.
            c.setLabelPos(20, Chart.LineColor)

        End If

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

        ' Set the sector colors
        c.setColors2(Chart.DataColor, colors)

        ' Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) border
        c.setSectorStyle(Chart.LocalGradientShading, &Hbb000000, 1)

        ' 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\circlelabelpie.cs
using System;
using ChartDirector;

namespace CSharpWPFDemo
{
    public class circlelabelpie : DemoModule
    {
        //Name of demo module
        public string getName() { return "Circular Label Layout"; }

        //Number of charts produced in this demo module
        public int getNoOfCharts() { return 2; }

        //Main code for creating charts
        public void createChart(WPFChartViewer viewer, int chartIndex)
        {
            // The data for the pie chart
            double[] data = {42, 18, 8};

            // The labels for the pie chart
            string[] labels = {"Agree", "Disagree", "Not Sure"};

            // The colors to use for the sectors
            int[] colors = {0x66ff66, 0xff6666, 0xffff00};

            // Create a PieChart object of size 300 x 300 pixels. Set the background to a gradient
            // color from blue (aaccff) to sky blue (ffffff), with a grey (888888) border. Use
            // rounded corners and soft drop shadow.
            PieChart c = new PieChart(300, 300);
            c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xaaccff, 0xffffff),
                0x888888);
            c.setRoundedFrame();
            c.setDropShadow();

            if (chartIndex == 0) {
            //============================================================
            //    Draw a pie chart where the label is on top of the pie
            //============================================================

                // Set the center of the pie at (150, 150) and the radius to 120 pixels
                c.setPieSize(150, 150, 120);

                // Set the label position to -40 pixels from the perimeter of the pie (-ve means
                // label is inside the pie)
                c.setLabelPos(-40);

            } else {
            //============================================================
            //    Draw a pie chart where the label is outside the pie
            //============================================================

                // Set the center of the pie at (150, 150) and the radius to 80 pixels
                c.setPieSize(150, 150, 80);

                // Set the sector label position to be 20 pixels from the pie. Use a join line to
                // connect the labels to the sectors.
                c.setLabelPos(20, Chart.LineColor);

            }

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

            // Set the sector colors
            c.setColors2(Chart.DataColor, colors);

            // Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) border
            c.setSectorStyle(Chart.LocalGradientShading, unchecked((int)0xbb000000), 1);

            // 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\circlelabelpie.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)
{
    // The data for the pie chart
    double[] data = {42, 18, 8};

    // The labels for the pie chart
    string[] labels = {"Agree", "Disagree", "Not Sure"};

    // The colors to use for the sectors
    int[] colors = {0x66ff66, 0xff6666, 0xffff00};

    // Create a PieChart object of size 300 x 300 pixels. Set the background to a gradient color
    // from blue (aaccff) to sky blue (ffffff), with a grey (888888) border. Use rounded corners and
    // soft drop shadow.
    PieChart c = new PieChart(300, 300);
    c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xaaccff, 0xffffff), 0x888888)
        ;
    c.setRoundedFrame();
    c.setDropShadow();

    if (chartIndex == 0) {
    //============================================================
    //    Draw a pie chart where the label is on top of the pie
    //============================================================

        // Set the center of the pie at (150, 150) and the radius to 120 pixels
        c.setPieSize(150, 150, 120);

        // Set the label position to -40 pixels from the perimeter of the pie (-ve means label is
        // inside the pie)
        c.setLabelPos(-40);

    } else {
    //============================================================
    //    Draw a pie chart where the label is outside the pie
    //============================================================

        // Set the center of the pie at (150, 150) and the radius to 80 pixels
        c.setPieSize(150, 150, 80);

        // Set the sector label position to be 20 pixels from the pie. Use a join line to connect
        // the labels to the sectors.
        c.setLabelPos(20, Chart.LineColor);

    }

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

    // Set the sector colors
    c.setColors2(Chart.DataColor, colors);

    // Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) border
    c.setSectorStyle(Chart.LocalGradientShading, unchecked((int)0xbb000000), 1);

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

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

//
// Page Load event handler
//
protected void Page_Load(object sender, EventArgs e)
{
    createChart(WebChartViewer0, 0);
    createChart(WebChartViewer1, 1);
}

</script>

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

[ASP.NET Web Forms - VB Version] NetWebCharts\VBNetASP\circlelabelpie.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)

    ' The data for the pie chart
    Dim data() As Double = {42, 18, 8}

    ' The labels for the pie chart
    Dim labels() As String = {"Agree", "Disagree", "Not Sure"}

    ' The colors to use for the sectors
    Dim colors() As Integer = {&H66ff66, &Hff6666, &Hffff00}

    ' Create a PieChart object of size 300 x 300 pixels. Set the background to a gradient color from
    ' blue (aaccff) to sky blue (ffffff), with a grey (888888) border. Use rounded corners and soft
    ' drop shadow.
    Dim c As PieChart = New PieChart(300, 300)
    c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, &Haaccff, &Hffffff), _
        &H888888)
    c.setRoundedFrame()
    c.setDropShadow()

    If chartIndex = 0 Then
    '============================================================
    '    Draw a pie chart where the label is on top of the pie
    '============================================================

        ' Set the center of the pie at (150, 150) and the radius to 120 pixels
        c.setPieSize(150, 150, 120)

        ' Set the label position to -40 pixels from the perimeter of the pie (-ve means label is
        ' inside the pie)
        c.setLabelPos(-40)

    Else
    '============================================================
    '    Draw a pie chart where the label is outside the pie
    '============================================================

        ' Set the center of the pie at (150, 150) and the radius to 80 pixels
        c.setPieSize(150, 150, 80)

        ' Set the sector label position to be 20 pixels from the pie. Use a join line to connect the
        ' labels to the sectors.
        c.setLabelPos(20, Chart.LineColor)

    End If

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

    ' Set the sector colors
    c.setColors2(Chart.DataColor, colors)

    ' Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) border
    c.setSectorStyle(Chart.LocalGradientShading, &Hbb000000, 1)

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

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

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)

End Sub

</script>

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

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

namespace NetMvcCharts.Controllers
{
    public class CirclelabelpieController : Controller
    {
        //
        // Default Action
        //
        public ActionResult Index()
        {
            ViewBag.Title = "Circular Label Layout";

            // This example contains 2 charts.
            ViewBag.Viewer = new RazorChartViewer[2];
            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)
        {
            // The data for the pie chart
            double[] data = {42, 18, 8};

            // The labels for the pie chart
            string[] labels = {"Agree", "Disagree", "Not Sure"};

            // The colors to use for the sectors
            int[] colors = {0x66ff66, 0xff6666, 0xffff00};

            // Create a PieChart object of size 300 x 300 pixels. Set the background to a gradient color
            // from blue (aaccff) to sky blue (ffffff), with a grey (888888) border. Use rounded corners
            // and soft drop shadow.
            PieChart c = new PieChart(300, 300);
            c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xaaccff, 0xffffff),
                0x888888);
            c.setRoundedFrame();
            c.setDropShadow();

            if (chartIndex == 0) {
            //============================================================
            //    Draw a pie chart where the label is on top of the pie
            //============================================================

                // Set the center of the pie at (150, 150) and the radius to 120 pixels
                c.setPieSize(150, 150, 120);

                // Set the label position to -40 pixels from the perimeter of the pie (-ve means label is
                // inside the pie)
                c.setLabelPos(-40);

            } else {
            //============================================================
            //    Draw a pie chart where the label is outside the pie
            //============================================================

                // Set the center of the pie at (150, 150) and the radius to 80 pixels
                c.setPieSize(150, 150, 80);

                // Set the sector label position to be 20 pixels from the pie. Use a join line to connect
                // the labels to the sectors.
                c.setLabelPos(20, Chart.LineColor);

            }

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

            // Set the sector colors
            c.setColors2(Chart.DataColor, colors);

            // Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) border
            c.setSectorStyle(Chart.LocalGradientShading, unchecked((int)0xbb000000), 1);

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

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