Pie Chart

Produce a pie chart.

Complete Code

Download file: PieChart.pg

POD for Macro Files

See Also

PG problem file

Explanation

DOCUMENT();

loadMacros("PGstandard.pl", 'PGML.pl', 'StatisticalPlots.pl', 'PGcourse.pl');

Preamble

This uses the add_piechart method in the StatisticalPlots.pl macro. A donut plot version of this is available at Donut Plot.

@values  = (1,          5,        8,       2,       4);
@animals = ('Anteater', 'Bobcat', 'Camel', 'Dingo', 'Elephant');

$stat_plot = StatPlot(xvisible => 0, yvisible => 0, show_grid => 0);
$stat_plot->add_piechart(
    ~~@values,
    labels          => ~~@animals,
    radius          => 3.25,
    angle_offset    => 30,
    stroke_width    => 1,
    rounded_corners => 1
);

$total = 0;
$total += $_ for (@values);
$alt_text = 'A pie chart of animals.  ';
for (0 .. 4) {
    $alt_text .=
        "The size of the $animals[$_] wedge is about "
        . Round(100 * $values[$_] / $total, 1)
        . '% of the total. ';
}

Setup

In this example, the size of each wedge in the pie chart is given as an array.

The StatPlot is created with the axes and grid turned off. The add_piechart method requires an array reference of values that will generate pie wedges with given size. Optionally, the labels are included as an array reference and are placed outside the piechart. The radius gives the size of the circle and the angle_offset shifts the angle (in degrees) of the starting sector from the horizontal axis.

The default colors come from the rainbow palette, and other options exist.

A detailed alternative text should accompany all plots/graphs.

BEGIN_PGML

>> Pie Chart of the number of animals at the local zoo <<

>>[! [$alt_text] !]{$stat_plot}{400}<<

END_PGML

Statement

In this case, we just plot the box plots.

BEGIN_PGML_SOLUTION
Place a detailed solution here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.