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.22. Example - Annotating a Plot Using a Callback</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-stackedarea1.html" title="5.21. Example - Stacked Area Plot" /><link rel="next" href="ex-webform.html" title="5.23. Example - Complete Web Form with Plot" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.22. Example - Annotating a Plot Using a Callback</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-stackedarea1.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-webform.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-annotate"></a>5.22. Example - Annotating a Plot Using a Callback</h2></div></div></div><p>
|
|
|
3 |
This is an advanced example that uses a drawing callback to
|
|
|
4 |
add annotations to a plot. More information on this topic can
|
|
|
5 |
be found in <a class="xref" href="callbacks.html#callbacks-drawing" title="4.4.5. Using Callbacks to Annotate Plots">Section 4.4.5, “Using Callbacks to Annotate Plots”</a>,
|
|
|
6 |
where this example is described in detail.
|
|
|
7 |
</p><div class="example"><a id="example-annotate"></a><p class="title"><strong>Example 5.22. Annotated Plot</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/annotate.png" alt="Annotated Plot Example" /></div></div><pre class="programlisting"><?php
|
|
|
8 |
# PHPlot Example: Annotating a plot using callbacks
|
|
|
9 |
# Note: This example is coded for PHPlot > 5.0.7
|
|
|
10 |
require_once 'phplot.php';
|
|
|
11 |
|
|
|
12 |
# Get the Sales data. In real life, this would most likely come from
|
|
|
13 |
# a database or external file. For this example, we will use 'random'
|
|
|
14 |
# data, but with a fixed seed for repeatable results.
|
|
|
15 |
function get_data()
|
|
|
16 |
{
|
|
|
17 |
mt_srand(1);
|
|
|
18 |
$data = array();
|
|
|
19 |
# Build an array with 12 arrays of (month_name, value):
|
|
|
20 |
for ($month = 1; $month <= 12; $month++)
|
|
|
21 |
$data[] = array(strftime('%b', mktime(12, 0, 0, $month, 1)),
|
|
|
22 |
5 + mt_rand(5, 40));
|
|
|
23 |
return $data;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
# Find the best and worst sales data.
|
|
|
27 |
# Gets the Y value (sales data) and X value. For PHPlot text-data data,
|
|
|
28 |
# the X values are assigned as 0.5, 1.5, 2.5, etc.
|
|
|
29 |
# The data array is in 'text-data' format: array of array(label, Y)...
|
|
|
30 |
function get_best_worst($data,
|
|
|
31 |
&$best_index, &$best_sales, &$worst_index, &$worst_sales)
|
|
|
32 |
{
|
|
|
33 |
$best_sales = NULL;
|
|
|
34 |
$worst_sales = NULL;
|
|
|
35 |
foreach ($data as $x => $point) {
|
|
|
36 |
if (!isset($best_sales) || $point[1] > $best_sales) {
|
|
|
37 |
$best_sales = $point[1];
|
|
|
38 |
$best_index = $x + 0.5;
|
|
|
39 |
}
|
|
|
40 |
if (!isset($worst_sales) || $point[1] < $worst_sales) {
|
|
|
41 |
$worst_sales = $point[1];
|
|
|
42 |
$worst_index = $x + 0.5;
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
# Plot annotation callback.
|
|
|
48 |
# The pass-through argument is the PHPlot object.
|
|
|
49 |
function annotate_plot($img, $plot)
|
|
|
50 |
{
|
|
|
51 |
global $best_index, $best_sales, $worst_index, $worst_sales;
|
|
|
52 |
|
|
|
53 |
# Allocate our own colors, rather than poking into the PHPlot object:
|
|
|
54 |
$red = imagecolorresolve($img, 255, 0, 0);
|
|
|
55 |
$green = imagecolorresolve($img, 0, 216, 0);
|
|
|
56 |
|
|
|
57 |
# Get the pixel coordinates of the data points for the best and worst:
|
|
|
58 |
list($best_x, $best_y) = $plot->GetDeviceXY($best_index, $best_sales);
|
|
|
59 |
list($worst_x, $worst_y) = $plot->GetDeviceXY($worst_index, $worst_sales);
|
|
|
60 |
|
|
|
61 |
# Draw ellipses centered on those two points:
|
|
|
62 |
imageellipse($img, $best_x, $best_y, 50, 20, $green);
|
|
|
63 |
imageellipse($img, $worst_x, $worst_y, 50, 20, $red);
|
|
|
64 |
|
|
|
65 |
# Place some text above the points:
|
|
|
66 |
$font = '3';
|
|
|
67 |
$fh = imagefontheight($font);
|
|
|
68 |
$fw = imagefontwidth($font);
|
|
|
69 |
imagestring($img, $font, $best_x-$fw*4, $best_y-$fh-10,
|
|
|
70 |
'Good Job!', $green);
|
|
|
71 |
|
|
|
72 |
# We can also use the PHPlot internal function for text.
|
|
|
73 |
# It does the center/bottom alignment calculations for us.
|
|
|
74 |
# Specify the font argument as NULL or '' to use the generic one.
|
|
|
75 |
$plot->DrawText('', 0, $worst_x, $worst_y-10, $red,
|
|
|
76 |
'Bad News!', 'center', 'bottom');
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
# Begin main processing:
|
|
|
80 |
|
|
|
81 |
# Fill the data array:
|
|
|
82 |
$data = get_data();
|
|
|
83 |
|
|
|
84 |
# Find the best and worst months:
|
|
|
85 |
get_best_worst($data, $best_index, $best_sales, $worst_index, $worst_sales);
|
|
|
86 |
|
|
|
87 |
# Create the PHPlot object, set title, plot type, data array type, and data:
|
|
|
88 |
$plot = new PHPlot(800, 600);
|
|
|
89 |
$plot->SetTitle('Monthly Widget Sales');
|
|
|
90 |
$plot->SetPlotType('bars');
|
|
|
91 |
$plot->SetDataType('text-data');
|
|
|
92 |
$plot->SetDataValues($data);
|
|
|
93 |
# Borders are needed for the manual:
|
|
|
94 |
$plot->SetImageBorderType('plain');
|
|
|
95 |
|
|
|
96 |
# Select X data labels (not tick labels):
|
|
|
97 |
$plot->SetXTickPos('none');
|
|
|
98 |
$plot->SetXTickLabelPos('none');
|
|
|
99 |
$plot->SetXDataLabelPos('plotdown');
|
|
|
100 |
|
|
|
101 |
# Format Y labels as "$nM" with no decimals, steps of 5:
|
|
|
102 |
$plot->SetYLabelType('data', 0, '$', 'M');
|
|
|
103 |
$plot->SetYTickIncrement(5.0);
|
|
|
104 |
|
|
|
105 |
# Force the bottom of the plot to be at Y=0, and omit
|
|
|
106 |
# the bottom "$0M" tick label because it looks odd:
|
|
|
107 |
$plot->SetPlotAreaWorld(NULL, 0);
|
|
|
108 |
$plot->SetSkipBottomTick(True);
|
|
|
109 |
|
|
|
110 |
# Establish the drawing callback to do the annotation:
|
|
|
111 |
$plot->SetCallback('draw_all', 'annotate_plot', $plot);
|
|
|
112 |
|
|
|
113 |
# Draw the graph:
|
|
|
114 |
$plot->DrawGraph();
|
|
|
115 |
</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-stackedarea1.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-webform.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.21. Example - Stacked Area Plot </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.23. Example - Complete Web Form with Plot</td></tr></table></div></body></html>
|