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.25. Example - Creative Use of the Data Color 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-truecolor-histogram.html" title="5.24. Example - Using Truecolor To Make a Histogram" /><link rel="next" href="ex-colorcallbackbars.html" title="5.26. Example - Custom Bar Colors Using the Data Color Callback" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.25. Example - Creative Use of the Data Color Callback</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-truecolor-histogram.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-colorcallbackbars.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-colorcallbackgradient"></a>5.25. Example - Creative Use of the Data Color Callback</h2></div></div></div><p>
|
|
|
3 |
This example uses the data_color callback to vary the colors used in a
|
|
|
4 |
thinbarline plot.
|
|
|
5 |
The callback function <code class="function">getcolor</code> simply returns the row
|
|
|
6 |
number, which corresponds to each point's position along the X axis.
|
|
|
7 |
PHPlot will therefore use a different color for each plotted point (modulo
|
|
|
8 |
the number of defined colors). A large data color array is also defined,
|
|
|
9 |
with colors set to shades of blue from dark to light and back to dark.
|
|
|
10 |
</p><p>
|
|
|
11 |
A truecolor plot image is used to allow for more colors than would be
|
|
|
12 |
allowed in a palette image.
|
|
|
13 |
</p><p>
|
|
|
14 |
Using the data color callback is described in
|
|
|
15 |
<a class="xref" href="adv-datacolor-callback.html" title="4.5. Custom Data Color Selection">Section 4.5, “Custom Data Color Selection”</a>.
|
|
|
16 |
More information on callbacks can be found in
|
|
|
17 |
<a class="xref" href="callbacks.html" title="4.4. Callbacks">Section 4.4, “Callbacks”</a>.
|
|
|
18 |
More information on truecolor images can be found in
|
|
|
19 |
<a class="xref" href="adv-truecolor.html" title="4.3. Truecolor Images">Section 4.3, “Truecolor Images”</a>.
|
|
|
20 |
</p><div class="example"><a id="example-colorcallbackgradient"></a><p class="title"><strong>Example 5.25. Creative Use of the Data Color Callback</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/colorcallbackgradient.png" alt="Data Color Callback Example" /></div></div><pre class="programlisting"><?php
|
|
|
21 |
# PHPlot Example: Creative use of data colors
|
|
|
22 |
require_once 'phplot.php';
|
|
|
23 |
|
|
|
24 |
# Callback for picking a data color.
|
|
|
25 |
# PHPlot will call this every time it needs a data color.
|
|
|
26 |
# This simply returns the row number as the color index.
|
|
|
27 |
function getcolor($img, $unused, $row, $col)
|
|
|
28 |
{
|
|
|
29 |
return $row; // Use row, rather than column, as color index.
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
# Make some pseudo-random data.
|
|
|
33 |
mt_srand(1);
|
|
|
34 |
$data = array();
|
|
|
35 |
$value = 10;
|
|
|
36 |
for ($i = 0; $i < 500; $i++) {
|
|
|
37 |
$data[] = array('', $i, $value);
|
|
|
38 |
$value = max(0, $value + mt_rand(-9, 10));
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
# Make a color gradient array of blue:
|
|
|
42 |
$colors = array();
|
|
|
43 |
for ($b = 32; $b <= 255; $b += 2) $colors[] = array(0, 0, $b);
|
|
|
44 |
for ($b = 255; $b >= 32; $b -= 2) $colors[] = array(0, 0, $b);
|
|
|
45 |
|
|
|
46 |
# Use a truecolor plot image in order to get more colors.
|
|
|
47 |
$plot = new PHPlot_truecolor(800, 600);
|
|
|
48 |
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
|
|
|
49 |
|
|
|
50 |
$plot->SetPlotType('thinbarline');
|
|
|
51 |
$plot->SetDataType('data-data');
|
|
|
52 |
$plot->SetDataValues($data);
|
|
|
53 |
$plot->SetLineWidths(2);
|
|
|
54 |
$plot->SetDataColors($colors);
|
|
|
55 |
$plot->SetXTickPos('none');
|
|
|
56 |
$plot->SetPlotAreaWorld(0, 0, 500, NULL);
|
|
|
57 |
$plot->SetTitle('Meaningless Data with Color Gradient');
|
|
|
58 |
|
|
|
59 |
# Establish the function 'getcolor' as a data color selection callback.
|
|
|
60 |
$plot->SetCallback('data_color', 'getcolor');
|
|
|
61 |
|
|
|
62 |
$plot->DrawGraph();
|
|
|
63 |
</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-truecolor-histogram.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-colorcallbackbars.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.24. Example - Using Truecolor To Make a Histogram </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.26. Example - Custom Bar Colors Using the Data Color Callback</td></tr></table></div></body></html>
|