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.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="2
0%" 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>
This example uses the data_color callback to vary the colors used in a
thinbarline plot.
The callback function <code class="function">getcolor</code> simply returns the row
number, which corresponds to each point's position along the X axis.
PHPlot will therefore use a different color for each plotted point (modulo
the number of defined colors). A large data color array is also defined,
with colors set to shades of blue from dark to light and back to dark.
</p><p>
A truecolor plot image is used to allow for more colors than would be
allowed in a palette image.
</p><p>
Using the data color callback is described in
<a class="xref" href="adv-datacolor-callback.html" title="4.5. Custom Data Color Selection">Section 4.5, “Custom Data Color Selection”</a>.
More information on callbacks can be found in
<a class="xref" href="callbacks.html" title="4.4. Callbacks">Section 4.4, “Callbacks”</a>.
More information on truecolor images can be found in
<a class="xref" href="adv-truecolor.html" title="4.3. Truecolor Images">Section 4.3, “Truecolor Images”</a>.
</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
# PHPlot Example: Creative use of data colors
require_once 'phplot.php';
# Callback for picking a data color.
# PHPlot will call this every time it needs a data color.
# This simply returns the row number as the color index.
function getcolor($img, $unused, $row, $col)
{
return $row; // Use row, rather than column, as color index.
}
# Make some pseudo-random data.
mt_srand(1);
$data = array();
$value = 10;
for ($i = 0; $i < 500; $i++) {
$data[] = array('', $i, $value);
$value = max(0, $value + mt_rand(-9, 10));
}
# Make a color gradient array of blue:
$colors = array();
for ($b = 32; $b <= 255; $b += 2) $colors[] = array(0, 0, $b);
for ($b = 255; $b >= 32; $b -= 2) $colors[] = array(0, 0, $b);
# Use a truecolor plot image in order to get more colors.
$plot = new PHPlot_truecolor(800, 600);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetPlotType('thinbarline');
$plot->SetDataType('data-data');
$plot->SetDataValues($data);
$plot->SetLineWidths(2);
$plot->SetDataColors($colors);
$plot->SetXTickPos('none');
$plot->SetPlotAreaWorld(0, 0, 500, NULL);
$plot->SetTitle('Meaningless Data with Color Gradient');
# Establish the function 'getcolor' as a data color selection callback.
$plot->SetCallback('data_color', 'getcolor');
$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-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>