98 |
- |
1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
|
|
|
2 |
<!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>
|
|
|
3 |
This example uses an X tick anchor (see <a class="xref" href="SetXTickAnchor.html" title="SetXTickAnchor"><span class="refentrytitle">SetXTickAnchor</span></a>) to
|
|
|
4 |
offset the X tick marks and grid lines. Hourly data is being plotted, for
|
|
|
5 |
one week, and the goal is to have the tick marks and grid lines separate
|
|
|
6 |
the days of the week, regardless of the time during the day of the initial
|
|
|
7 |
data point.
|
|
|
8 |
</p><p>
|
|
|
9 |
In this example, both X axis data labels and X tick labels are turned on.
|
|
|
10 |
The data labels display weekday names, and the tick labels display the dates.
|
|
|
11 |
Having both labels on is usually something to avoid, because the labels will
|
|
|
12 |
overlay.
|
|
|
13 |
In this case, however, only 1 in 24 points has a data label (those points
|
|
|
14 |
representing noon). Tick marks are also every 24 hours, but anchored at
|
|
|
15 |
midnight. So the tick and data labels are separated.
|
|
|
16 |
To avoid any overlap, the tick labels are drawn vertically (90 degrees)
|
|
|
17 |
while the data labels are horizontal.
|
|
|
18 |
</p><p>
|
|
|
19 |
(Note that <a class="xref" href="SetXLabelAngle.html" title="SetXLabelAngle"><span class="refentrytitle">SetXLabelAngle</span></a> sets the angle for both tick
|
|
|
20 |
and data labels, while <a class="xref" href="SetXDataLabelAngle.html" title="SetXDataLabelAngle"><span class="refentrytitle">SetXDataLabelAngle</span></a> sets the angle
|
|
|
21 |
for data labels only. PHPlot does this to maintain compatibility.
|
|
|
22 |
As a result, since the example here wants to turn the tick labels only
|
|
|
23 |
(from the default 0 degrees), we also need to turn the data labels back to 0.
|
|
|
24 |
The order of the calls does not matter, however.
|
|
|
25 |
The same is true for label formatting with
|
|
|
26 |
<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>.
|
|
|
27 |
To get non-default tick label formatting and default data label formatting
|
|
|
28 |
requires two calls as shown in the example.)
|
|
|
29 |
</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"><?php
|
|
|
30 |
# PHPlot Example: Using an X tick anchor to control grid lines
|
|
|
31 |
# This example is based on a question on the PHPlot forum on 5/8/2011.
|
|
|
32 |
# It requires PHPlot >= 5.4.0
|
|
|
33 |
require_once 'phplot.php';
|
|
|
34 |
|
|
|
35 |
# The first data point was recorded at this date/time: (always top of an hour)
|
|
|
36 |
# Example: 5/1/2011 at 10:00am
|
|
|
37 |
$year = 2011;
|
|
|
38 |
$month = 5;
|
|
|
39 |
$day = 1;
|
|
|
40 |
$hour = 10;
|
|
|
41 |
|
|
|
42 |
# Number of hourly data points, e.g. 168 for 1 week's worth:
|
|
|
43 |
$n_points = 168;
|
|
|
44 |
# ==================================================================
|
|
|
45 |
|
|
|
46 |
# Timestamp for the first data point:
|
|
|
47 |
$time0 = mktime($hour, 0, 0, $month, $day, $year); // H M S m d y
|
|
|
48 |
|
|
|
49 |
mt_srand(1); // For repeatable, identical results
|
|
|
50 |
|
|
|
51 |
# Build the PHPlot data array from the base data, and base timestamp.
|
|
|
52 |
$data = array();
|
|
|
53 |
$ts = $time0;
|
|
|
54 |
$tick_anchor = NULL;
|
|
|
55 |
$d = 5; // Data value
|
|
|
56 |
for ($i = 0; $i < $n_points; $i++) {
|
|
|
57 |
|
|
|
58 |
# Decode this point's timestamp as hour 0-23:
|
|
|
59 |
$hour = date('G', $ts);
|
|
|
60 |
|
|
|
61 |
# Label noon data points with the weekday name, all others unlabelled.
|
|
|
62 |
$label = ($hour == 12) ? strftime('%A', $ts) : '';
|
|
|
63 |
|
|
|
64 |
# Remember the first midnight datapoint seen for use as X tick anchor:
|
|
|
65 |
if (!isset($tick_anchor) && $hour == 0)
|
|
|
66 |
$tick_anchor = $ts;
|
|
|
67 |
|
|
|
68 |
# Make a random data point, and add a row to the data array:
|
|
|
69 |
$d += mt_rand(-200, 250) / 100;
|
|
|
70 |
$data[] = array($label, $ts, $d);
|
|
|
71 |
|
|
|
72 |
# Step to next hour:
|
|
|
73 |
$ts += 3600;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$plot = new PHPlot(800, 600);
|
|
|
77 |
$plot->SetImageBorderType('plain'); // For presentation in the manual
|
|
|
78 |
$plot->SetTitle('Hourly Data Example Plot');
|
|
|
79 |
$plot->SetDataType('data-data');
|
|
|
80 |
$plot->SetDataValues($data);
|
|
|
81 |
$plot->SetPlotType('lines');
|
|
|
82 |
|
|
|
83 |
# Make the X tick marks (and therefore X grid lines) 24 hours apart:
|
|
|
84 |
$plot->SetXTickIncrement(60 * 60 * 24);
|
|
|
85 |
$plot->SetDrawXGrid(True);
|
|
|
86 |
|
|
|
87 |
# Anchor the X tick marks at midnight. This makes the X grid lines act as
|
|
|
88 |
# separators between days of the week, regardless of the starting hour.
|
|
|
89 |
# (Except this messes up around daylight saving time changes.)
|
|
|
90 |
$plot->SetXTickAnchor($tick_anchor);
|
|
|
91 |
|
|
|
92 |
# We want both X axis data labels and X tick labels displayed. They will
|
|
|
93 |
# be positioned in a way that prevents them from overwriting.
|
|
|
94 |
$plot->SetXDataLabelPos('plotdown');
|
|
|
95 |
$plot->SetXTickLabelPos('plotdown');
|
|
|
96 |
|
|
|
97 |
# Increase the left and right margins to leave room for weekday labels.
|
|
|
98 |
$plot->SetMarginsPixels(50, 50);
|
|
|
99 |
|
|
|
100 |
# Tick labels will be formatted as date/times, showing the date:
|
|
|
101 |
$plot->SetXLabelType('time', '%Y-%m-%d');
|
|
|
102 |
# ... but then we must reset the data label formatting to no formatting.
|
|
|
103 |
$plot->SetXDataLabelType('');
|
|
|
104 |
|
|
|
105 |
# Show tick labels (with dates) at 90 degrees, to fit between the data labels.
|
|
|
106 |
$plot->SetXLabelAngle(90);
|
|
|
107 |
# ... but then we must reset the data labels to 0 degrees.
|
|
|
108 |
$plot->SetXDataLabelAngle(0);
|
|
|
109 |
|
|
|
110 |
# Force the Y range to 0:100.
|
|
|
111 |
$plot->SetPlotAreaWorld(NULL, 0, NULL, 100);
|
|
|
112 |
|
|
|
113 |
# Now draw the graph:
|
|
|
114 |
$plot->DrawGraph();
|
|
|
115 |
</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>
|