Blame | Letzte Änderung | Log anzeigen | RSS feed
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1"><LINK REL="Start" HREF="index.html"><LINK REL="Contents" HREF="toc.html"><LINK REL="Prev" HREF="8101Usingtheautomaticdatetimescale.html"><LINK REL="Next" HREF="811Adjustinglabelsonatextscale.html"><STYLE TYPE="text/css"><!--BODY { font-family: serif }H1 { font-family: sans-serif }H2 { font-family: sans-serif }H3 { font-family: sans-serif }H4 { font-family: sans-serif }H5 { font-family: sans-serif }H6 { font-family: sans-serif }SUB { font-size: smaller }SUP { font-size: smaller }PRE { font-family: monospace }A { text-decoration: none }--></STYLE></HEAD><BODY><A HREF="toc.html">Contents</A><A HREF="8101Usingtheautomaticdatetimescale.html">Previous</A><A HREF="811Adjustinglabelsonatextscale.html">Next</A><HR NOSHADE><H3><A NAME="8_10_2">8.10.2 Specifying a date/time scale with a manualcall-back</A></H3><P> In the following we will assume that all data points are specifiedby a tuple where the date/time is specified as a timestamp in second inthe same format as is returned by the PHP function time().</P><P> The trick here is to use a label formatting callback routine whichgets called to format each label on the scale.</P><P> What we do is that we specify that the X-scale should be an ordinary"int" scale (remember that the data values are timestamps which areintegers). We then install our custom label callback (with a call to <A href="../ref/Axis.html#_AXIS_SETLABELFORMATCALLBACK">SetLabelFormatCallback()</A>) which given a timestamp formats it to asuitable human readable form. In our example we will use the PHPfunction Date() for this purpose.</P><P> The callback we use will be<DIV class="phpscript"><CODE><FONT color="#000000"><FONT color="#0000BB"> </FONT><FONT color="#FF8000">// The callback that converts timestamp to minutes and seconds<BR></FONT><FONT color="#007700">function </FONT><FONT color="#0000BB">TimeCallback</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$aVal</FONT><FONT color="#007700">) {<BR> return </FONT><FONT color="#0000BB">Date</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'H:i:s'</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">$aVal</FONT><FONT color="#007700">);<BR>}</FONT><FONT color="#0000BB"></FONT></FONT></CODE></DIV></P><P> Using some random data we can now generate the following graph<DIV class="example"><BR> <A href="exframes/frame_dateaxisex1.html" target="blank"><IMG border="0"HEIGHT="250" src="img/dateaxisex1.png" WIDTH="324"></A><BR><B>Figure 78:</B> Example on how to format an axis to hold adate/time scale using and integer scale and a callback routine <A href="exframes/frame_dateaxisex1.html"target="blank">[src]</A> <P></P></DIV></P><P> In the above example we have specified the X-scale manually to makesure that the min/max values on the X-axis exactly matches the min/maxx-data values.</P><P> SetLabelFormatCallback() will be called for each of the displayedlabels and the argument passed on to the supplied function is thedefault label that the library is assigning that specific label.</P><P> This means that if the data to be displayed is based on, forexample, timestamps which are given in seconds the displayed valuemight not be aligned to how time/date values normally should bedisplayed.</P><P> Using integer scales this will not work very well since the librarydetermines label positions to be at even positions (e.g. every 2,5,10,20,50,100 etc) to suit the auto-scaling since the library will assumethat the data is integers and not timestamp values.</P><P> The best way to solve this is to use an integer X-scale togetherwith a a callback function with a manually specified scale. In order tosetup the scale a bit of manually work is needed. Depending on the datato be displayed one should ensure that the scale starts and ends atsuitable times and that the tick interval chosen fits with an evenmultiple of minutes, hours, days or what is best suited for the timerange that is to be displayed.</P><P> The following code example illustrates this. It creates some "fake"data that is assumed to be sampled time based data and sets up somesuitable scales and tick interval. This script may be used as a basisfor more advanced handling of the time data.<DIV class="phpscript">(File: timestampex01.php)<BR><CODE><FONT color="#000000"> <FONT color="#0000BB"><?php<BR></FONT><FONT color="#FF8000">// Example on how to treat and format timestamp as human readable labels<BR></FONT><FONT color="#007700">require_once(</FONT><FONT color="#DD0000">"../jpgraph.php"</FONT><FONT color="#007700">);<BR>require_once(</FONT><FONT color="#DD0000">"../jpgraph_line.php"</FONT><FONTcolor="#007700">);<BR><BR></FONT><FONT color="#FF8000">// Number of "fake" data points<BR></FONT><FONT color="#0000BB">DEFINE</FONT><FONT color="#007700">(</FONT><FONTcolor="#DD0000">'NDATAPOINTS'</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">500</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// Assume data points are sample every 10th second<BR></FONT><FONT color="#0000BB">DEFINE</FONT><FONT color="#007700">(</FONT><FONTcolor="#DD0000">'SAMPLERATE'</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">10</FONT><FONT color="#007700">); <BR><BR></FONT><FONT color="#FF8000">// Callback formatting function for the X-scale to convert timestamps<BR>// to hour and minutes.<BR></FONT><FONT color="#007700">function </FONT><FONT color="#0000BB">TimeCallback</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$aVal</FONT><FONT color="#007700">) {<BR> return </FONT><FONT color="#0000BB">Date</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'H:i'</FONT><FONT color="#007700">, </FONT><FONTcolor="#0000BB">$aVal</FONT><FONT color="#007700">);<BR>}<BR><BR></FONT><FONT color="#FF8000">// Get start time<BR></FONT><FONT color="#0000BB">$start </FONT><FONT color="#007700">= </FONT><FONTcolor="#0000BB">time</FONT><FONT color="#007700">();<BR></FONT><FONT color="#FF8000">// Set the start time to be on the closest minute just before the "start" timestamp<BR></FONT><FONT color="#0000BB">$adjstart </FONT><FONT color="#007700">= </FONT><FONT color="#0000BB">floor</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">$start </FONT><FONT color="#007700">/ </FONT><FONT color="#0000BB">60</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// Create a data set in range (20,100) and X-positions<BR>// We also apply a simple low pass filter on the data to make it less<BR>// random and a little smoother<BR></FONT><FONT color="#0000BB">$data </FONT><FONT color="#007700">= array();<BR></FONT><FONT color="#0000BB">$xdata </FONT><FONT color="#007700">= array();<BR></FONT><FONT color="#0000BB">$data</FONT><FONT color="#007700">[</FONT><FONTcolor="#0000BB">0</FONT><FONT color="#007700">] = </FONT><FONT color="#0000BB">rand</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">20</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$xdata</FONT><FONT color="#007700">[</FONT><FONTcolor="#0000BB">0</FONT><FONT color="#007700">] = </FONT><FONT color="#0000BB">$adjstart</FONT><FONT color="#007700">;<BR>for( </FONT><FONT color="#0000BB">$i</FONT><FONT color="#007700">=</FONT><FONTcolor="#0000BB">1</FONT><FONT color="#007700">; </FONT><FONT color="#0000BB">$i </FONT><FONT color="#007700">< </FONT><FONT color="#0000BB">NDATAPOINTS</FONT><FONT color="#007700">; ++</FONT><FONT color="#0000BB">$i </FONT><FONT color="#007700">) {<BR> </FONT><FONT color="#0000BB">$data</FONT><FONT color="#007700">[</FONT><FONTcolor="#0000BB">$i</FONT><FONT color="#007700">] = </FONT><FONT color="#0000BB">rand</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">20</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">)*</FONT><FONT color="#0000BB">0.2 </FONT><FONT color="#007700">+ </FONT><FONTcolor="#0000BB">$data</FONT><FONT color="#007700">[</FONT><FONT color="#0000BB">$i</FONT><FONT color="#007700">-</FONT><FONT color="#0000BB">1</FONT><FONTcolor="#007700">]*</FONT><FONT color="#0000BB">0.8</FONT><FONT color="#007700">;<BR> </FONT><FONT color="#0000BB">$xdata</FONT><FONT color="#007700">[</FONT><FONT color="#0000BB">$i</FONT><FONT color="#007700">] = </FONT><FONTcolor="#0000BB">$adjstart </FONT><FONT color="#007700">+ </FONT><FONT color="#0000BB">$i </FONT><FONT color="#007700">* </FONT><FONT color="#0000BB">SAMPLERATE</FONT><FONT color="#007700">;<BR>}<BR><BR></FONT><FONT color="#FF8000">// Assume that the data points represents data that is sampled every 10s<BR>// when determing the end value on the scale. We also add some extra<BR>// length to end on an even label tick.<BR></FONT><FONT color="#0000BB">$adjend </FONT><FONT color="#007700">= </FONT><FONTcolor="#0000BB">$adjstart </FONT><FONT color="#007700">+ (</FONT><FONT color="#0000BB">NDATAPOINTS</FONT><FONT color="#007700">+</FONT><FONT color="#0000BB">10</FONT><FONTcolor="#007700">)*</FONT><FONT color="#0000BB">10</FONT><FONT color="#007700">;<BR><BR></FONT><FONT color="#0000BB">$graph </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">Graph</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">500</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">250</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetMargin</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">40</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">20</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">30</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">50</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// Now specify the X-scale explicit but let the Y-scale be auto-scaled<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetScale</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">"intlin"</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$adjstart</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">$adjend</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">title</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">Set</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">"Example on TimeStamp Callback"</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// Setup the callback and adjust the angle of the labels<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">xaxis</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">SetLabelFormatCallback</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'TimeCallback'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">xaxis</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">SetLabelAngle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">90</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#FF8000">// Set the labels every 5min (i.e. 300seconds) and minor ticks every minute<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">xaxis</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">scale</FONT><FONT color="#007700">-></FONT><FONT color="#0000BB">ticks</FONT><FONTcolor="#007700">-></FONT><FONT color="#0000BB">Set</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">300</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">60</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#0000BB">$line </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">LinePlot</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$data</FONT><FONT color="#007700">,</FONT><FONTcolor="#0000BB">$xdata</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$line</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'lightblue'</FONT><FONT color="#007700">);<BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Add</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">$line</FONT><FONT color="#007700">);<BR><BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Stroke</FONT><FONT color="#007700">();<BR></FONT><FONT color="#0000BB">?><BR></FONT></FONT></CODE></DIV></P><HR NOSHADE><A HREF="toc.html">Contents</A><A HREF="8101Usingtheautomaticdatetimescale.html">Previous</A><A HREF="811Adjustinglabelsonatextscale.html">Next</A></BODY></HTML>