Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2
<HTML>
3
<HEAD>
4
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
5
<LINK REL="Start" HREF="index.html">
6
<LINK REL="Contents" HREF="toc.html">
7
<LINK REL="Prev" HREF="8101Usingtheautomaticdatetimescale.html">
8
<LINK REL="Next" HREF="811Adjustinglabelsonatextscale.html">
9
<STYLE TYPE="text/css"><!--
10
BODY { font-family: serif }
11
H1 { font-family: sans-serif }
12
H2 { font-family: sans-serif }
13
H3 { font-family: sans-serif }
14
H4 { font-family: sans-serif }
15
H5 { font-family: sans-serif }
16
H6 { font-family: sans-serif }
17
SUB { font-size: smaller }
18
SUP { font-size: smaller }
19
PRE { font-family: monospace }
20
A { text-decoration: none }
21
--></STYLE>
22
</HEAD>
23
<BODY>
24
<A HREF="toc.html">Contents</A>
25
<A HREF="8101Usingtheautomaticdatetimescale.html">Previous</A>
26
<A HREF="811Adjustinglabelsonatextscale.html">Next</A>
27
<HR NOSHADE>
28
<H3><A NAME="8_10_2">8.10.2 Specifying a date/time scale with a manual
29
 call-back</A></H3>
30
<P> In the following we will assume that all data points are specified
31
 by a tuple where the date/time is specified as a timestamp in second in
32
 the same format as is returned by the PHP function time().</P>
33
<P> The trick here is to use a label formatting callback routine which
34
 gets called to format each label on the scale.</P>
35
<P> What we do is that we specify that the X-scale should be an ordinary
36
 &quot;int&quot; scale (remember that the data values are timestamps which are
37
 integers). We then install our custom label callback (with a call to <A href="../ref/Axis.html#_AXIS_SETLABELFORMATCALLBACK">
38
 SetLabelFormatCallback()</A>) which given a timestamp formats it to a
39
 suitable human readable form. In our example we will use the PHP
40
 function Date() for this purpose.</P>
41
<P> The callback we use will be<DIV class="phpscript"><CODE><FONT color="#000000">
42
 <FONT color="#0000BB">&nbsp;</FONT><FONT color="#FF8000">
43
//&nbsp;The&nbsp;callback&nbsp;that&nbsp;converts&nbsp;timestamp&nbsp;to&nbsp;minutes&nbsp;and&nbsp;seconds
44
<BR></FONT><FONT color="#007700">function&nbsp;</FONT><FONT color="#0000BB">
45
TimeCallback</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
46
$aVal</FONT><FONT color="#007700">)&nbsp;{
47
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</FONT><FONT color="#0000BB">Date</FONT><FONT color="#007700">
48
(</FONT><FONT color="#DD0000">'H:i:s'</FONT><FONT color="#007700">,</FONT><FONT
49
color="#0000BB">$aVal</FONT><FONT color="#007700">);
50
<BR>}</FONT><FONT color="#0000BB"></FONT></FONT></CODE></DIV></P>
51
<P> Using some random data we can now generate the following graph<DIV class="example">
52
<BR> <A href="exframes/frame_dateaxisex1.html" target="blank"><IMG border="0"
53
HEIGHT="250"  src="img/dateaxisex1.png" WIDTH="324"></A>
54
<BR><B>Figure 78:</B> Example on how to format an axis to hold a
55
 date/time scale using and integer scale and a callback routine <A href="exframes/frame_dateaxisex1.html"
56
target="blank">[src]</A>&nbsp;
57
<P></P>
58
</DIV></P>
59
<P> In the above example we have specified the X-scale manually to make
60
 sure that the min/max values on the X-axis exactly matches the min/max
61
 x-data values.</P>
62
<P> SetLabelFormatCallback() will be called for each of the displayed
63
 labels and the argument passed on to the supplied function is the
64
 default label that the library is assigning that specific label.</P>
65
<P> This means that if the data to be displayed is based on, for
66
 example, timestamps which are given in seconds the displayed value
67
 might not be aligned to how time/date values normally should be
68
 displayed.</P>
69
<P> Using integer scales this will not work very well since the library
70
 determines label positions to be at even positions (e.g. every 2,5,10,
71
 20,50,100 etc) to suit the auto-scaling since the library will assume
72
 that the data is integers and not timestamp values.</P>
73
<P> The best way to solve this is to use an integer X-scale together
74
 with a a callback function with a manually specified scale. In order to
75
 setup the scale a bit of manually work is needed. Depending on the data
76
 to be displayed one should ensure that the scale starts and ends at
77
 suitable times and that the tick interval chosen fits with an even
78
 multiple of minutes, hours, days or what is best suited for the time
79
 range that is to be displayed.</P>
80
<P> The following code example illustrates this. It creates some &quot;fake&quot;
81
 data that is assumed to be sampled time based data and sets up some
82
 suitable scales and tick interval. This script may be used as a basis
83
 for more advanced handling of the time data.<DIV class="phpscript">
84
(File: timestampex01.php)
85
<BR><CODE><FONT color="#000000"> <FONT color="#0000BB">&lt;?php
86
<BR></FONT><FONT color="#FF8000">
87
//&nbsp;Example&nbsp;on&nbsp;how&nbsp;to&nbsp;treat&nbsp;and&nbsp;format&nbsp;timestamp&nbsp;as&nbsp;human&nbsp;readable&nbsp;labels
88
<BR></FONT><FONT color="#007700">require_once(</FONT><FONT color="#DD0000">
89
&quot;../jpgraph.php&quot;</FONT><FONT color="#007700">);
90
<BR>require_once(</FONT><FONT color="#DD0000">&quot;../jpgraph_line.php&quot;</FONT><FONT
91
color="#007700">);
92
<BR>
93
<BR></FONT><FONT color="#FF8000">//&nbsp;Number&nbsp;of&nbsp;&quot;fake&quot;&nbsp;data&nbsp;points
94
<BR></FONT><FONT color="#0000BB">DEFINE</FONT><FONT color="#007700">(</FONT><FONT
95
color="#DD0000">'NDATAPOINTS'</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">
96
500</FONT><FONT color="#007700">);
97
<BR>
98
<BR></FONT><FONT color="#FF8000">
99
//&nbsp;Assume&nbsp;data&nbsp;points&nbsp;are&nbsp;sample&nbsp;every&nbsp;10th&nbsp;second
100
<BR></FONT><FONT color="#0000BB">DEFINE</FONT><FONT color="#007700">(</FONT><FONT
101
color="#DD0000">'SAMPLERATE'</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">
102
10</FONT><FONT color="#007700">);&nbsp;
103
<BR>
104
<BR></FONT><FONT color="#FF8000">
105
//&nbsp;Callback&nbsp;formatting&nbsp;function&nbsp;for&nbsp;the&nbsp;X-scale&nbsp;to&nbsp;convert&nbsp;timestamps
106
<BR>//&nbsp;to&nbsp;hour&nbsp;and&nbsp;minutes.
107
<BR></FONT><FONT color="#007700">function&nbsp;</FONT><FONT color="#0000BB">
108
TimeCallback</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
109
$aVal</FONT><FONT color="#007700">)&nbsp;{
110
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</FONT><FONT color="#0000BB">Date</FONT><FONT color="#007700">
111
(</FONT><FONT color="#DD0000">'H:i'</FONT><FONT color="#007700">,&nbsp;</FONT><FONT
112
color="#0000BB">$aVal</FONT><FONT color="#007700">);
113
<BR>}
114
<BR>
115
<BR></FONT><FONT color="#FF8000">//&nbsp;Get&nbsp;start&nbsp;time
116
<BR></FONT><FONT color="#0000BB">$start&nbsp;</FONT><FONT color="#007700">=&nbsp;</FONT><FONT
117
color="#0000BB">time</FONT><FONT color="#007700">();
118
<BR></FONT><FONT color="#FF8000">
119
//&nbsp;Set&nbsp;the&nbsp;start&nbsp;time&nbsp;to&nbsp;be&nbsp;on&nbsp;the&nbsp;closest&nbsp;minute&nbsp;just&nbsp;before&nbsp;the&nbsp;&quot;start&quot;&nbsp;timestamp
120
<BR></FONT><FONT color="#0000BB">$adjstart&nbsp;</FONT><FONT color="#007700">
121
=&nbsp;</FONT><FONT color="#0000BB">floor</FONT><FONT color="#007700">(</FONT><FONT
122
color="#0000BB">$start&nbsp;</FONT><FONT color="#007700">/&nbsp;</FONT><FONT color="#0000BB">
123
60</FONT><FONT color="#007700">);
124
<BR>
125
<BR></FONT><FONT color="#FF8000">
126
//&nbsp;Create&nbsp;a&nbsp;data&nbsp;set&nbsp;in&nbsp;range&nbsp;(20,100)&nbsp;and&nbsp;X-positions
127
<BR>
128
//&nbsp;We&nbsp;also&nbsp;apply&nbsp;a&nbsp;simple&nbsp;low&nbsp;pass&nbsp;filter&nbsp;on&nbsp;the&nbsp;data&nbsp;to&nbsp;make&nbsp;it&nbsp;less
129
<BR>//&nbsp;random&nbsp;and&nbsp;a&nbsp;little&nbsp;smoother
130
<BR></FONT><FONT color="#0000BB">$data&nbsp;</FONT><FONT color="#007700">
131
=&nbsp;array();
132
<BR></FONT><FONT color="#0000BB">$xdata&nbsp;</FONT><FONT color="#007700">
133
=&nbsp;array();
134
<BR></FONT><FONT color="#0000BB">$data</FONT><FONT color="#007700">[</FONT><FONT
135
color="#0000BB">0</FONT><FONT color="#007700">]&nbsp;=&nbsp;</FONT><FONT color="#0000BB">
136
rand</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">20</FONT><FONT
137
color="#007700">,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">
138
);
139
<BR></FONT><FONT color="#0000BB">$xdata</FONT><FONT color="#007700">[</FONT><FONT
140
color="#0000BB">0</FONT><FONT color="#007700">]&nbsp;=&nbsp;</FONT><FONT color="#0000BB">
141
$adjstart</FONT><FONT color="#007700">;
142
<BR>for(&nbsp;</FONT><FONT color="#0000BB">$i</FONT><FONT color="#007700">=</FONT><FONT
143
color="#0000BB">1</FONT><FONT color="#007700">;&nbsp;</FONT><FONT color="#0000BB">
144
$i&nbsp;</FONT><FONT color="#007700">&lt;&nbsp;</FONT><FONT color="#0000BB">
145
NDATAPOINTS</FONT><FONT color="#007700">;&nbsp;++</FONT><FONT color="#0000BB">
146
$i&nbsp;</FONT><FONT color="#007700">)&nbsp;{
147
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT color="#0000BB">$data</FONT><FONT color="#007700">[</FONT><FONT
148
color="#0000BB">$i</FONT><FONT color="#007700">]&nbsp;=&nbsp;</FONT><FONT color="#0000BB">
149
rand</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">20</FONT><FONT
150
color="#007700">,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">
151
)*</FONT><FONT color="#0000BB">0.2&nbsp;</FONT><FONT color="#007700">+&nbsp;</FONT><FONT
152
color="#0000BB">$data</FONT><FONT color="#007700">[</FONT><FONT color="#0000BB">
153
$i</FONT><FONT color="#007700">-</FONT><FONT color="#0000BB">1</FONT><FONT
154
color="#007700">]*</FONT><FONT color="#0000BB">0.8</FONT><FONT color="#007700">
155
;
156
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT color="#0000BB">$xdata</FONT><FONT color="#007700">
157
[</FONT><FONT color="#0000BB">$i</FONT><FONT color="#007700">]&nbsp;=&nbsp;</FONT><FONT
158
color="#0000BB">$adjstart&nbsp;</FONT><FONT color="#007700">+&nbsp;</FONT><FONT color="#0000BB">
159
$i&nbsp;</FONT><FONT color="#007700">*&nbsp;</FONT><FONT color="#0000BB">
160
SAMPLERATE</FONT><FONT color="#007700">;
161
<BR>}
162
<BR>
163
<BR></FONT><FONT color="#FF8000">
164
//&nbsp;Assume&nbsp;that&nbsp;the&nbsp;data&nbsp;points&nbsp;represents&nbsp;data&nbsp;that&nbsp;is&nbsp;sampled&nbsp;every&nbsp;10s
165
<BR>//&nbsp;when&nbsp;determing&nbsp;the&nbsp;end&nbsp;value&nbsp;on&nbsp;the&nbsp;scale.&nbsp;We&nbsp;also&nbsp;add&nbsp;some&nbsp;extra
166
<BR>//&nbsp;length&nbsp;to&nbsp;end&nbsp;on&nbsp;an&nbsp;even&nbsp;label&nbsp;tick.
167
<BR></FONT><FONT color="#0000BB">$adjend&nbsp;</FONT><FONT color="#007700">=&nbsp;</FONT><FONT
168
color="#0000BB">$adjstart&nbsp;&nbsp;</FONT><FONT color="#007700">+&nbsp;(</FONT><FONT color="#0000BB">
169
NDATAPOINTS</FONT><FONT color="#007700">+</FONT><FONT color="#0000BB">10</FONT><FONT
170
color="#007700">)*</FONT><FONT color="#0000BB">10</FONT><FONT color="#007700">
171
;
172
<BR>
173
<BR></FONT><FONT color="#0000BB">$graph&nbsp;</FONT><FONT color="#007700">
174
=&nbsp;new&nbsp;</FONT><FONT color="#0000BB">Graph</FONT><FONT color="#007700">(</FONT><FONT
175
color="#0000BB">500</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">
176
250</FONT><FONT color="#007700">);
177
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
178
color="#0000BB">SetMargin</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
179
40</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">20</FONT><FONT
180
color="#007700">,</FONT><FONT color="#0000BB">30</FONT><FONT color="#007700">
181
,</FONT><FONT color="#0000BB">50</FONT><FONT color="#007700">);
182
<BR>
183
<BR></FONT><FONT color="#FF8000">
184
//&nbsp;Now&nbsp;specify&nbsp;the&nbsp;X-scale&nbsp;explicit&nbsp;but&nbsp;let&nbsp;the&nbsp;Y-scale&nbsp;be&nbsp;auto-scaled
185
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
186
color="#0000BB">SetScale</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
187
&quot;intlin&quot;</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT
188
color="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">
189
,</FONT><FONT color="#0000BB">$adjstart</FONT><FONT color="#007700">,</FONT><FONT
190
color="#0000BB">$adjend</FONT><FONT color="#007700">);
191
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
192
color="#0000BB">title</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
193
Set</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
194
&quot;Example&nbsp;on&nbsp;TimeStamp&nbsp;Callback&quot;</FONT><FONT color="#007700">);
195
<BR>
196
<BR></FONT><FONT color="#FF8000">
197
//&nbsp;Setup&nbsp;the&nbsp;callback&nbsp;and&nbsp;adjust&nbsp;the&nbsp;angle&nbsp;of&nbsp;the&nbsp;labels
198
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
199
color="#0000BB">xaxis</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
200
SetLabelFormatCallback</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
201
'TimeCallback'</FONT><FONT color="#007700">);
202
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
203
color="#0000BB">xaxis</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
204
SetLabelAngle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
205
90</FONT><FONT color="#007700">);
206
<BR>
207
<BR></FONT><FONT color="#FF8000">
208
//&nbsp;Set&nbsp;the&nbsp;labels&nbsp;every&nbsp;5min&nbsp;(i.e.&nbsp;300seconds)&nbsp;and&nbsp;minor&nbsp;ticks&nbsp;every&nbsp;minute
209
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
210
color="#0000BB">xaxis</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
211
scale</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">ticks</FONT><FONT
212
color="#007700">-&gt;</FONT><FONT color="#0000BB">Set</FONT><FONT color="#007700">
213
(</FONT><FONT color="#0000BB">300</FONT><FONT color="#007700">,</FONT><FONT
214
color="#0000BB">60</FONT><FONT color="#007700">);
215
<BR>
216
<BR></FONT><FONT color="#0000BB">$line&nbsp;</FONT><FONT color="#007700">
217
=&nbsp;new&nbsp;</FONT><FONT color="#0000BB">LinePlot</FONT><FONT color="#007700">
218
(</FONT><FONT color="#0000BB">$data</FONT><FONT color="#007700">,</FONT><FONT
219
color="#0000BB">$xdata</FONT><FONT color="#007700">);
220
<BR></FONT><FONT color="#0000BB">$line</FONT><FONT color="#007700">-&gt;</FONT><FONT
221
color="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
222
'lightblue'</FONT><FONT color="#007700">);
223
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
224
color="#0000BB">Add</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
225
$line</FONT><FONT color="#007700">);
226
<BR>
227
<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
228
color="#0000BB">Stroke</FONT><FONT color="#007700">();
229
<BR></FONT><FONT color="#0000BB">?&gt;
230
<BR></FONT></FONT></CODE></DIV></P>
231
<HR NOSHADE>
232
<A HREF="toc.html">Contents</A>
233
<A HREF="8101Usingtheautomaticdatetimescale.html">Previous</A>
234
<A HREF="811Adjustinglabelsonatextscale.html">Next</A>
235
</BODY>
236
</HTML>