98 |
- |
1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
|
|
|
2 |
<!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>
|
|
|
3 |
This example shows some advanced usage of label formatting. A user-defined
|
|
|
4 |
function can be used to format labels, using the <code class="literal">custom</code>
|
|
|
5 |
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>,
|
|
|
6 |
<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>.
|
|
|
7 |
The user-defined function will have access to the row and column of the data
|
|
|
8 |
point being plotted (for most data value labels), or the column (for axis data
|
|
|
9 |
labels), and it can use these variables to control the formatting.
|
|
|
10 |
</p><p>
|
|
|
11 |
This example produces a linepoints plot with multiple data sets and with
|
|
|
12 |
labels identifying the data values. The goal is to label only the greatest
|
|
|
13 |
Y value for each X value. Before plotting, the data array is scanned to
|
|
|
14 |
find the column index of the largest Y value for each row, and this
|
|
|
15 |
information is stored in an array <code class="literal">$max_indexes</code>. That
|
|
|
16 |
array is passed to the custom label formatting function as the pass-through
|
|
|
17 |
argument. The label formatting function uses the array, along with the row
|
|
|
18 |
and column of the point being labeled, to determine if a label should be
|
|
|
19 |
produced or not.
|
|
|
20 |
</p><p>
|
|
|
21 |
Access to the data point row and column index was added in PHPlot-5.8.0.
|
|
|
22 |
</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
|
|
|
23 |
# PHPlot Example: Custom label formatting using row and column
|
|
|
24 |
# This example produces a linepoints plot with only the maximum Y value
|
|
|
25 |
# for each X value labeled. This is an example of a custom label
|
|
|
26 |
# formatting with access to the data array position.
|
|
|
27 |
# This requires PHPlot > 5.7.0
|
|
|
28 |
require_once 'phplot.php';
|
|
|
29 |
|
|
|
30 |
# Build a data array. The values are psuedo-random integers, but the first
|
|
|
31 |
# and last rows have all Y=0. Return the completed data array.
|
|
|
32 |
function make_data_array($n_rows, $n_y_per_row)
|
|
|
33 |
{
|
|
|
34 |
mt_srand(10); // For repeatable results
|
|
|
35 |
$data[0] = array('', 0) + array_fill(2, $n_y_per_row, 0);
|
|
|
36 |
for ($i = 1; $i < $n_rows - 1; $i++) {
|
|
|
37 |
$row = array('', $i);
|
|
|
38 |
for ($j = 0; $j < $n_y_per_row; $j++) $row[] = mt_rand(0, 999);
|
|
|
39 |
$data[] = $row;
|
|
|
40 |
}
|
|
|
41 |
if ($n_rows > 1)
|
|
|
42 |
$data[] = array('', $n_rows - 1) + array_fill(2, $n_y_per_row, 0);
|
|
|
43 |
return $data;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
# Find the index of the largest Y for each X in the data array.
|
|
|
47 |
# Build an array $max_indexes such that $max_indexes[$row]=$column means
|
|
|
48 |
# the $column-th Y value is the largest in row $row (where $row and $column
|
|
|
49 |
# are zero-based). However, if max Y<=0 in the row, mark this with $column=-1
|
|
|
50 |
# to prevent labeling. Returns the array $max_indexes.
|
|
|
51 |
function find_max_indexes($data)
|
|
|
52 |
{
|
|
|
53 |
$max_indexes = array();
|
|
|
54 |
foreach ($data as $row) {
|
|
|
55 |
$max_index = 0;
|
|
|
56 |
$max_y = $row[2]; // Skip label and X value
|
|
|
57 |
// Process remaining values in the row:
|
|
|
58 |
for ($j = 3; $j < count($row); $j++) {
|
|
|
59 |
if ($row[$j] > $max_y) {
|
|
|
60 |
$max_y = $row[$j];
|
|
|
61 |
$max_index = $j - 2; // Offset by 2 for label and X value
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
if ($max_y <= 0) $max_index = -1; // Will suppress the label
|
|
|
65 |
$max_indexes[] = $max_index;
|
|
|
66 |
}
|
|
|
67 |
return $max_indexes;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
# Custom label formatting function: Return an empty string, unless this is
|
|
|
71 |
# the largest value in the row.
|
|
|
72 |
function fmt_label($value, $maxes, $row, $column)
|
|
|
73 |
{
|
|
|
74 |
if ($maxes[$row] == $column) return $value;
|
|
|
75 |
return "";
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
# Get the data array with 11 rows, 6 values per row:
|
|
|
79 |
$data = make_data_array(11, 6);
|
|
|
80 |
# Process the data array to find the largest Y per row:
|
|
|
81 |
$max_indexes = find_max_indexes($data);
|
|
|
82 |
|
|
|
83 |
# Now plot the data:
|
|
|
84 |
$plot = new PHPlot(800, 600);
|
|
|
85 |
$plot->SetImageBorderType('plain'); // For presentation in the manual
|
|
|
86 |
$plot->SetPlotType('linepoints');
|
|
|
87 |
$plot->SetDataType('data-data');
|
|
|
88 |
$plot->SetDataValues($data);
|
|
|
89 |
$plot->SetTitle('Linepoints plot with only max Y values labeled');
|
|
|
90 |
$plot->SetYDataLabelPos('plotin');
|
|
|
91 |
$plot->SetYDataLabelType('custom', 'fmt_label', $max_indexes);
|
|
|
92 |
$plot->SetLineStyles('solid');
|
|
|
93 |
$plot->SetYTickIncrement(100);
|
|
|
94 |
$plot->DrawGraph();
|
|
|
95 |
</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>
|