Blame | Last modification | View Log | RSS feed
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>5.41. Example - Pie Chart Label Types</title><link rel="stylesheet" type="text/css" href="phplotdoc.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.78.1" /><link rel="home" href="index.html" title="PHPlot Reference Manual" /><link rel="up" href="examples.html" title="Chapter 5. PHPlot Examples" /><link rel="prev" href="ex-bubbles1.html" title="5.40. Example - Bubbles Plot" /><link rel="next" href="ex-drawmessage.html" title="5.42. Example - DrawMessage" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.41. Example - Pie Chart Label Types</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-bubbles1.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-drawmessage.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ex-pielabeltype"></a>5.41. Example - Pie Chart Label Types</h2></div></div></div><p>By default, PHPlot labels pie chart segments with their percentage. Thatis, a pie segment that spans 90 degrees will be labeled "25.0%".Using <a class="xref" href="SetPieLabelType.html" title="SetPieLabelType"><span class="refentrytitle">SetPieLabelType</span></a>, you can choose the source of thelabels (percentage, data array labels, segment index, or value) and how theresult should be formatted.</p><p>Notes: Pie label type options were added in PHPlot-5.6.0.</p><p>All of the examples in this section include the following script whichcontains the data array and plot title. The script is called<code class="filename">pielabeltypedata.php</code>.</p><pre class="programlisting"><?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),);</pre><p></p><div class="example"><a id="example-pielabeltype"></a><p class="title"><strong>Example 5.41. Pie Chart Label Types</strong></p><div class="example-contents"><p>This first script shows the default behavior - percentage labels.</p><div class="informalfigure"><div class="mediaobject"><img src="examples/pielabeltype1.png" alt="Pie Chart Label Types Example - 1) Defaults" /></div></div><pre class="programlisting"><?php# PHPlot Example: Pie Chart Label Types - baseline, default label type# This requires PHPlot >= 5.6.0require_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();</pre><p>The example below uses the label strings from the data array to labelthe pie segments. Note that this only works when the data array data typeis <code class="literal">text-data-single</code>.</p><div class="informalfigure"><div class="mediaobject"><img src="examples/pielabeltype2.png" alt="Pie Chart Label Types Example - 2) Data Array Labels" /></div></div><pre class="programlisting"><?php# PHPlot Example: Pie Chart Label Types - Labels from data array# This requires PHPlot >= 5.6.0require_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();</pre><p>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.</p><div class="informalfigure"><div class="mediaobject"><img src="examples/pielabeltype3.png" alt="Pie Chart Label Types Example - 3) Segment Value Labels" /></div></div><pre class="programlisting"><?php# PHPlot Example: Pie Chart Label Types - Formatted values as labels# This requires PHPlot >= 5.6.0require_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();</pre><p>In this example, we want to use strings from an external array to labelthe pie segments. To do that, set the label source to 'index' to get asegment number starting with zero, and define a custom formatting callbackfunction to get the value for each label.</p><div class="informalfigure"><div class="mediaobject"><img src="examples/pielabeltype4.png" alt="Pie Chart Label Types Example - 4) Index and custom callback" /></div></div><pre class="programlisting"><?php# PHPlot Example: Pie Chart Label Types - Index and custom callback# This requires PHPlot >= 5.6.0require_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();</pre><p>The last example of pie chart labels shows how to use multiple label sourcesto make complex labels. In this case, we want the pie segment labels to showthe label string from the data array and the percentage. When multiplelabel sources are selected, PHPlot separates the fields with a space, thenpasses them to your custom formatting callback function as a single string.In order to separate them, use the <code class="function">explode()</code>. Sincethe data array labels might contain spaces, be sure to put this field last,and limit how many fields <code class="function">explode</code> will return.</p><div class="informalfigure"><div class="mediaobject"><img src="examples/pielabeltype5.png" alt="Pie Chart Label Types Example - 5) Multipart Labels" /></div></div><pre class="programlisting"><?php# PHPlot Example: Pie Chart Label Types - Multi-part labels# This requires PHPlot >= 5.6.0require_once 'phplot.php';require_once 'pielabeltypedata.php'; // Defines $data and $titlefunction 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();</pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ex-bubbles1.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="examples.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ex-drawmessage.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.40. Example - Bubbles Plot </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.42. Example - DrawMessage</td></tr></table></div></body></html>