5.25. Example - Creative Use of the Data Color Callback

This example uses the data_color callback to vary the colors used in a thinbarline plot. The callback function getcolor 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.

A truecolor plot image is used to allow for more colors than would be allowed in a palette image.

Using the data color callback is described in Section 4.5, “Custom Data Color Selection”. More information on callbacks can be found in Section 4.4, “Callbacks”. More information on truecolor images can be found in Section 4.3, “Truecolor Images”.

Example 5.25. Creative Use of the Data Color Callback

Data Color Callback Example
# 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.
$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->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');


SourceForge.net Logo

This version of the manual was produced for the PHPlot Sourceforge project web service site, which requires the logo on each page.

To download a logo-free copy of the manual, see the PHPlot project downloads area.