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.22. Example - Annotating a Plot Using a 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-stackedarea1.html" title="5.21. Example - Stacked Area Plot" /><link rel="next" href="ex-webform.html" title="5.23. Example - Complete Web Form with Plot" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.22. Example - Annotating a Plot Using a Callback</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-stackedarea1.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-webform.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-annotate"></a>5.22. Example - Annotating a Plot Using a Callback</h2></div></div></div><p>
This is an advanced example that uses a drawing callback to
add annotations to a plot. More information on this topic can
be found in <a class="xref" href="callbacks.html#callbacks-drawing" title="4.4.5. Using Callbacks to Annotate Plots">Section 4.4.5, &#8220;Using Callbacks to Annotate Plots&#8221;</a>,
where this example is described in detail.
</p><div class="example"><a id="example-annotate"></a><p class="title"><strong>Example 5.22. Annotated Plot</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/annotate.png" alt="Annotated Plot Example" /></div></div><pre class="programlisting">&lt;?php
# PHPlot Example: Annotating a plot using callbacks
# Note: This example is coded for PHPlot &gt; 5.0.7
require_once 'phplot.php';

# Get the Sales data. In real life, this would most likely come from
# a database or external file. For this example, we will use 'random'
# data, but with a fixed seed for repeatable results.
function get_data()
{
    mt_srand(1);
    $data = array();
    # Build an array with 12 arrays of (month_name, value):
    for ($month = 1; $month &lt;= 12; $month++)
        $data[] = array(strftime('%b', mktime(12, 0, 0, $month, 1)),
                        5 + mt_rand(5, 40));
    return $data;
}

# Find the best and worst sales data.
# Gets the Y value (sales data) and X value. For PHPlot text-data data,
# the X values are assigned as 0.5, 1.5, 2.5, etc.
# The data array is in 'text-data' format: array of array(label, Y)...
function get_best_worst($data,
    &amp;$best_index, &amp;$best_sales, &amp;$worst_index, &amp;$worst_sales)
{
  $best_sales = NULL;
  $worst_sales = NULL;
  foreach ($data as $x =&gt; $point) {
      if (!isset($best_sales) || $point[1] &gt; $best_sales) {
          $best_sales = $point[1];
          $best_index = $x + 0.5;
      }
      if (!isset($worst_sales) || $point[1] &lt; $worst_sales) {
          $worst_sales = $point[1];
          $worst_index = $x + 0.5;
      }
  }
}

# Plot annotation callback.
# The pass-through argument is the PHPlot object.
function annotate_plot($img, $plot)
{
    global $best_index, $best_sales, $worst_index, $worst_sales;

    # Allocate our own colors, rather than poking into the PHPlot object:
    $red = imagecolorresolve($img, 255, 0, 0);
    $green = imagecolorresolve($img, 0, 216, 0);

    # Get the pixel coordinates of the data points for the best and worst:
    list($best_x, $best_y) = $plot-&gt;GetDeviceXY($best_index, $best_sales);
    list($worst_x, $worst_y) = $plot-&gt;GetDeviceXY($worst_index, $worst_sales);

    # Draw ellipses centered on those two points:
    imageellipse($img, $best_x, $best_y, 50, 20, $green);
    imageellipse($img, $worst_x, $worst_y, 50, 20, $red);

    # Place some text above the points:
    $font = '3';
    $fh = imagefontheight($font);
    $fw = imagefontwidth($font);
    imagestring($img, $font, $best_x-$fw*4, $best_y-$fh-10,
                'Good Job!', $green);

    # We can also use the PHPlot internal function for text.
    # It does the center/bottom alignment calculations for us.
    # Specify the font argument as NULL or '' to use the generic one.
    $plot-&gt;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-&gt;SetTitle('Monthly Widget Sales');
$plot-&gt;SetPlotType('bars');
$plot-&gt;SetDataType('text-data');
$plot-&gt;SetDataValues($data);
# Borders are needed for the manual:
$plot-&gt;SetImageBorderType('plain');

# Select X data labels (not tick labels):
$plot-&gt;SetXTickPos('none');
$plot-&gt;SetXTickLabelPos('none');
$plot-&gt;SetXDataLabelPos('plotdown');

# Format Y labels as "$nM" with no decimals, steps of 5:
$plot-&gt;SetYLabelType('data', 0, '$', 'M');
$plot-&gt;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-&gt;SetPlotAreaWorld(NULL, 0);
$plot-&gt;SetSkipBottomTick(True);

# Establish the drawing callback to do the annotation:
$plot-&gt;SetCallback('draw_all', 'annotate_plot', $plot);

# Draw the graph:
$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-stackedarea1.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-webform.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.21. Example - Stacked Area Plot </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.23. Example - Complete Web Form with Plot</td></tr></table></div></body></html>