Subversion Repositories cheapmusic

Rev

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.26. Example - Custom Bar Colors Using 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-colorcallbackgradient.html" title="5.25. Example - Creative Use of the Data Color Callback" /><link rel="next" href="ex-horizbar.html" title="5.27. Example - Horizontal Bar Chart" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.26. Example - Custom Bar Colors Using the Data Color Callback</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-colorcallbackgradient.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-horizbar.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-colorcallbackbars"></a>5.26. Example - Custom Bar Colors Using the Data Color Callback</h2></div></div></div><p>
This example uses the data_color callback to customize the colors in a bar
chart. The goal is to have the bar colors depend on the value of the bar
(rather than the position of the bar in the bar group). Bars above 80% will
be drawn in green, bars below 60% will be red, and bars in between those
two values will be yellow.
</p><p>
The function <code class="function">pickcolor</code> is the data_color callback.
It accesses the data array using its pass-through argument, indexing into
it with the current row and column. (Note col+1 is used to skip over the row
label.) It then checks the data value, and returns an index into the data
colors array: 0, 1, or 2, depending on the value.
</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, &#8220;Custom Data Color Selection&#8221;</a>.
More information on callbacks can be found in <a class="xref" href="callbacks.html" title="4.4. Callbacks">Section 4.4, &#8220;Callbacks&#8221;</a>.
</p><div class="example"><a id="example-colorcallbackbars"></a><p class="title"><strong>Example 5.26. Custom Bar Colors Using the Data Color Callback</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/colorcallbackbars.png" alt="Custom Bar Colors Example" /></div></div><pre class="programlisting">&lt;?php
# PHPlot Example: Bar chart with bar color depending on value
require_once 'phplot.php';

# Callback for picking a data color.
# PHPlot will call this every time it needs a data color.
# This returns a color index which depends on the data value.
# Color 0 is for values &gt;= 80%, 1 is for &gt;= 60%, 2 is for &lt; 60%.
# The data_array must have 'text-data' type.
function pickcolor($img, $data_array, $row, $col)
{
  $d = $data_array[$row][$col+1]; // col+1 skips over the row's label
  if ($d &gt;= 80) return 0;
  if ($d &gt;= 60) return 1;
  return 2;
}

# The data array has our monthly performance as a percentage.
$data = array(
    array('Jan',  95), array('Feb',  75), array('Mar',  83),
    array('Apr',  66), array('May',  90), array('Jun',  80),
    array('Jul',  70), array('Aug',  50), array('Sep',  60),
    array('Oct',  70), array('Nov',  80), array('Dec',  45),
);

$plot = new PHPlot(800, 600);
$plot-&gt;SetImageBorderType('plain'); // Improves presentation in the manual
$plot-&gt;SetPlotType('bars');
$plot-&gt;SetDataValues($data);
$plot-&gt;SetDataType('text-data');
$plot-&gt;SetTitle('Monthly Performance Rating');

# Turn off X Tick labels which have no meaning here.
$plot-&gt;SetXTickPos('none');

# Force the Y axis to be exactly 0:100
$plot-&gt;SetPlotAreaWorld(NULL, 0, NULL, 100);

# Establish the function 'pickcolor' as a data color selection callback.
# Set the $data array as the pass-through argument, so the function has
# access to the data values without relying on global variables.
$plot-&gt;SetCallback('data_color', 'pickcolor', $data);

# The three colors are meaningful to the data color callback.
$plot-&gt;SetDataColors(array('green', 'yellow', 'red'));

# The legend will explain the use of the 3 colors.
$plot-&gt;SetLegend(array('Exceeded expectations', 'Met expectations',
  'Failed to meet expectations'));

$plot-&gt;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-colorcallbackgradient.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-horizbar.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.25. Example - Creative Use of the Data Color Callback </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.27. Example - Horizontal Bar Chart</td></tr></table></div></body></html>