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.38. Example - Hourly Data Using X Tick Anchor</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-ytickanchor.html" title="5.37. Example - Setting a Y Tick Anchor" /><link rel="next" href="ex-encodeimage.html" title="5.39. Example - Embedding Image with EncodeImage" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.38. Example - Hourly Data Using X Tick Anchor</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-ytickanchor.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-encodeimage.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-xtickanchor"></a>5.38. Example - Hourly Data Using X Tick Anchor</h2></div></div></div><p>
This example uses an X tick anchor (see <a class="xref" href="SetXTickAnchor.html" title="SetXTickAnchor"><span class="refentrytitle">SetXTickAnchor</span></a>) to
offset the X tick marks and grid lines. Hourly data is being plotted, for
one week, and the goal is to have the tick marks and grid lines separate
the days of the week, regardless of the time during the day of the initial
data point.
</p><p>
In this example, both X axis data labels and X tick labels are turned on.
The data labels display weekday names, and the tick labels display the dates.
Having both labels on is usually something to avoid, because the labels will
overlay.
In this case, however, only 1 in 24 points has a data label (those points
representing noon). Tick marks are also every 24 hours, but anchored at
midnight. So the tick and data labels are separated.
To avoid any overlap, the tick labels are drawn vertically (90 degrees)
while the data labels are horizontal.
</p><p>
(Note that <a class="xref" href="SetXLabelAngle.html" title="SetXLabelAngle"><span class="refentrytitle">SetXLabelAngle</span></a> sets the angle for both tick
and data labels, while <a class="xref" href="SetXDataLabelAngle.html" title="SetXDataLabelAngle"><span class="refentrytitle">SetXDataLabelAngle</span></a> sets the angle
for data labels only. PHPlot does this to maintain compatibility.
As a result, since the example here wants to turn the tick labels only
(from the default 0 degrees), we also need to turn the data labels back to 0.
The order of the calls does not matter, however.
The same is true for label formatting with
<a class="xref" href="SetXLabelType.html" title="SetXLabelType"><span class="refentrytitle">SetXLabelType</span></a> and <a class="xref" href="SetXDataLabelType.html" title="SetXDataLabelType"><span class="refentrytitle">SetXDataLabelType</span></a>.
To get non-default tick label formatting and default data label formatting
requires two calls as shown in the example.)
</p><div class="example"><a id="example-xtickanchor"></a><p class="title"><strong>Example 5.38. Hourly Data Using X Tick Anchor</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/xtickanchor.png" alt="Hourly Data Using X Tick Anchor" /></div></div><pre class="programlisting">&lt;?php
# PHPlot Example: Using an X tick anchor to control grid lines
# This example is based on a question on the PHPlot forum on 5/8/2011.
# It requires PHPlot &gt;= 5.4.0
require_once 'phplot.php';

# The first data point was recorded at this date/time: (always top of an hour)
# Example: 5/1/2011 at 10:00am
$year = 2011;
$month = 5;
$day = 1;
$hour = 10;

# Number of hourly data points, e.g. 168 for 1 week's worth:
$n_points = 168;
# ==================================================================

# Timestamp for the first data point:
$time0 = mktime($hour, 0, 0, $month, $day, $year); // H M S m d y

mt_srand(1); // For repeatable, identical results

# Build the PHPlot data array from the base data, and base timestamp.
$data = array();
$ts = $time0;
$tick_anchor = NULL;
$d = 5; // Data value
for ($i = 0; $i &lt; $n_points; $i++) {

    # Decode this point's timestamp as hour 0-23:
    $hour = date('G', $ts);

    # Label noon data points with the weekday name, all others unlabelled.
    $label = ($hour == 12) ? strftime('%A', $ts) : '';

    # Remember the first midnight datapoint seen for use as X tick anchor:
    if (!isset($tick_anchor) &amp;&amp; $hour == 0)
        $tick_anchor = $ts;

    # Make a random data point, and add a row to the data array:
    $d += mt_rand(-200, 250) / 100; 
    $data[] = array($label, $ts, $d);

    # Step to next hour:
    $ts += 3600;
}

$plot = new PHPlot(800, 600);
$plot-&gt;SetImageBorderType('plain'); // For presentation in the manual
$plot-&gt;SetTitle('Hourly Data Example Plot');
$plot-&gt;SetDataType('data-data');
$plot-&gt;SetDataValues($data);
$plot-&gt;SetPlotType('lines');

# Make the X tick marks (and therefore X grid lines) 24 hours apart:
$plot-&gt;SetXTickIncrement(60 * 60 * 24);
$plot-&gt;SetDrawXGrid(True);

# Anchor the X tick marks at midnight. This makes the X grid lines act as
# separators between days of the week, regardless of the starting hour.
# (Except this messes up around daylight saving time changes.)
$plot-&gt;SetXTickAnchor($tick_anchor);

# We want both X axis data labels and X tick labels displayed. They will
# be positioned in a way that prevents them from overwriting.
$plot-&gt;SetXDataLabelPos('plotdown');
$plot-&gt;SetXTickLabelPos('plotdown');

# Increase the left and right margins to leave room for weekday labels.
$plot-&gt;SetMarginsPixels(50, 50);

# Tick labels will be formatted as date/times, showing the date:
$plot-&gt;SetXLabelType('time', '%Y-%m-%d');
# ... but then we must reset the data label formatting to no formatting.
$plot-&gt;SetXDataLabelType('');

# Show tick labels (with dates) at 90 degrees, to fit between the data labels.
$plot-&gt;SetXLabelAngle(90);
# ... but then we must reset the data labels to 0 degrees.
$plot-&gt;SetXDataLabelAngle(0);

# Force the Y range to 0:100.
$plot-&gt;SetPlotAreaWorld(NULL, 0, NULL, 100);

# Now 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-ytickanchor.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-encodeimage.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.37. Example - Setting a Y Tick Anchor </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.39. Example - Embedding Image with EncodeImage</td></tr></table></div></body></html>