5.41. Example - Pie Chart Label Types
Prev | Chapter 5. PHPlot Examples | Next

## 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.

    <?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`.

    <?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.

    <?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.

    <?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.

    <?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();

Prev | Up | Next
5.40. Example - Bubbles Plot | Home | 5.42. Example - DrawMessage