5.41. Example - Pie Chart Label Types

By default, PHPlot labels pie chart segments with their percentage. That is, a pie segment that spans 90 degrees will be labeled "25.0%". Using SetPieLabelType, you can choose the source of the labels (percentage, data array labels, segment index, or value) and how the result should be formatted.

Notes: Pie label type options were added in PHPlot-5.6.0.

All of the examples in this section include the following script which contains the data array and plot title. The script is called pielabeltypedata.php.

<?php
# PHPlot Example: Pie Chart Label Types - Data array
# This is used by several examples. The data is 'altered' for appearance.
$title = 'Energy Production By Source, 2005';
$data = array(
    array('Biomass',        3120.43),
    array('Coal',          23185.20), 
    array('Geotherm.',       343.74),
    array('Hydro',          2703.92),
    array('NGPL',           2334.04),
    array('Nat. Gas',      18574.55), 
    array('Nuclear',        8160.49),
    array('Oil',           10963.63),
);

Example 5.41. Pie Chart Label Types

This first script shows the default behavior - percentage labels.

Pie Chart Label Types Example - 1) Defaults
<?php
# PHPlot Example: Pie Chart Label Types - baseline, default label type
# This requires PHPlot >= 5.6.0
require_once 'phplot.php';
require_once 'pielabeltypedata.php'; // Defines $data and $title

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetPlotType('pie');
$plot->SetDataType('text-data-single');
$plot->SetDataValues($data);
$plot->SetTitle($title);
$plot->DrawGraph();

The example below uses the label strings from the data array to label the pie segments. Note that this only works when the data array data type is text-data-single.

Pie Chart Label Types Example - 2) Data Array Labels
<?php
# PHPlot Example: Pie Chart Label Types - Labels from data array
# This requires PHPlot >= 5.6.0
require_once 'phplot.php';
require_once 'pielabeltypedata.php'; // Defines $data and $title

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetPlotType('pie');
$plot->SetDataType('text-data-single');
$plot->SetDataValues($data);
$plot->SetTitle($title);
# Set label type: Use labels from data array
$plot->SetPieLabelType('label');
$plot->DrawGraph();

The next example uses the numeric value of each segment to label the segment. In addition, 'data' formatting is used with 2 digits of precision.

Pie Chart Label Types Example - 3) Segment Value Labels
<?php
# PHPlot Example: Pie Chart Label Types - Formatted values as labels
# This requires PHPlot >= 5.6.0
require_once 'phplot.php';
require_once 'pielabeltypedata.php'; // Defines $data and $title

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetPlotType('pie');
$plot->SetDataType('text-data-single');
$plot->SetDataValues($data);
$plot->SetTitle($title);
# Set label type: segment values, formatted with 2 decimal places
$plot->SetPieLabelType('value', 'data', 2);
$plot->DrawGraph();

In this example, we want to use strings from an external array to label the pie segments. To do that, set the label source to 'index' to get a segment number starting with zero, and define a custom formatting callback function to get the value for each label.

Pie Chart Label Types Example - 4) Index and custom callback
<?php
# PHPlot Example: Pie Chart Label Types - Index and custom callback
# This requires PHPlot >= 5.6.0
require_once 'phplot.php';
require_once 'pielabeltypedata.php'; // Defines $data and $title

$mylabels = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
function mycallback($index)
{
    global $mylabels;
    return $mylabels[$index];
}

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetPlotType('pie');
$plot->SetDataType('text-data-single');
$plot->SetDataValues($data);
$plot->SetTitle($title);
# Set label type: Pass segment index (0-N) to custom formating function
$plot->SetPieLabelType('index', 'custom', 'mycallback');
$plot->DrawGraph();

The last example of pie chart labels shows how to use multiple label sources to make complex labels. In this case, we want the pie segment labels to show the label string from the data array and the percentage. When multiple label sources are selected, PHPlot separates the fields with a space, then passes them to your custom formatting callback function as a single string. In order to separate them, use the explode(). Since the data array labels might contain spaces, be sure to put this field last, and limit how many fields explode will return.

Pie Chart Label Types Example - 5) Multipart Labels
<?php
# PHPlot Example: Pie Chart Label Types - Multi-part labels
# This requires PHPlot >= 5.6.0
require_once 'phplot.php';
require_once 'pielabeltypedata.php'; // Defines $data and $title

function mycallback($str)
{
    list($percent, $label) = explode(' ', $str, 2);
    return sprintf('%s (%.1f%%)', $label, $percent);
}

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetPlotType('pie');
$plot->SetDataType('text-data-single');
$plot->SetDataValues($data);
$plot->SetTitle($title);
# Set label type: combine 2 fields and pass to custom formatting function
$plot->SetPieLabelType(array('percent', 'label'), 'custom', 'mycallback');
$plot->DrawGraph();

SourceForge.net Logo

This version of the manual was produced for the PHPlot Sourceforge project web service site, which requires the logo on each page.

To download a logo-free copy of the manual, see the PHPlot project downloads area.