source: trunk/Graph.php@ 63

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