source: graph.php@ 34

Last change on this file since 34 was 28, checked in by george, 17 years ago

Upraveno: Změna kódování stránek na UTF-8.
Přidáno: Verze 3.1 v sekci vývoje.
Přidáno: Odkaz pod grafem na vybraný časový úsek.
Opraveno: Zobrazování desetinných míst u čísel bez jednotek.
Opraveno: Zobrazování textů Min, Avg, Max v grafu.

File size: 8.2 KB
Line 
1<?php
2include('global.php');
3
4if(array_key_exists('Debug', $_GET)) $Debug = $_GET['Debug'];
5 else $Debug = 0;
6
7if(!array_key_exists('From',$_GET)) die('Musíte zadat čas počátku');
8$StartTime = addslashes($_GET['From']);
9if(!array_key_exists('To',$_GET)) die('Musíte zadat čas konce');
10$EndTime = addslashes($_GET['To']);
11if($EndTime < $StartTime) $EndTime = $StartTime + 60;
12$TimeDifference = $EndTime - $StartTime;
13if(!array_key_exists('Measure',$_GET)) die('Musíte zadat měřenou veličinu');
14$MeasureId = addslashes($_GET['Measure']);
15if(!array_key_exists('Width', $_GET)) $Width = $DefaultWidth;
16else $Width = addslashes($_GET['Width']);
17if(!array_key_exists('Height', $_GET)) $Height = $DefaultHeight;
18else $Height = addslashes($_GET['Height']);
19if(!array_key_exists('Differential', $_GET)) $Differential = $Config['DefaultVariables']['Differential'];
20else $Differential = addslashes($_GET['Differential']);
21$VerticalLinesCount = round($Height / ($FontSize + 4));
22
23$StopWatchStart = GetMicrotime();
24
25$Measure = GetMeasureById($MeasureId);
26
27$Level = floor(log(($EndTime - $StartTime) / $DivisionCount / 60) / log($LevelReducing)) - 1;
28if($Level < 0) $Level = 0;
29if($Level > $MaxLevel) $Level = $MaxLevel;
30//$Level = 0;
31
32
33$Points = GetValues($Measure, $StartTime, $EndTime, $Level);
34
35if($Debug) echo('Points count: '.count($Points).'<br>');
36//if($Debug) foreach($Points as $Index => $Item)
37// echo($Index.': '.$Item['min'].'<br>');
38
39// Calculate total max, avg, min value
40$MaxValue = -1000000000000000000;
41$AvgValue = 0;
42$MinValue = 1000000000000000000;
43foreach($Points as $Index => $Item)
44{
45 //$Points[$Index]['min'] = $Points[$Index]['min'] / $Measure['Divider'];
46 //$Points[$Index]['avg'] = $Points[$Index]['avg'] / $Measure['Divider'];
47 //$Points[$Index]['max'] = $Points[$Index]['max'] / $Measure['Divider'];
48 if($Points[$Index]['avg'] > $MaxValue) $MaxValue = $Points[$Index]['avg'];
49 if($Points[$Index]['avg'] < $MinValue) $MinValue = $Points[$Index]['avg'];
50 if($Points[$Index]['max'] > $MaxValue) $MaxValue = $Points[$Index]['max'];
51 if($Points[$Index]['min'] < $MinValue) $MinValue = $Points[$Index]['min'];
52 $AvgValue = $AvgValue + $Points[$Index]['avg'];
53}
54//$MinValue = round($MinValue * $Measure['Divider']) / $Measure['Divider'];
55//$MaxValue = round($MaxValue * $Measure['Divider']) / $Measure['Divider'];
56$AvgValue = $AvgValue / count($Points); //round( * $Measure['Divider']) / $Measure['Divider'];
57
58// Generate polygon and recalculate y values to fit graph height
59$PointsMin = array(0, $Height-1);
60$PointsAvg = array(0, $Height-1);
61$PointsMax = array(0, $Height-1);
62if(($MaxValue - $MinValue) == 0) $MaxValue = $MinValue + 1;
63{
64 foreach($Points as $Index => $Item)
65 {
66 $PointsMin[] = $Index*$Width/$DivisionCount;
67 $PointsMin[] = $Height - 1 - ($Points[$Index]['min'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
68 $PointsAvg[] = $Index*$Width/$DivisionCount;
69 $PointsAvg[] = $Height - 1 - ($Points[$Index]['avg'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
70 $PointsMax[] = $Index*$Width/$DivisionCount;
71 $PointsMax[] = $Height - 1 - ($Points[$Index]['max'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
72 //echo($Index.' - '.$Item.' '.$Points[$Index].'<br>');
73 }
74}
75$PointsMin[] = $Width-1;
76$PointsMin[] = $Height-1;
77$PointsAvg[] = $Width-1;
78$PointsAvg[] = $Height-1;
79$PointsMax[] = $Width-1;
80$PointsMax[] = $Height-1;
81$PointsMin[] = $Width-1;
82$PointsMin[] = $Height-1;
83$PointsAvg[] = $Width-1;
84$PointsAvg[] = $Height-1;
85$PointsMax[] = $Width-1;
86$PointsMax[] = $Height-1;
87
88
89//array_unshift($Points, $Height - 1);
90//array_unshift($Points, 0);
91//$Points[] = $Width - 1;
92//$Points[] = $Height - 1;
93
94//print_r($PointsMax);
95
96// Generate image
97if(!$Debug)
98{
99 header("Content-type: image/png");
100 header("Cache-Control: no-cache"); // Dynamic graph - no cache
101 $Image = @imagecreate($Width, $Height);
102 $BackgroundColor = imagecolorallocate($Image, 255, 255, 255);
103 $Black = imagecolorallocate($Image, 0, 0, 0);
104 $White = imagecolorallocate($Image, 255, 255, 255);
105 $Gray = imagecolorallocate($Image, 200, 200, 200);
106 $DarkGray = imagecolorallocate($Image, 100, 100, 100);
107 $LightBlue = imagecolorallocate($Image, 150, 150, 255);
108 $Blue = imagecolorallocate($Image, 0, 0, 255);
109 $LightRed = imagecolorallocate($Image, 255, 150, 150);
110 $Red = imagecolorallocate($Image, 255, 0, 0);
111 $Green = imagecolorallocate($Image, 0, 200, 0);
112 $LightGreen = imagecolorallocate($Image, 150, 255, 150);
113
114 imagefilledpolygon($Image, $PointsMax, count($PointsMax) / 2, $LightRed);
115 imagefilledpolygon($Image, $PointsAvg, count($PointsAvg) / 2, $LightGreen);
116 imagefilledpolygon($Image, $PointsMin, count($PointsMin) / 2, $LightBlue);
117
118 $TimeMarks = array(1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 60*60*24*365, 60*60*24*365*10);
119
120 $TimeRange = $EndTime - $StartTime;
121 $TimeMarksIndex = 0;
122 while(($TimeRange / $TimeMarks[$TimeMarksIndex]) > 1) $TimeMarksIndex += 1;
123 if($TimeMarksIndex < 2) $TimeMarksIndex = 2;
124 $MajorTimeMarks = $TimeMarks[$TimeMarksIndex - 1];
125 $MinorTimeMarks = $TimeMarks[$TimeMarksIndex - 2];
126
127 $TimeShift = AlignTime($StartTime, $MajorTimeMarks) - $StartTime;
128 //imagestring($Image, 10, 40, 50, $TimeShift, $Black);
129
130 // Zobraz měřítko Y
131 $VerticalLinesDistance = $Height / $VerticalLinesCount;
132 for($I=1; $I<=$VerticalLinesCount; $I++)
133 {
134 $Y = $Height - 1 - ($VerticalLinesDistance * $I);
135 for($X=1; $X < $Width; $X = $X + 3) imagesetpixel($Image, $X, $Y, $Gray);
136 //imageline($Image, 30, $Y, $Width-1, $Y, IMG_COLOR_STYLED);
137 }
138
139 $TimeShift = AlignTime($StartTime, $MinorTimeMarks) - $StartTime;
140
141 // Zobraz měřítko X
142 $LastTextEnd = 0;
143 for($Time = $StartTime; $Time < $EndTime; $Time += $MajorTimeMarks)
144 {
145 $X = round(($Time - $StartTime + $TimeShift) / $TimeRange * $Width) % $Width;
146 //imageline($Image, 30, $Y, $Width-1, $Y, IMG_COLOR_STYLED);
147 if(($MajorTimeMarks > 60*60*24)) $Text = date('j.n.Y', $Time + $TimeShift);
148 else $Text = date('j.n.Y G:i', $Time + $TimeShift);
149 $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
150 if($LastTextEnd < ($X - ($BoundBox[2] - $BoundBox[0] + 20) / 2))
151 {
152 for($Y=0; $Y < $Height; $Y = $Y + 1) imagesetpixel($Image, $X, $Y, $Gray);
153 imagettftext($Image, $FontSize, 0, $X - ($BoundBox[2] - $BoundBox[0]) / 2, $Height - 2, $Black, $FontFile, $Text);
154 $LastTextEnd = $X + ($BoundBox[2] - $BoundBox[0]) / 2;
155 }
156 else for($Y=0; $Y < $Height; $Y = $Y + 3) imagesetpixel($Image, $X, $Y, $Gray);
157 }
158
159 // Popisky osy Y
160 for($I=1; $I<=$VerticalLinesCount; $I++)
161 {
162 $Y = $Height - 1 - ($VerticalLinesDistance * $I);
163 //$Y = $Height - 1 - ($VerticalLinesDistance * $I / ($MaxValue - $MinValue) * $K * $Height);
164 $Text = AddPrefixMultipliers(round(($I * $VerticalLinesDistance / $Height / $K * ($MaxValue - $MinValue) + $MinValue)), $Measure['Unit'], 3);
165 $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
166 if(($Y - ($BoundBox[5] - $BoundBox[1]) / 2) > 10)
167 imagettftext($Image, $FontSize, 0, 2, $Y - ($BoundBox[5] - $BoundBox[1]) / 2, $Black, $FontFile, $Text);
168 }
169 $GenerationTime = floor((GetMicrotime() - $StopWatchStart) * 1000 ) / 1000;
170
171 $Left = $Width - 10;
172 $Text = " Max. ".AddPrefixMultipliers($MaxValue, $Measure['Unit']);
173 $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
174 $Left -= ($BoundingBox[2] - $BoundingBox[0]);
175 imagettftext($Image, $FontSize, 0, $Left, 14, $Red, $FontFile, $Text);
176
177 $Text = " Avg. ".AddPrefixMultipliers($AvgValue, $Measure['Unit']);
178 $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
179 $Left -= ($BoundingBox[2] - $BoundingBox[0]);
180 imagettftext($Image, $FontSize, 0, $Left, 14, $Green, $FontFile, $Text);
181
182 $Text = " Min. ".AddPrefixMultipliers($MinValue, $Measure['Unit']);
183 $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
184 $Left -= ($BoundingBox[2] - $BoundingBox[0]);
185 imagettftext($Image, $FontSize, 0, $Left, 14, $Blue, $FontFile, $Text);
186 //imagestring($Image, 2, 70, 20, 'Vygenerováno za '.$GenerationTime.' sekund', $Black);
187 //imagestring($Image, 2, 50, 30, 'Level: '.$Level, $Black);
188
189 imagettftext($Image, $FontSize, 0, 70, 14, $Black, $FontFile, $Measure['Description']);
190 imagerectangle($Image, 0, 0, $Width - 1, $Height - 1, $Black); // Frame border
191 imagepng($Image);
192 imagedestroy($Image);
193}
194
195?>
Note: See TracBrowser for help on using the repository browser.