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.43. Example - Custom Data Value Label Formatting</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-drawmessage.html" title="5.42. Example - DrawMessage" /><link rel="next" href="ex-imagemap-bars.html" title="5.44. Example - Image Map from Bar Chart" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.43. Example - Custom Data Value Label Formatting</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-drawmessage.html">Prev</a>
</td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-imagemap-bars.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-dlexformat"></a>5.43. Example - Custom Data Value Label Formatting</h2></div></div></div><p>
This example shows some advanced usage of label formatting. A user-defined
function can be used to format labels, using the <code class="literal">custom</code>
label type in <a class="xref" href="SetXLabelType.html" title="SetXLabelType"><span class="refentrytitle">SetXLabelType</span></a>, <a class="xref" href="SetYLabelType.html" title="SetYLabelType"><span class="refentrytitle">SetYLabelType</span></a>,
<a class="xref" href="SetXDataLabelType.html" title="SetXDataLabelType"><span class="refentrytitle">SetXDataLabelType</span></a>, and <a class="xref" href="SetYDataLabelType.html" title="SetYDataLabelType"><span class="refentrytitle">SetYDataLabelType</span></a>.
The user-defined function will have access to the row and column of the data
point being plotted (for most data value labels), or the column (for axis data
labels), and it can use these variables to control the formatting.
</p><p>
This example produces a linepoints plot with multiple data sets and with
labels identifying the data values. The goal is to label only the greatest
Y value for each X value. Before plotting, the data array is scanned to
find the column index of the largest Y value for each row, and this
information is stored in an array <code class="literal">$max_indexes</code>. That
array is passed to the custom label formatting function as the pass-through
argument. The label formatting function uses the array, along with the row
and column of the point being labeled, to determine if a label should be
produced or not.
</p><p>
Access to the data point row and column index was added in PHPlot-5.8.0.
</p><div class="example"><a id="example-dlexformat"></a><p class="title"><strong>Example 5.43. Custom Data Value Label Formatting</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/dlexformat.png" alt="Custom Data Value Label Formatting Example" /></div></div><pre class="programlisting"><?php
# PHPlot Example: Custom label formatting using row and column
# This example produces a linepoints plot with only the maximum Y value
# for each X value labeled. This is an example of a custom label
# formatting with access to the data array position.
# This requires PHPlot > 5.7.0
require_once 'phplot.php';
# Build a data array. The values are psuedo-random integers, but the first
# and last rows have all Y=0. Return the completed data array.
function make_data_array($n_rows, $n_y_per_row)
{
mt_srand(10); // For repeatable results
$data[0] = array('', 0) + array_fill(2, $n_y_per_row, 0);
for ($i = 1; $i < $n_rows - 1; $i++) {
$row = array('', $i);
for ($j = 0; $j < $n_y_per_row; $j++) $row[] = mt_rand(0, 999);
$data[] = $row;
}
if ($n_rows > 1)
$data[] = array('', $n_rows - 1) + array_fill(2, $n_y_per_row, 0);
return $data;
}
# Find the index of the largest Y for each X in the data array.
# Build an array $max_indexes such that $max_indexes[$row]=$column means
# the $column-th Y value is the largest in row $row (where $row and $column
# are zero-based). However, if max Y<=0 in the row, mark this with $column=-1
# to prevent labeling. Returns the array $max_indexes.
function find_max_indexes($data)
{
$max_indexes = array();
foreach ($data as $row) {
$max_index = 0;
$max_y = $row[2]; // Skip label and X value
// Process remaining values in the row:
for ($j = 3; $j < count($row); $j++) {
if ($row[$j] > $max_y) {
$max_y = $row[$j];
$max_index = $j - 2; // Offset by 2 for label and X value
}
}
if ($max_y <= 0) $max_index = -1; // Will suppress the label
$max_indexes[] = $max_index;
}
return $max_indexes;
}
# Custom label formatting function: Return an empty string, unless this is
# the largest value in the row.
function fmt_label($value, $maxes, $row, $column)
{
if ($maxes[$row] == $column) return $value;
return "";
}
# Get the data array with 11 rows, 6 values per row:
$data = make_data_array(11, 6);
# Process the data array to find the largest Y per row:
$max_indexes = find_max_indexes($data);
# Now plot the data:
$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain'); // For presentation in the manual
$plot->SetPlotType('linepoints');
$plot->SetDataType('data-data');
$plot->SetDataValues($data);
$plot->SetTitle('Linepoints plot with only max Y values labeled');
$plot->SetYDataLabelPos('plotin');
$plot->SetYDataLabelType('custom', 'fmt_label', $max_indexes);
$plot->SetLineStyles('solid');
$plot->SetYTickIncrement(100);
$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-drawmessage.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-imagemap-bars.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.42. Example - DrawMessage </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.44. Example - Image Map from Bar Chart</td></tr></table></div></body></html>