undef HALF_FREQUENCY_SENDING_TO_CLIENT
[ryzomcore.git] / web / public_php / admin / jpgraph / jpgraph_date.php
blob21a0b8ebbfa520511793b0136830f0775f72840c
1 <?php
2 /*=======================================================================
3 // File: JPGRAPH_DATE.PHP
4 // Description: Classes to handle Date scaling
5 // Created: 2005-05-02
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_date.php,v 1.1 2006/07/07 13:37:14 powles Exp $
8 //
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
13 DEFINE('HOURADJ_1',0+30);
14 DEFINE('HOURADJ_2',1+30);
15 DEFINE('HOURADJ_3',2+30);
16 DEFINE('HOURADJ_4',3+30);
17 DEFINE('HOURADJ_6',4+30);
18 DEFINE('HOURADJ_12',5+30);
20 DEFINE('MINADJ_1',0+20);
21 DEFINE('MINADJ_5',1+20);
22 DEFINE('MINADJ_10',2+20);
23 DEFINE('MINADJ_15',3+20);
24 DEFINE('MINADJ_30',4+20);
26 DEFINE('SECADJ_1',0);
27 DEFINE('SECADJ_5',1);
28 DEFINE('SECADJ_10',2);
29 DEFINE('SECADJ_15',3);
30 DEFINE('SECADJ_30',4);
33 DEFINE('YEARADJ_1',0+30);
34 DEFINE('YEARADJ_2',1+30);
35 DEFINE('YEARADJ_5',2+30);
37 DEFINE('MONTHADJ_1',0+20);
38 DEFINE('MONTHADJ_6',1+20);
40 DEFINE('DAYADJ_1',0);
41 DEFINE('DAYADJ_WEEK',1);
42 DEFINE('DAYADJ_7',1);
44 DEFINE('SECPERYEAR',31536000);
45 DEFINE('SECPERDAY',86400);
46 DEFINE('SECPERHOUR',3600);
47 DEFINE('SECPERMIN',60);
50 class DateScale extends LinearScale {
51 var $date_format = '';
52 var $iStartAlign = false, $iEndAlign = false;
53 var $iStartTimeAlign = false, $iEndTimeAlign = false;
55 //---------------
56 // CONSTRUCTOR
57 function DateScale($aMin=0,$aMax=0,$aType='x') {
58 assert($aType=="x");
59 assert($aMin<=$aMax);
61 $this->type=$aType;
62 $this->scale=array($aMin,$aMax);
63 $this->world_size=$aMax-$aMin;
64 $this->ticks = new LinearTicks();
65 $this->intscale=true;
69 //------------------------------------------------------------------------------------------
70 // Utility Function AdjDate()
71 // Description: Will round a given time stamp to an even year, month or day
72 // argument.
73 //------------------------------------------------------------------------------------------
75 function AdjDate($aTime,$aRound=0,$aYearType=false,$aMonthType=false,$aDayType=false) {
76 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
77 $h=0;$i=0;$s=0;
78 if( $aYearType !== false ) {
79 $yearAdj = array(0=>1, 1=>2, 2=>5);
80 if( $aRound == 0 ) {
81 $y = floor($y/$yearAdj[$aYearType])*$yearAdj[$aYearType];
83 else {
84 ++$y;
85 $y = ceil($y/$yearAdj[$aYearType])*$yearAdj[$aYearType];
87 $m=1;$d=1;
89 elseif( $aMonthType !== false ) {
90 $monthAdj = array(0=>1, 1=>6);
91 if( $aRound == 0 ) {
92 $m = floor($m/$monthAdj[$aMonthType])*$monthAdj[$aMonthType];
93 $d=1;
95 else {
96 ++$m;
97 $m = ceil($m/$monthAdj[$aMonthType])*$monthAdj[$aMonthType];
98 $d=1;
101 elseif( $aDayType !== false ) {
102 if( $aDayType == 0 ) {
103 if( $aRound == 1 ) {
104 //++$d;
105 $h=23;$i=59;$s=59;
108 else {
109 // Adjust to an even week boundary.
110 $w = (int)date('w',$aTime); // Day of week 0=Sun, 6=Sat
111 if( true ) { // Adjust to start on Mon
112 if( $w==0 ) $w=6;
113 else --$w;
115 if( $aRound == 0 ) {
116 $d -= $w;
118 else {
119 $d += (7-$w);
120 $h=23;$i=59;$s=59;
124 return mktime($h,$i,$s,$m,$d,$y);
128 //------------------------------------------------------------------------------------------
129 // Wrapper for AdjDate that will round a timestamp to an even date rounding
130 // it downwards.
131 //------------------------------------------------------------------------------------------
132 function AdjStartDate($aTime,$aYearType=false,$aMonthType=false,$aDayType=false) {
133 return $this->AdjDate($aTime,0,$aYearType,$aMonthType,$aDayType);
136 //------------------------------------------------------------------------------------------
137 // Wrapper for AdjDate that will round a timestamp to an even date rounding
138 // it upwards
139 //------------------------------------------------------------------------------------------
140 function AdjEndDate($aTime,$aYearType=false,$aMonthType=false,$aDayType=false) {
141 return $this->AdjDate($aTime,1,$aYearType,$aMonthType,$aDayType);
144 //------------------------------------------------------------------------------------------
145 // Utility Function AdjTime()
146 // Description: Will round a given time stamp to an even time according to
147 // argument.
148 //------------------------------------------------------------------------------------------
150 function AdjTime($aTime,$aRound=0,$aHourType=false,$aMinType=false,$aSecType=false) {
151 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
152 $h = (int)date('H',$aTime); $i = (int)date('i',$aTime); $s = (int)date('s',$aTime);
153 if( $aHourType !== false ) {
154 $aHourType %= 6;
155 $hourAdj = array(0=>1, 1=>2, 2=>3, 3=>4, 4=>6, 5=>12);
156 if( $aRound == 0 )
157 $h = floor($h/$hourAdj[$aHourType])*$hourAdj[$aHourType];
158 else {
159 if( ($h % $hourAdj[$aHourType]==0) && ($i > 0 || $s > 0) ) {
160 $h++;
162 $h = ceil($h/$hourAdj[$aHourType])*$hourAdj[$aHourType];
163 if( $h >= 24 ) {
164 $aTime += 86400;
165 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
166 $h -= 24;
169 $i=0;$s=0;
171 elseif( $aMinType !== false ) {
172 $aMinType %= 5;
173 $minAdj = array(0=>1, 1=>5, 2=>10, 3=>15, 4=>30);
174 if( $aRound == 0 ) {
175 $i = floor($i/$minAdj[$aMinType])*$minAdj[$aMinType];
177 else {
178 if( ($i % $minAdj[$aMinType]==0) && $s > 0 ) {
179 $i++;
181 $i = ceil($i/$minAdj[$aMinType])*$minAdj[$aMinType];
182 if( $i >= 60) {
183 $aTime += 3600;
184 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
185 $h = (int)date('H',$aTime); $i = 0;
188 $s=0;
190 elseif( $aSecType !== false ) {
191 $aSecType %= 5;
192 $secAdj = array(0=>1, 1=>5, 2=>10, 3=>15, 4=>30);
193 if( $aRound == 0 ) {
194 $s = floor($s/$secAdj[$aSecType])*$secAdj[$aSecType];
196 else {
197 $s = ceil($s/$secAdj[$aSecType]*1.0)*$secAdj[$aSecType];
198 if( $s >= 60) {
199 $s=0;
200 $aTime += 60;
201 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
202 $h = (int)date('H',$aTime); $i = (int)date('i',$aTime);
206 return mktime($h,$i,$s,$m,$d,$y);
209 //------------------------------------------------------------------------------------------
210 // Wrapper for AdjTime that will round a timestamp to an even time rounding
211 // it downwards.
212 // Example: AdjStartTime(mktime(18,27,13,2,22,2005),false,2) => 18:20
213 //------------------------------------------------------------------------------------------
214 function AdjStartTime($aTime,$aHourType=false,$aMinType=false,$aSecType=false) {
215 return $this->AdjTime($aTime,0,$aHourType,$aMinType,$aSecType);
218 //------------------------------------------------------------------------------------------
219 // Wrapper for AdjTime that will round a timestamp to an even time rounding
220 // it upwards
221 // Example: AdjEndTime(mktime(18,27,13,2,22,2005),false,2) => 18:30
222 //------------------------------------------------------------------------------------------
223 function AdjEndTime($aTime,$aHourType=false,$aMinType=false,$aSecType=false) {
224 return $this->AdjTime($aTime,1,$aHourType,$aMinType,$aSecType);
227 //------------------------------------------------------------------------------------------
228 // DateAutoScale
229 // Autoscale a date axis given start and end time
230 // Returns an array ($start,$end,$major,$minor,$format)
231 //------------------------------------------------------------------------------------------
232 function DoDateAutoScale($aStartTime,$aEndTime,$aDensity=0,$aAdjust=true) {
233 // Format of array
234 // array ( Decision point, array( array( Major-scale-step-array ),
235 // array( Minor-scale-step-array ),
236 // array( 0=date-adjust, 1=time-adjust, adjustment-alignment) )
238 $scalePoints =
239 array(
240 /* Intervall larger than 10 years */
241 SECPERYEAR*10,array(array(SECPERYEAR*5,SECPERYEAR*2),
242 array(SECPERYEAR),
243 array(0,YEARADJ_1, 0,YEARADJ_1) ),
245 /* Intervall larger than 2 years */
246 SECPERYEAR*2,array(array(SECPERYEAR),array(SECPERYEAR),
247 array(0,YEARADJ_1) ),
249 /* Intervall larger than 90 days (approx 3 month) */
250 SECPERDAY*90,array(array(SECPERDAY*30,SECPERDAY*14,SECPERDAY*7,SECPERDAY),
251 array(SECPERDAY*5,SECPERDAY*7,SECPERDAY,SECPERDAY),
252 array(0,MONTHADJ_1, 0,DAYADJ_WEEK, 0,DAYADJ_1, 0,DAYADJ_1)),
254 /* Intervall larger than 30 days (approx 1 month) */
255 SECPERDAY*30,array(array(SECPERDAY*14,SECPERDAY*7,SECPERDAY*2, SECPERDAY),
256 array(SECPERDAY,SECPERDAY.SECPERDAY,SECPERDAY),
257 array(0,DAYADJ_WEEK, 0,DAYADJ_1, 0,DAYADJ_1, 0,DAYADJ_1)),
259 /* Intervall larger than 7 days */
260 SECPERDAY*7,array(array(SECPERDAY,SECPERHOUR*12,SECPERHOUR*6,SECPERHOUR*2),
261 array(SECPERHOUR*6,SECPERHOUR*3,SECPERHOUR,SECPERHOUR),
262 array(0,DAYADJ_1, 1,HOURADJ_12, 1,HOURADJ_6, 1,HOURADJ_1)),
264 /* Intervall larger than 1 day */
265 SECPERDAY,array(array(SECPERDAY,SECPERHOUR*12,SECPERHOUR*6,SECPERHOUR*2,SECPERHOUR),
266 array(SECPERHOUR*6,SECPERHOUR*2,SECPERHOUR,SECPERHOUR,SECPERHOUR),
267 array(1,HOURADJ_12, 1,HOURADJ_6, 1,HOURADJ_1, 1,HOURADJ_1)),
269 /* Intervall larger than 12 hours */
270 SECPERHOUR*12,array(array(SECPERHOUR*2,SECPERHOUR,SECPERMIN*30,900,600),
271 array(1800,1800,900,300,300),
272 array(1,HOURADJ_1, 1,MINADJ_30, 1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5) ),
274 /* Intervall larger than 2 hours */
275 SECPERHOUR*2,array(array(SECPERHOUR,SECPERMIN*30,900,600,300),
276 array(1800,900,300,120,60),
277 array(1,HOURADJ_1, 1,MINADJ_30, 1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5) ),
279 /* Intervall larger than 1 hours */
280 SECPERHOUR,array(array(SECPERMIN*30,900,600,300),array(900,300,120,60),
281 array(1,MINADJ_30, 1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5) ),
283 /* Intervall larger than 30 min */
284 SECPERMIN*30,array(array(SECPERMIN*15,SECPERMIN*10,SECPERMIN*5,SECPERMIN),
285 array(300,300,60,10),
286 array(1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5, 1,MINADJ_1)),
288 /* Intervall larger than 1 min */
289 SECPERMIN,array(array(SECPERMIN,15,10,5),
290 array(15,5,2,1),
291 array(1,MINADJ_1, 1,SECADJ_15, 1,SECADJ_10, 1,SECADJ_5)),
293 /* Intervall larger than 10 sec */
294 10,array(array(5,2),
295 array(1,1),
296 array(1,SECADJ_5, 1,SECADJ_1)),
298 /* Intervall larger than 1 sec */
299 1,array(array(1),
300 array(1),
301 array(1,SECADJ_1)),
304 $ns = count($scalePoints);
305 // Establish major and minor scale units for the date scale
306 $diff = $aEndTime - $aStartTime;
307 if( $diff < 1 ) return false;
308 $done=false;
309 $i=0;
310 while( ! $done ) {
311 if( $diff > $scalePoints[2*$i] ) {
312 // Get major and minor scale for this intervall
313 $scaleSteps = $scalePoints[2*$i+1];
314 $major = $scaleSteps[0][min($aDensity,count($scaleSteps[0])-1)];
315 // Try to find out which minor step looks best
316 $minor = $scaleSteps[1][min($aDensity,count($scaleSteps[1])-1)];
317 if( $aAdjust ) {
318 // Find out how we should align the start and end timestamps
319 $idx = 2*min($aDensity,floor(count($scaleSteps[2])/2)-1);
320 if( $scaleSteps[2][$idx] === 0 ) {
321 // Use date adjustment
322 $adj = $scaleSteps[2][$idx+1];
323 if( $adj >= 30 ) {
324 $start = $this->AdjStartDate($aStartTime,$adj-30);
325 $end = $this->AdjEndDate($aEndTime,$adj-30);
327 elseif( $adj >= 20 ) {
328 $start = $this->AdjStartDate($aStartTime,false,$adj-20);
329 $end = $this->AdjEndDate($aEndTime,false,$adj-20);
331 else {
332 $start = $this->AdjStartDate($aStartTime,false,false,$adj);
333 $end = $this->AdjEndDate($aEndTime,false,false,$adj);
334 // We add 1 second for date adjustment to make sure we end on 00:00 the following day
335 // This makes the final major tick be srawn when we step day-by-day instead of ending
336 // on xx:59:59 which would not draw the final major tick
337 $end++;
340 else {
341 // Use time adjustment
342 $adj = $scaleSteps[2][$idx+1];
343 if( $adj >= 30 ) {
344 $start = $this->AdjStartTime($aStartTime,$adj-30);
345 $end = $this->AdjEndTime($aEndTime,$adj-30);
347 elseif( $adj >= 20 ) {
348 $start = $this->AdjStartTime($aStartTime,false,$adj-20);
349 $end = $this->AdjEndTime($aEndTime,false,$adj-20);
351 else {
352 $start = $this->AdjStartTime($aStartTime,false,false,$adj);
353 $end = $this->AdjEndTime($aEndTime,false,false,$adj);
357 // If the overall date span is larger than 1 day ten we show date
358 $format = '';
359 if( ($end-$start) > SECPERDAY ) {
360 $format = 'Y-m-d ';
362 // If the major step is less than 1 day we need to whow hours + min
363 if( $major < SECPERDAY ) {
364 $format .= 'H:i';
366 // If the major step is less than 1 min we need to show sec
367 if( $major < 60 ) {
368 $format .= ':s';
370 $done=true;
372 ++$i;
374 return array($start,$end,$major,$minor,$format);
377 // Overrides the automatic determined date format. Must be a valid date() format string
378 function SetDateFormat($aFormat) {
379 $this->date_format = $aFormat;
382 function SetDateAlign($aStartAlign,$aEndAlign=false) {
383 if( $aEndAlign === false ) {
384 $aEndAlign=$aStartAlign;
386 $this->iStartAlign = $aStartAlign;
387 $this->iEndAlign = $aEndAlign;
390 function SetTimeAlign($aStartAlign,$aEndAlign=false) {
391 if( $aEndAlign === false ) {
392 $aEndAlign=$aStartAlign;
394 $this->iStartTimeAlign = $aStartAlign;
395 $this->iEndTimeAlign = $aEndAlign;
399 function AutoScale(&$img,$aStartTime,$aEndTime,$aNumSteps) {
400 if( $aStartTime == $aEndTime ) {
401 // Special case when we only have one data point.
402 // Create a small artifical intervall to do the autoscaling
403 $aStartTime -= 10;
404 $aEndTime += 10;
406 $done=false;
407 $i=0;
408 while( ! $done && $i < 5) {
409 list($adjstart,$adjend,$maj,$min,$format) = $this->DoDateAutoScale($aStartTime,$aEndTime,$i);
410 $n = floor(($adjend-$adjstart)/$maj);
411 if( $n * 1.7 > $aNumSteps ) {
412 $done=true;
414 $i++;
418 if( 0 ) { // DEBUG
419 echo " Start =".date("Y-m-d H:i:s",$aStartTime)."<br>";
420 echo " End =".date("Y-m-d H:i:s",$aEndTime)."<br>";
421 echo "Adj Start =".date("Y-m-d H:i:s",$adjstart)."<br>";
422 echo "Adj End =".date("Y-m-d H:i:s",$adjend)."<p>";
423 echo "Major = $maj s, ".floor($maj/60)."min, ".floor($maj/3600)."h, ".floor($maj/86400)."day<br>";
424 echo "Min = $min s, ".floor($min/60)."min, ".floor($min/3600)."h, ".floor($min/86400)."day<br>";
425 echo "Format=$format<p>";
429 if( $this->iStartTimeAlign !== false && $this->iStartAlign !== false ) {
430 JpGraphError::RaiseL(3001);
431 //('It is only possible to use either SetDateAlign() or SetTimeAlign() but not both');
434 if( $this->iStartTimeAlign !== false ) {
435 if( $this->iStartTimeAlign >= 30 ) {
436 $adjstart = $this->AdjStartTime($aStartTime,$this->iStartTimeAlign-30);
438 elseif( $this->iStartTimeAlign >= 20 ) {
439 $adjstart = $this->AdjStartTime($aStartTime,false,$this->iStartTimeAlign-20);
441 else {
442 $adjstart = $this->AdjStartTime($aStartTime,false,false,$this->iStartTimeAlign);
445 if( $this->iEndTimeAlign !== false ) {
446 if( $this->iEndTimeAlign >= 30 ) {
447 $adjend = $this->AdjEndTime($aEndTime,$this->iEndTimeAlign-30);
449 elseif( $this->iEndTimeAlign >= 20 ) {
450 $adjend = $this->AdjEndTime($aEndTime,false,$this->iEndTimeAlign-20);
452 else {
453 $adjend = $this->AdjEndTime($aEndTime,false,false,$this->iEndTimeAlign);
459 if( $this->iStartAlign !== false ) {
460 if( $this->iStartAlign >= 30 ) {
461 $adjstart = $this->AdjStartDate($aStartTime,$this->iStartAlign-30);
463 elseif( $this->iStartAlign >= 20 ) {
464 $adjstart = $this->AdjStartDate($aStartTime,false,$this->iStartAlign-20);
466 else {
467 $adjstart = $this->AdjStartDate($aStartTime,false,false,$this->iStartAlign);
470 if( $this->iEndAlign !== false ) {
471 if( $this->iEndAlign >= 30 ) {
472 $adjend = $this->AdjEndDate($aEndTime,$this->iEndAlign-30);
474 elseif( $this->iEndAlign >= 20 ) {
475 $adjend = $this->AdjEndDate($aEndTime,false,$this->iEndAlign-20);
477 else {
478 $adjend = $this->AdjEndDate($aEndTime,false,false,$this->iEndAlign);
481 $this->Update($img,$adjstart,$adjend);
482 if( ! $this->ticks->IsSpecified() )
483 $this->ticks->Set($maj,$min);
484 if( $this->date_format == '' )
485 $this->ticks->SetLabelDateFormat($format);
486 else
487 $this->ticks->SetLabelDateFormat($this->date_format);