Link Search Menu Expand Document

Pies and doughnuts

Pies and doughnuts are used to represent relative proportions of data. These can be drawn using the Pie class; this class can be used to draw both pies and doughnuts.

The following example shows how to use it:

   

Expand source code
using VectSharp;
using VectSharp.Plots;
using VectSharp.SVG;

// Pie chart
{
    // Create some random data.
    Random rnd = new Random();
    double[] data = new double[] { rnd.Next(1, 10), rnd.Next(1, 10), rnd.Next(1, 10), rnd.Next(1, 10) };

    // Create a linear coordinate system.
    LinearCoordinateSystem2D coordinateSystem = new LinearCoordinateSystem2D(-1, 1, -1, 1, 250, 250);

    // Create the pie chart plot element.
    Pie pie = new Pie(data, new double[] { 0, 0 }, new double[] { 1, 1 }, coordinateSystem)
    {
        PresentationAttributes = new PlotElementPresentationAttributes[]
        {
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(0, 114, 178) },
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(213, 94, 0) },
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(204, 121, 167) },
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(230, 159, 0) },
        }
    };

    // Create the plot.
    Plot plot = new Plot();

    // Add the pie to the plot.
    plot.AddPlotElement(pie);

    // Render the plot to a Page and save it as an SVG document.
    Page pag = plot.Render();
    pag.SaveAsSVG("pie.svg");
}

// Doughnut chart
{
    // Create some random data.
    Random rnd = new Random();
    double[] data = new double[] { rnd.Next(1, 10), rnd.Next(1, 10), rnd.Next(1, 10), rnd.Next(1, 10) };

    // Create a linear coordinate system.
    LinearCoordinateSystem2D coordinateSystem = new LinearCoordinateSystem2D(-1, 1, -1, 1, 250, 250);

    // Create the doughnut chart plot element.
    Pie doughnut = new Pie(data, new double[] { 0, 0 }, new double[] { 0.5, 0.5 }, new double[] { 1, 1 }, coordinateSystem)
    {
        PresentationAttributes = new PlotElementPresentationAttributes[]
        {
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(0, 114, 178) },
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(213, 94, 0) },
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(204, 121, 167) },
            new PlotElementPresentationAttributes(){ Stroke= null, Fill = Colour.FromRgb(230, 159, 0) },
        }
    };

    // Create the plot.
    Plot plot = new Plot();

    // Add the doughnut to the plot.
    plot.AddPlotElement(doughnut);

    // Render the plot to a Page and save it as an SVG document.
    Page pag = plot.Render();
    pag.SaveAsSVG("doughnut.svg");
}

The Pie class

This plot element draws both pie charts and doughnut charts. Whether a pie or a doughnut is drawn depends on the value of the InnerRadius property: if this is [0, 0], a pie is drawn; otherwise, this determines the size of the “hole” and a doughnut is drawn. The Centre of the pie/doughnut is specified in data coordinates (which are then converted by the CoordinateSystem), but also the InnerRadius and OuterRadius are specified as vectors in the data coordinate space. The first and second elements of these arrays specify, respectively, the width and the height of the pie chart. Depending on the values for these and on the coordinate system, the chart may be a perfect circle, or it may be more or less “squished”.