source: trunk/client/modules/Elezioni/grafici/jpgraph_date.php@ 392

Last change on this file since 392 was 346, checked in by roby, 4 years ago

ulteriori modifiche per adeguamento a php7
Client: aggiornamento jpgraph

File size: 19.9 KB
RevLine 
[2]1<?php
2/*=======================================================================
[284]3 // File: JPGRAPH_DATE.PHP
4 // Description: Classes to handle Date scaling
5 // Created: 2005-05-02
6 // Ver: $Id: jpgraph_date.php 1106 2009-02-22 20:16:35Z ljp $
7 //
8 // Copyright (c) Asial Corporation. All rights reserved.
9 //========================================================================
10 */
[2]11
12define('HOURADJ_1',0+30);
13define('HOURADJ_2',1+30);
14define('HOURADJ_3',2+30);
15define('HOURADJ_4',3+30);
16define('HOURADJ_6',4+30);
17define('HOURADJ_12',5+30);
18
19define('MINADJ_1',0+20);
20define('MINADJ_5',1+20);
21define('MINADJ_10',2+20);
22define('MINADJ_15',3+20);
23define('MINADJ_30',4+20);
24
25define('SECADJ_1',0);
26define('SECADJ_5',1);
27define('SECADJ_10',2);
28define('SECADJ_15',3);
29define('SECADJ_30',4);
30
31
32define('YEARADJ_1',0+30);
33define('YEARADJ_2',1+30);
34define('YEARADJ_5',2+30);
35
36define('MONTHADJ_1',0+20);
37define('MONTHADJ_6',1+20);
38
39define('DAYADJ_1',0);
40define('DAYADJ_WEEK',1);
41define('DAYADJ_7',1);
42
43define('SECPERYEAR',31536000);
44define('SECPERDAY',86400);
45define('SECPERHOUR',3600);
46define('SECPERMIN',60);
47
48
49class DateScale extends LinearScale {
50 private $date_format = '';
51 private $iStartAlign = false, $iEndAlign = false;
52 private $iStartTimeAlign = false, $iEndTimeAlign = false;
53
[284]54 //---------------
55 // CONSTRUCTOR
56 function __construct($aMin=0,$aMax=0,$aType='x') {
57 assert($aType=="x");
58 assert($aMin<=$aMax);
59
60 $this->type=$aType;
61 $this->scale=array($aMin,$aMax);
62 $this->world_size=$aMax-$aMin;
63 $this->ticks = new LinearTicks();
64 $this->intscale=true;
[2]65 }
66
67
[284]68 //------------------------------------------------------------------------------------------
69 // Utility Function AdjDate()
70 // Description: Will round a given time stamp to an even year, month or day
71 // argument.
72 //------------------------------------------------------------------------------------------
[2]73
74 function AdjDate($aTime,$aRound=0,$aYearType=false,$aMonthType=false,$aDayType=false) {
[284]75 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
76 $h=0;$i=0;$s=0;
77 if( $aYearType !== false ) {
78 $yearAdj = array(0=>1, 1=>2, 2=>5);
79 if( $aRound == 0 ) {
80 $y = floor($y/$yearAdj[$aYearType])*$yearAdj[$aYearType];
81 }
82 else {
83 ++$y;
84 $y = ceil($y/$yearAdj[$aYearType])*$yearAdj[$aYearType];
85 }
86 $m=1;$d=1;
87 }
88 elseif( $aMonthType !== false ) {
89 $monthAdj = array(0=>1, 1=>6);
90 if( $aRound == 0 ) {
91 $m = floor($m/$monthAdj[$aMonthType])*$monthAdj[$aMonthType];
92 $d=1;
93 }
94 else {
95 ++$m;
96 $m = ceil($m/$monthAdj[$aMonthType])*$monthAdj[$aMonthType];
97 $d=1;
98 }
99 }
100 elseif( $aDayType !== false ) {
101 if( $aDayType == 0 ) {
102 if( $aRound == 1 ) {
103 //++$d;
104 $h=23;$i=59;$s=59;
105 }
106 }
107 else {
108 // Adjust to an even week boundary.
109 $w = (int)date('w',$aTime); // Day of week 0=Sun, 6=Sat
110 if( true ) { // Adjust to start on Mon
111 if( $w==0 ) $w=6;
112 else --$w;
113 }
114 if( $aRound == 0 ) {
115 $d -= $w;
116 }
117 else {
118 $d += (7-$w);
119 $h=23;$i=59;$s=59;
120 }
121 }
122 }
123 return mktime($h,$i,$s,$m,$d,$y);
124
[2]125 }
126
[284]127 //------------------------------------------------------------------------------------------
128 // Wrapper for AdjDate that will round a timestamp to an even date rounding
129 // it downwards.
130 //------------------------------------------------------------------------------------------
[2]131 function AdjStartDate($aTime,$aYearType=false,$aMonthType=false,$aDayType=false) {
[284]132 return $this->AdjDate($aTime,0,$aYearType,$aMonthType,$aDayType);
[2]133 }
134
[284]135 //------------------------------------------------------------------------------------------
136 // Wrapper for AdjDate that will round a timestamp to an even date rounding
137 // it upwards
138 //------------------------------------------------------------------------------------------
[2]139 function AdjEndDate($aTime,$aYearType=false,$aMonthType=false,$aDayType=false) {
[284]140 return $this->AdjDate($aTime,1,$aYearType,$aMonthType,$aDayType);
[2]141 }
142
[284]143 //------------------------------------------------------------------------------------------
144 // Utility Function AdjTime()
145 // Description: Will round a given time stamp to an even time according to
146 // argument.
147 //------------------------------------------------------------------------------------------
[2]148
149 function AdjTime($aTime,$aRound=0,$aHourType=false,$aMinType=false,$aSecType=false) {
[284]150 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
151 $h = (int)date('H',$aTime); $i = (int)date('i',$aTime); $s = (int)date('s',$aTime);
152 if( $aHourType !== false ) {
153 $aHourType %= 6;
154 $hourAdj = array(0=>1, 1=>2, 2=>3, 3=>4, 4=>6, 5=>12);
155 if( $aRound == 0 )
156 $h = floor($h/$hourAdj[$aHourType])*$hourAdj[$aHourType];
157 else {
158 if( ($h % $hourAdj[$aHourType]==0) && ($i > 0 || $s > 0) ) {
159 $h++;
160 }
161 $h = ceil($h/$hourAdj[$aHourType])*$hourAdj[$aHourType];
162 if( $h >= 24 ) {
163 $aTime += 86400;
164 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
165 $h -= 24;
166 }
167 }
168 $i=0;$s=0;
169 }
170 elseif( $aMinType !== false ) {
171 $aMinType %= 5;
172 $minAdj = array(0=>1, 1=>5, 2=>10, 3=>15, 4=>30);
173 if( $aRound == 0 ) {
174 $i = floor($i/$minAdj[$aMinType])*$minAdj[$aMinType];
175 }
176 else {
177 if( ($i % $minAdj[$aMinType]==0) && $s > 0 ) {
178 $i++;
179 }
180 $i = ceil($i/$minAdj[$aMinType])*$minAdj[$aMinType];
181 if( $i >= 60) {
182 $aTime += 3600;
183 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
184 $h = (int)date('H',$aTime); $i = 0;
185 }
186 }
187 $s=0;
188 }
189 elseif( $aSecType !== false ) {
190 $aSecType %= 5;
191 $secAdj = array(0=>1, 1=>5, 2=>10, 3=>15, 4=>30);
192 if( $aRound == 0 ) {
193 $s = floor($s/$secAdj[$aSecType])*$secAdj[$aSecType];
194 }
195 else {
196 $s = ceil($s/$secAdj[$aSecType]*1.0)*$secAdj[$aSecType];
197 if( $s >= 60) {
198 $s=0;
199 $aTime += 60;
200 $y = (int)date('Y',$aTime); $m = (int)date('m',$aTime); $d = (int)date('d',$aTime);
201 $h = (int)date('H',$aTime); $i = (int)date('i',$aTime);
202 }
203 }
204 }
205 return mktime($h,$i,$s,$m,$d,$y);
[2]206 }
207
[284]208 //------------------------------------------------------------------------------------------
209 // Wrapper for AdjTime that will round a timestamp to an even time rounding
210 // it downwards.
211 // Example: AdjStartTime(mktime(18,27,13,2,22,2005),false,2) => 18:20
212 //------------------------------------------------------------------------------------------
[2]213 function AdjStartTime($aTime,$aHourType=false,$aMinType=false,$aSecType=false) {
[284]214 return $this->AdjTime($aTime,0,$aHourType,$aMinType,$aSecType);
[2]215 }
216
[284]217 //------------------------------------------------------------------------------------------
218 // Wrapper for AdjTime that will round a timestamp to an even time rounding
219 // it upwards
220 // Example: AdjEndTime(mktime(18,27,13,2,22,2005),false,2) => 18:30
221 //------------------------------------------------------------------------------------------
[2]222 function AdjEndTime($aTime,$aHourType=false,$aMinType=false,$aSecType=false) {
[284]223 return $this->AdjTime($aTime,1,$aHourType,$aMinType,$aSecType);
[2]224 }
225
[284]226 //------------------------------------------------------------------------------------------
227 // DateAutoScale
228 // Autoscale a date axis given start and end time
229 // Returns an array ($start,$end,$major,$minor,$format)
230 //------------------------------------------------------------------------------------------
[2]231 function DoDateAutoScale($aStartTime,$aEndTime,$aDensity=0,$aAdjust=true) {
[284]232 // Format of array
233 // array ( Decision point, array( array( Major-scale-step-array ),
234 // array( Minor-scale-step-array ),
235 // array( 0=date-adjust, 1=time-adjust, adjustment-alignment) )
236 //
237 $scalePoints =
238 array(
239 /* Intervall larger than 10 years */
240 SECPERYEAR*10,array(array(SECPERYEAR*5,SECPERYEAR*2),
241 array(SECPERYEAR),
242 array(0,YEARADJ_1, 0,YEARADJ_1) ),
[2]243
[284]244 /* Intervall larger than 2 years */
245 SECPERYEAR*2,array(array(SECPERYEAR),array(SECPERYEAR),
246 array(0,YEARADJ_1) ),
[2]247
[284]248 /* Intervall larger than 90 days (approx 3 month) */
249 SECPERDAY*90,array(array(SECPERDAY*30,SECPERDAY*14,SECPERDAY*7,SECPERDAY),
250 array(SECPERDAY*5,SECPERDAY*7,SECPERDAY,SECPERDAY),
251 array(0,MONTHADJ_1, 0,DAYADJ_WEEK, 0,DAYADJ_1, 0,DAYADJ_1)),
[2]252
[284]253 /* Intervall larger than 30 days (approx 1 month) */
254 SECPERDAY*30,array(array(SECPERDAY*14,SECPERDAY*7,SECPERDAY*2, SECPERDAY),
255 array(SECPERDAY,SECPERDAY,SECPERDAY,SECPERDAY),
256 array(0,DAYADJ_WEEK, 0,DAYADJ_1, 0,DAYADJ_1, 0,DAYADJ_1)),
[2]257
[284]258 /* Intervall larger than 7 days */
259 SECPERDAY*7,array(array(SECPERDAY,SECPERHOUR*12,SECPERHOUR*6,SECPERHOUR*2),
260 array(SECPERHOUR*6,SECPERHOUR*3,SECPERHOUR,SECPERHOUR),
261 array(0,DAYADJ_1, 1,HOURADJ_12, 1,HOURADJ_6, 1,HOURADJ_1)),
[2]262
[284]263 /* Intervall larger than 1 day */
264 SECPERDAY,array(array(SECPERDAY,SECPERHOUR*12,SECPERHOUR*6,SECPERHOUR*2,SECPERHOUR),
265 array(SECPERHOUR*6,SECPERHOUR*2,SECPERHOUR,SECPERHOUR,SECPERHOUR),
266 array(1,HOURADJ_12, 1,HOURADJ_6, 1,HOURADJ_1, 1,HOURADJ_1)),
[2]267
[284]268 /* Intervall larger than 12 hours */
269 SECPERHOUR*12,array(array(SECPERHOUR*2,SECPERHOUR,SECPERMIN*30,900,600),
270 array(1800,1800,900,300,300),
271 array(1,HOURADJ_1, 1,MINADJ_30, 1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5) ),
[2]272
[284]273 /* Intervall larger than 2 hours */
274 SECPERHOUR*2,array(array(SECPERHOUR,SECPERMIN*30,900,600,300),
275 array(1800,900,300,120,60),
276 array(1,HOURADJ_1, 1,MINADJ_30, 1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5) ),
[2]277
[284]278 /* Intervall larger than 1 hours */
279 SECPERHOUR,array(array(SECPERMIN*30,900,600,300),array(900,300,120,60),
280 array(1,MINADJ_30, 1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5) ),
[2]281
[284]282 /* Intervall larger than 30 min */
283 SECPERMIN*30,array(array(SECPERMIN*15,SECPERMIN*10,SECPERMIN*5,SECPERMIN),
284 array(300,300,60,10),
285 array(1,MINADJ_15, 1,MINADJ_10, 1,MINADJ_5, 1,MINADJ_1)),
[2]286
[284]287 /* Intervall larger than 1 min */
288 SECPERMIN,array(array(SECPERMIN,15,10,5),
289 array(15,5,2,1),
290 array(1,MINADJ_1, 1,SECADJ_15, 1,SECADJ_10, 1,SECADJ_5)),
[2]291
[284]292 /* Intervall larger than 10 sec */
293 10,array(array(5,2),
294 array(1,1),
295 array(1,SECADJ_5, 1,SECADJ_1)),
[2]296
[284]297 /* Intervall larger than 1 sec */
298 1,array(array(1),
299 array(1),
300 array(1,SECADJ_1)),
301 );
[2]302
[284]303 $ns = count($scalePoints);
304 // Establish major and minor scale units for the date scale
305 $diff = $aEndTime - $aStartTime;
306 if( $diff < 1 ) return false;
307 $done=false;
308 $i=0;
309 while( ! $done ) {
310 if( $diff > $scalePoints[2*$i] ) {
311 // Get major and minor scale for this intervall
312 $scaleSteps = $scalePoints[2*$i+1];
313 $major = $scaleSteps[0][min($aDensity,count($scaleSteps[0])-1)];
314 // Try to find out which minor step looks best
315 $minor = $scaleSteps[1][min($aDensity,count($scaleSteps[1])-1)];
316 if( $aAdjust ) {
317 // Find out how we should align the start and end timestamps
318 $idx = 2*min($aDensity,floor(count($scaleSteps[2])/2)-1);
319 if( $scaleSteps[2][$idx] === 0 ) {
320 // Use date adjustment
321 $adj = $scaleSteps[2][$idx+1];
322 if( $adj >= 30 ) {
323 $start = $this->AdjStartDate($aStartTime,$adj-30);
324 $end = $this->AdjEndDate($aEndTime,$adj-30);
325 }
326 elseif( $adj >= 20 ) {
327 $start = $this->AdjStartDate($aStartTime,false,$adj-20);
328 $end = $this->AdjEndDate($aEndTime,false,$adj-20);
329 }
330 else {
331 $start = $this->AdjStartDate($aStartTime,false,false,$adj);
332 $end = $this->AdjEndDate($aEndTime,false,false,$adj);
333 // We add 1 second for date adjustment to make sure we end on 00:00 the following day
334 // This makes the final major tick be srawn when we step day-by-day instead of ending
335 // on xx:59:59 which would not draw the final major tick
336 $end++;
337 }
338 }
339 else {
340 // Use time adjustment
341 $adj = $scaleSteps[2][$idx+1];
342 if( $adj >= 30 ) {
343 $start = $this->AdjStartTime($aStartTime,$adj-30);
344 $end = $this->AdjEndTime($aEndTime,$adj-30);
345 }
346 elseif( $adj >= 20 ) {
347 $start = $this->AdjStartTime($aStartTime,false,$adj-20);
348 $end = $this->AdjEndTime($aEndTime,false,$adj-20);
349 }
350 else {
351 $start = $this->AdjStartTime($aStartTime,false,false,$adj);
352 $end = $this->AdjEndTime($aEndTime,false,false,$adj);
353 }
354 }
355 }
356 // If the overall date span is larger than 1 day ten we show date
357 $format = '';
358 if( ($end-$start) > SECPERDAY ) {
359 $format = 'Y-m-d ';
360 }
361 // If the major step is less than 1 day we need to whow hours + min
362 if( $major < SECPERDAY ) {
363 $format .= 'H:i';
364 }
365 // If the major step is less than 1 min we need to show sec
366 if( $major < 60 ) {
367 $format .= ':s';
368 }
369 $done=true;
370 }
371 ++$i;
372 }
373 return array($start,$end,$major,$minor,$format);
[2]374 }
375
376 // Overrides the automatic determined date format. Must be a valid date() format string
377 function SetDateFormat($aFormat) {
[284]378 $this->date_format = $aFormat;
379 $this->ticks->SetLabelDateFormat($this->date_format);
[2]380 }
381
382 function AdjustForDST($aFlg=true) {
[284]383 $this->ticks->AdjustForDST($aFlg);
[2]384 }
385
386
387 function SetDateAlign($aStartAlign,$aEndAlign=false) {
[284]388 if( $aEndAlign === false ) {
389 $aEndAlign=$aStartAlign;
390 }
391 $this->iStartAlign = $aStartAlign;
392 $this->iEndAlign = $aEndAlign;
[2]393 }
394
395 function SetTimeAlign($aStartAlign,$aEndAlign=false) {
[284]396 if( $aEndAlign === false ) {
397 $aEndAlign=$aStartAlign;
398 }
399 $this->iStartTimeAlign = $aStartAlign;
400 $this->iEndTimeAlign = $aEndAlign;
[2]401 }
402
403
404 function AutoScale($img,$aStartTime,$aEndTime,$aNumSteps,$_adummy=false) {
[284]405 // We need to have one dummy argument to make the signature of AutoScale()
406 // identical to LinearScale::AutoScale
407 if( $aStartTime == $aEndTime ) {
408 // Special case when we only have one data point.
[346]409 // Create a small artificial interval to do the autoscaling
[284]410 $aStartTime -= 10;
411 $aEndTime += 10;
412 }
[346]413 if( abs($aEndTime - $aStartTime) <= 1 ) {
414 // Special case when we only have one second.
415 // Create a small artificial interval to do the autoscaling
416 $aStartTime -= 1;
417 $aEndTime += 1;
418 }
[284]419 $done=false;
420 $i=0;
421 while( ! $done && $i < 5) {
422 list($adjstart,$adjend,$maj,$min,$format) = $this->DoDateAutoScale($aStartTime,$aEndTime,$i);
423 $n = floor(($adjend-$adjstart)/$maj);
424 if( $n * 1.7 > $aNumSteps ) {
425 $done=true;
426 }
427 $i++;
428 }
[2]429
[284]430 /*
431 if( 0 ) { // DEBUG
432 echo " Start =".date("Y-m-d H:i:s",$aStartTime)."<br>";
433 echo " End =".date("Y-m-d H:i:s",$aEndTime)."<br>";
434 echo "Adj Start =".date("Y-m-d H:i:s",$adjstart)."<br>";
435 echo "Adj End =".date("Y-m-d H:i:s",$adjend)."<p>";
436 echo "Major = $maj s, ".floor($maj/60)."min, ".floor($maj/3600)."h, ".floor($maj/86400)."day<br>";
437 echo "Min = $min s, ".floor($min/60)."min, ".floor($min/3600)."h, ".floor($min/86400)."day<br>";
438 echo "Format=$format<p>";
439 }
440 */
[2]441
[284]442 if( $this->iStartTimeAlign !== false && $this->iStartAlign !== false ) {
443 JpGraphError::RaiseL(3001);
444 //('It is only possible to use either SetDateAlign() or SetTimeAlign() but not both');
445 }
[2]446
[284]447 if( $this->iStartTimeAlign !== false ) {
448 if( $this->iStartTimeAlign >= 30 ) {
449 $adjstart = $this->AdjStartTime($aStartTime,$this->iStartTimeAlign-30);
450 }
451 elseif( $this->iStartTimeAlign >= 20 ) {
452 $adjstart = $this->AdjStartTime($aStartTime,false,$this->iStartTimeAlign-20);
453 }
454 else {
455 $adjstart = $this->AdjStartTime($aStartTime,false,false,$this->iStartTimeAlign);
456 }
457 }
458 if( $this->iEndTimeAlign !== false ) {
459 if( $this->iEndTimeAlign >= 30 ) {
460 $adjend = $this->AdjEndTime($aEndTime,$this->iEndTimeAlign-30);
461 }
462 elseif( $this->iEndTimeAlign >= 20 ) {
463 $adjend = $this->AdjEndTime($aEndTime,false,$this->iEndTimeAlign-20);
464 }
465 else {
466 $adjend = $this->AdjEndTime($aEndTime,false,false,$this->iEndTimeAlign);
467 }
468 }
469
470
471
472 if( $this->iStartAlign !== false ) {
473 if( $this->iStartAlign >= 30 ) {
474 $adjstart = $this->AdjStartDate($aStartTime,$this->iStartAlign-30);
475 }
476 elseif( $this->iStartAlign >= 20 ) {
477 $adjstart = $this->AdjStartDate($aStartTime,false,$this->iStartAlign-20);
478 }
479 else {
480 $adjstart = $this->AdjStartDate($aStartTime,false,false,$this->iStartAlign);
481 }
482 }
483 if( $this->iEndAlign !== false ) {
484 if( $this->iEndAlign >= 30 ) {
485 $adjend = $this->AdjEndDate($aEndTime,$this->iEndAlign-30);
486 }
487 elseif( $this->iEndAlign >= 20 ) {
488 $adjend = $this->AdjEndDate($aEndTime,false,$this->iEndAlign-20);
489 }
490 else {
491 $adjend = $this->AdjEndDate($aEndTime,false,false,$this->iEndAlign);
492 }
493 }
494 $this->Update($img,$adjstart,$adjend);
495 if( ! $this->ticks->IsSpecified() )
496 $this->ticks->Set($maj,$min);
497 if( $this->date_format == '' )
498 $this->ticks->SetLabelDateFormat($format);
499 else
500 $this->ticks->SetLabelDateFormat($this->date_format);
[2]501 }
502}
503
504
505?>
Note: See TracBrowser for help on using the repository browser.