// col+1 skips over the row's label if ($d >= 80) { return 0; } if ($d >= 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->SetImageBorderType('plain'); // Improves presentation in the manual $plot->SetPlotType('bars'); $plot->SetDataValues($data); $plot->SetDataType('text-data'); $plot->SetTitle('Monthly Performance Rating'); # Turn off X Tick labels which have no meaning here. $plot->SetXTickPos('none'); # Force the Y axis to be exactly 0:100 $plot->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->SetCallback('data_color', 'pickcolor', $data); # The three colors are meaningful to the data color callback. $plot->SetDataColors(array('green', 'yellow', 'red')); # The legend will explain the use of the 3 colors. $plot->SetLegend(array('Exceeded expectations', 'Met expectations', 'Failed to meet expectations')); $plot->DrawGraph();
$row = array('', $x); for ($j = 0; $j < 10; $j++) { $row[] = mt_rand(0, $x + $j); } $data[] = $row; } # This gets imploded to be the title, after options are added on: $title = array("Lines plot"); if ($tp['truecolor']) { $p = new PHPlot_truecolor(1024, 768); $title[] = "Truecolor"; } else { $p = new PHPlot(1024, 768); $title[] = "Palette Color"; } $p->SetCallback('draw_setup', 'pre_plot'); $p->SetCallback('draw_all', 'post_plot'); # Reload data colors and apply alpha to all: if (isset($tp['alpha'])) { $p->SetDataColors(NULL, NULL, $tp['alpha']); $title[] = "Alpha=" . $tp['alpha']; } $p->SetFileFormat($tp['output']); $title[] = strtoupper($tp['output']) . ' Output'; if ($tp['antialias']) { $title[] = "Antialiased"; } if ($tp['noalphablend']) { $title[] = "No alpha blending"; } if (isset($tp['gamma'])) {
# Specify the font argument as NULL or '' to use the generic one. $plot->DrawText('', 0, $worst_x, $worst_y - 10, $red, 'Bad News!', 'center', 'bottom'); } # Begin main processing: # Fill the data array: $data = get_data(); # Find the best and worst months: get_best_worst($data, $best_index, $best_sales, $worst_index, $worst_sales); # Create the PHPlot object, set title, plot type, data array type, and data: $plot = new PHPlot(800, 600); $plot->SetTitle('Monthly Widget Sales'); $plot->SetPlotType('bars'); $plot->SetDataType('text-data'); $plot->SetDataValues($data); # Borders are needed for the manual: $plot->SetImageBorderType('plain'); # Select X data labels (not tick labels): $plot->SetXTickPos('none'); $plot->SetXTickLabelPos('none'); $plot->SetXDataLabelPos('plotdown'); # Format Y labels as "$nM" with no decimals, steps of 5: $plot->SetYLabelType('data', 0, '$', 'M'); $plot->SetYTickIncrement(5.0); # Force the bottom of the plot to be at Y=0, and omit # the bottom "$0M" tick label because it looks odd: $plot->SetPlotAreaWorld(NULL, 0); $plot->SetSkipBottomTick(True); # Establish the drawing callback to do the annotation: $plot->SetCallback('draw_all', 'annotate_plot', $plot); # Draw the graph: $plot->DrawGraph();
# Color callback - data-data-error plots, baseline # This is the baseline script. Other scripts define the variables below, # then include this script. # $plot_type : one of the types that accepts text-data (or text-data-yx) # $data_type : (only supports the default data-data-error) # $subtitle : Optional addition to title # Scripts define a color callback function 'pick_color' to test the callback. require_once 'phplot.php'; if (!isset($plot_type)) { $plot_type = 'lines'; } if (!isset($data_type)) { $data_type = 'data-data-error'; } $title = "Data Color Callback - {$plot_type}, {$data_type}"; $data = array(array('', 0, 10, 2, 2, 5, 2, 2), array('', 1, 11, 2, 1, 7, 1, 1), array('', 2, 13, 1, 1, 4, 1, 2), array('', 3, 16, 1, 2, 8, 2, 1), array('', 4, 20, 3, 0, 3, 2, 1), array('', 5, 25, 0, 2, 0, 2, 0)); $plot = new PHPlot(800, 600); if (function_exists('pick_color')) { $title .= "\nWith data color callback"; $plot->SetCallback('data_color', 'pick_color'); } else { $title .= "\nDefault data colors"; } if (isset($subtitle)) { $title .= ' ' . $subtitle; } $plot->SetDataValues($data); $plot->SetDataType($data_type); $plot->SetPlotType($plot_type); $plot->SetTitle($title); $plot->DrawGraph();
$color_index = imagecolorresolve($img, 146, 146, 146); } imagestring($img, 3, $x, $y, "CB: {$arg}", $color_index); $y += 20; # Mark it has being called: unset($wasnt_called[$arg]); } function fatal($message) { fwrite(STDERR, "ERROR: {$message}\n"); exit(1); } $data = array(array('A', -3, 6), array('B', -2, 4), array('C', -1, 2), array('D', 0, 0), array('E', 1, -2), array('F', 2, -4), array('G', 3, -6)); $p = new PHPlot(400, 300); foreach ($cblist as $cbname) { if (!$p->SetCallback($cbname, 'cb', $cbname)) { fatal("Invalid callback: {$cbname}"); } } # Test valid/invalid callbacks: if ($p->SetCallback('no-such-callback', 'cb')) { fatal("Did not return false for invalid SetCallback reason"); } if ($p->GetCallback('no-such-callback', 'cb')) { fatal("Did not return false for invalid GetCallback reason"); } if ($p->RemoveCallback('no-such-callback', 'cb')) { fatal("Did not return false for invalid RemoveCallback reason"); } foreach ($cblist as $cbname) { $v = $p->GetCallback($cbname);
<?php # $Id: data_table.example2.php 999 2011-08-05 19:00:48Z lbayuk $ # phplot / contrib / data_table example 2: Line plot with data table on the side require_once 'phplot.php'; require_once 'data_table.php'; $data = array(); for ($i = 0; $i < 20; $i++) { $data[] = array('', $i, 2 * $i, $i * $i); } // The $settings array configures the data table: $settings = array('headers' => array(NULL, 'X', '2Y', 'Y^2'), 'position' => array(640, 20), 'width' => 150, 'data' => $data, 'font' => 3); $plot = new PHPlot(800, 600); $plot->SetTitle('Line Plot with Data Table on Right Side'); $plot->SetDataValues($data); $plot->SetDataType('data-data'); $plot->SetPlotType('linepoints'); $plot->SetPlotAreaPixels(NULL, NULL, 630, NULL); $plot->SetCallback('draw_graph', 'draw_data_table', $settings); $plot->SetLegend(array('2Y', 'Y^2')); $plot->DrawGraph();
// Use data_color callback? # 2 data sets $data2 = array(array('', 0, 1), array('', 2, 3), array('', 3, 4), array('', 4, 5)); # 3 data sets: $data3 = array(array('', 0, 2, 3), array('', 4, 5, 6), array('', 5, 6, 7), array('', 6, 7, 8)); # A data_color callback which is the same as the standard # no-callback behavior, but presence of a callback changes # how colors are allocated. function sdc($img, $unused, $row, $col) { return $col; } $title = "Multiplot vs data colors, type {$plot_type}"; $p = new PHPlot(800, 800); if ($do_callback) { $p->SetCallback('data_color', 'sdc'); $title .= ", with data_color callback"; } $p->SetPrintImage(False); $p->SetTitle($title); $p->SetPlotType($plot_type); $p->SetLineStyles('solid'); $p->SetDataType('text-data'); $p->SetPlotAreaWorld(0, 0, 4, 10); $p->SetPlotAreaPixels(60, 60, 339, 339); $p->SetDataValues($data3); $p->DrawGraph(); $p->SetPlotAreaPixels(460, 60, 739, 339); $p->SetDataValues($data2); $p->DrawGraph(); $p->SetPlotAreaPixels(60, 460, 339, 739);
# Data for points, linepoints: $data = array(array('', 0, 0, 1), array('', 1, 2, 1), array('', 2, 4, 2), array('', 3, 6, 3), array('', 4, 8, 5), array('', 5, 10, 8), array('', 6, 12, 12), array('', 7, 14, 20)); } $plot = new PHPlot(800, 600); $plot->SetFailureImage(False); // No error images $plot->SetPrintImage(False); // No automatic output $plot->SetTitle("'{$plot_type}' {$what} with image map"); $plot->SetImageBorderType('plain'); $plot->SetDataValues($data); $plot->SetDataType($data_type); $plot->SetPlotType($plot_type); $plot->SetXTickIncrement(1); # Check return value in case callback isn't defined: if (!$plot->SetCallback('data_points', 'store_map', $data)) { die("Missing callback\n"); } $plot->DrawGraph(); $data_url = $plot->EncodeImage(); $html = <<<END <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>PHPlot Example - Inline Image</title> </head> <body> <h1>PHPlot Example - Inline Image</h1> <map name="map1"> {$image_map}</map>
# Now grab the font array, checking for old vs new name: if (isset($p->generic_font)) { $font = $p->generic_font; } elseif (isset($p->fonts['generic'])) { $font = $p->fonts['generic']; } else { die("drawtext.php failure: Unable to determine font class variable name\n"); } # Assign colors: $black = imagecolorallocate($img, 0, 0, 0); $white = imagecolorallocate($img, 0xff, 0xff, 0xff); $red = imagecolorallocate($img, 0xff, 0, 0); $green = imagecolorallocate($img, 0, 0xcc, 0); $blue = imagecolorallocate($img, 0, 0, 0xff); # Register the callback for drawing the bounding box: $p->SetCallback('debug_textbox', 'draw_bbox', $green); # Array indexes are factors to offset the point from center. # Note: These are for the corrected top/bottom meaning after PHPlot-5.0.4 $v_opts = array(1 => 'top', 0 => 'center', -1 => 'bottom'); $h_opts = array(1 => 'left', 0 => 'center', -1 => 'right'); $cx = $width / 2; $cy = $height / 2; $x_off = $height / 4.0; $y_off = $width / 4.0; # Because we don't let PHPlot finish drawing, we need to set the background. imagefilledrectangle($img, 0, 0, $width, $height, $white); if (isset($tp['line_spacing'])) { $p->SetLineSpacing($tp['line_spacing']); } # Draw a title: ImageString($img, '3', 5, 5, $title, $black);
{ global $warn_level, $danger_level; // Fetch the Z value from the array with rows // that look like: (label, X, Yi, Zi, Yi, Zi...) // $col is 0 for the first Y,Z pair, 1 for the second, etc. $z = $data[$row][2 * $col + 3]; if ($z < $warn_level) { return 0; } // Green if ($z < $danger_level) { return 1; } // Yellow return 2; // Red } $data = array(); mt_srand(1); for ($i = 0; $i < $n_p; $i++) { $data[] = array('', mt_rand(0, $max_x - 1), mt_rand(0, $max_y - 1), mt_rand(0, $max_z - 1)); } $p = new PHPlot(1024, 768); $p->SetTitle("Bubble Plot - Random Scatterplot with Custom Data Colors"); $p->SetCallback('data_color', 'getcolor', $data); $p->SetDataType('data-data-xyz'); $p->SetDataColors(array('green', 'yellow', 'red')); $p->SetPlotType('bubbles'); $p->SetPlotAreaWorld(0, 0, $max_x, $max_y); $p->SetDataValues($data); $p->DrawGraph();
<?php # $Id$ # PHPlot test: Box plot, with image map area outlines shown. # This produces a plot image with the areas that would be in an image map # outlined. It does not produce HTML or an image map. require_once 'phplot.php'; $data = array(array('A', 1, 10, 12, 15, 17, 20), array('B', 2, 5, 10, 16, 20, 24, 2, 3, 4), array('C', 3, 12, 13, 14, 15, 16, 20), array('D', 4, 10, 11, 12, 13, 14), array('E', 5, 12, 12, 15, 18, 20), array('F', 6, 12, 14, 16, 18, 18), array('G', 7, 10, 10, 15, 20, 20)); $data_type = 'data-data'; $plot_type = 'boxes'; $title = "PHPlot Test: {$plot_type} plot with image map areas outlined"; # Callback for 'data_points': Outline the area function store_map($im, $passthru, $shape, $row, $col, $x1, $y1, $x2, $y2) { static $color = NULL; if (!isset($color)) { $color = imagecolorallocate($im, 255, 0, 0); } imagesetthickness($im, 3); imagerectangle($im, $x1, $y1, $x2, $y2, $color); imagesetthickness($im, 1); } $plot = new PHPlot(800, 600); $plot->SetTitle($title); $plot->SetImageBorderType('plain'); $plot->SetDataValues($data); $plot->SetDataType($data_type); $plot->SetPlotType($plot_type); $plot->SetPlotAreaWorld(0, 0, 8, 25); $plot->SetCallback('data_points', 'store_map'); $plot->DrawGraph();
# Skip the test with older PHPlot versions that do not support the # DrawText() font argment variations. if (!defined('PHPlot::version_id') || PHPlot::version_id < 60000) { echo "Skipping test: missing new DrawText() font argument support\n"; exit(2); } function my_draw($img, $plot) { $black = imagecolorresolve($img, 0, 0, 0); $x = 100; $y = 50; $dy = 30; $plot->DrawText('', 0, $x, $y += $dy, $black, 'Font="" (generic): sans italic 12pt'); $plot->DrawText('generic', 0, $x, $y += $dy, $black, 'Font="generic": sans italic 12pt'); $plot->DrawText('legend', 0, $x, $y += $dy, $black, 'Font="legend": serif bold 14pt'); $plot->DrawText($plot->fonts['title'], 0, $x, $y += $dy, $black, 'Font=fonts["title"]: sans 12pt'); $plot->DrawText('x_title', 0, $x, $y += $dy, $black, 'Font="x_title": mono bold 10pt'); } $data = array(array('', 0, 0), array('', 10, 10)); $plot = new PHPlot(800, 600); $plot->SetTitle("DrawText() Font Argument Test\nTitle is sans 12pt"); $plot->SetDataType('data-data'); $plot->SetDataValues($data); $plot->SetPlotType('lines'); $plot->SetTTFPath($phplot_test_ttfdir); $plot->SetFontTTF('title', $phplot_test_ttfonts['sans'], 12); $plot->SetFontTTF('generic', $phplot_test_ttfonts['sansitalic'], 12); $plot->SetFontTTF('legend', $phplot_test_ttfonts['serifbold'], 14); $plot->SetFontTTF('x_title', $phplot_test_ttfonts['monobold'], 10); $plot->SetCallback('draw_all', 'my_draw', $plot); $plot->DrawGraph();