183942ba18f568a5699ed9201effad4292177670
[weathermon.git] / web / image_minmax.php
1 <?php // content="text/plain; charset=utf-8"
2
3 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE);
4
5 include ('config_local.php');
6
7 require_once ('jpgraph/jpgraph.php');
8 require_once ('jpgraph/jpgraph_line.php');
9 require_once ('jpgraph/jpgraph_date.php');
10 require_once ('jpgraph/jpgraph_scatter.php');
11 require_once ('jpgraph/jpgraph_regstat.php');
12
13 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
14
15   die('Не могу подключиться к БД');
16
17 }  
18
19 $supported = imagetypes();
20 if( $supported & IMG_PNG )    $img_format="png";
21 elseif( $supported & IMG_GIF ) $img_format="gif";
22 elseif( $supported & IMG_JPG ) $img_format="jpeg";
23 elseif( $supported & IMG_WBMP ) $img_format="wbmp";
24 elseif( $supported & IMG_XPM ) $img_format="xpm";
25
26 $cachefilename = NULL;
27
28 $db -> exec('SET CHARACTER SET utf8');
29   
30 $type = $_REQUEST['type'];
31 $sensor=$_REQUEST['sensor'];
32 $param=$_REQUEST['param'];
33 $year=$_REQUEST['year'];
34 $month=$_REQUEST['month'];
35
36 if ($type and $param) {
37   
38   $sensor = intval($sensor);
39   $param = intval($param);
40
41   $q = $db -> prepare(
42     'select s_description from sensors where id='.$sensor
43   );
44   $q -> execute();
45
46   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
47     $sensor_name = $row['s_description'];
48   }                                                                
49
50   $q = $db -> prepare(
51     'select st.st_fill_color_top,st.st_fill_color_bottom,st.st_description,u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id
52 '
53   );
54   $q -> execute();
55
56   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
57     $param_name = $row['st_description'];
58     $from_unit = $row['id'];
59     $unit_group = $row['unit_group'];
60     $fill_color_top=$row['st_fill_color_top'];
61     $fill_color_bottom=$row['st_fill_color_bottom'];
62   }                                                                
63
64   if (!empty($_COOKIE['unit_'.$unit_group])) {
65     $to_unit=intval($_COOKIE['unit_'.$unit_group]);
66   } else {
67     $to_unit=$from_unit;
68   }  
69
70   $q = $db -> prepare(
71     'select u.name_short from units u where u.id='.$to_unit
72   );
73   $q -> execute();
74
75   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
76     $param_unit = $row['name_short'];
77   }                     
78   
79   $xdata = array();
80   $ydata = array();
81
82   if ($type == 'month') { 
83
84     $next_year = $year;
85     $next_month = $month+1;
86
87     if ($month==12) {
88     
89       $next_year++;
90       $next_month=1;
91
92     }
93
94     $curr = date("Ym");
95     $ym=sprintf('%04d%02d',$next_year,$next_month);
96     
97     if ($curr>$ym) {
98     
99       $cachefilename='meteo.month.'.$sensor.'.'.$param.'.'.$to_unit.'.'.$year.'-'.$month.'.'.$img_format;
100     
101     }
102
103     $q = $db -> prepare(
104       '
105         select 
106           x,
107           unitconv(min(min),'.$from_unit.','.$to_unit.') min_value,
108           unitconv(max(max),'.$from_unit.','.$to_unit.') max_value
109         from  (
110           select 
111             unix_timestamp(day) x,
112             min,
113             max
114           from 
115             sensors_ranges 
116           where 
117             day>=str_to_date(\''.$year.$month.'\',\'%Y%m\')
118             and day<str_to_date(\''.$next_year.$next_month.'\',\'%Y%m\')
119             and sensor='.$sensor.'
120             and parameter='.$param.'
121           ) t group by x
122         order by x'
123     );
124     
125   } elseif ($type == "year") {
126
127     $next_year = $year+1;
128
129     $curr = date("Y");
130         
131     if ($curr>$next_year) {
132                 
133         $cachefilename='meteo.year.'.$sensor.'.'.$param.'.'.$year.'.'.$img_format;
134                           
135     }
136
137     $q = $db -> prepare(
138       '
139         select 
140           x,
141           unitconv(min(min),'.$from_unit.','.$to_unit.') min_value,
142           unitconv(max(max),'.$from_unit.','.$to_unit.') max_value
143         from  (
144           select 
145             unix_timestamp(
146               DATE_SUB(day, INTERVAL DAYOFWEEK(day)-1 DAY)
147               ) x,
148             min,
149             max
150           from 
151             sensors_ranges
152           where 
153             day>=str_to_date(\''.$year.'\',\'%Y\')
154             and day<str_to_date(\''.$next_year.'\',\'%Y\')
155             and sensor='.$sensor.'
156             and parameter='.$param.'
157           ) t group by x
158         order by x'
159     );
160   
161   }
162
163   $g = new Graph(640,480);
164
165   if ($cachefilename) {
166       if ($g->cache->IsValid($cachefilename)) {
167   
168           $g->cache->StreamImgFile($g->img,$cachefilename);
169           return;
170   
171       } else {
172   
173           $timeout = 8640000;
174           $g->SetupCache($cachefilename,$timeout);
175
176       }
177   }
178
179
180   $q -> execute();
181     
182   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
183     
184     $xdata[] = $row['x'];
185     $mindata[] = $row['min_value'];
186     $maxdata[] = $row['max_value'];
187     
188   }                                                                
189
190
191   // Create the graph
192   $g->graph_theme = null;
193
194   //$g->img->SetAntiAliasing();
195
196   // We need a linlin scale since we provide both
197   // x and y coordinates for the data points.
198   $g->SetScale('datlin');
199   $g->xaxis->SetLabelAngle(90);
200   $g->xaxis->SetPos("min");
201   $g->xaxis->scale->SetTimeAlign( MINADJ_1 );
202
203   // We use a scatterplot to illustrate the original
204   // contro points.
205
206   $bplot = new LinePlot($maxdata,$xdata);
207   $g->Add($bplot);
208   $bplot->SetColor($fill_color_top);
209   $bplot->SetFillGradient($fill_color_top.'@0.2',$fill_color_bottom.'@0.9',100,TRUE);
210   $bplot->SetFillFromYMin(TRUE);        
211   $bplot->SetWeight(4);
212
213   $aplot = new LinePlot($mindata,$xdata);
214   $g->Add($aplot);
215   $aplot->SetColor($fill_color_bottom);
216   $aplot->SetFillGradient($fill_color_bottom.'@0.2','white@0.9',100,TRUE);
217   $aplot->SetFillFromYMin(TRUE);        
218   $aplot->SetWeight(4);
219
220   $g->SetMargin(60,60,60,130);
221   $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
222   $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
223   $g->subtitle->Set('(минимальные и максимальные значения за период)');
224   $g->subtitle->SetColor('darkred');
225   $g->SetMarginColor('silver');
226
227   $g->xgrid->Show();
228   $g->xgrid->SetLineStyle('dotted');
229   $g->ygrid->Show();
230   $g->ygrid->SetLineStyle('dotted');
231         
232   // Add the plots to the graph and stroke
233   $g->Stroke();
234   
235 } else {
236
237   header("Content-Type: text/html; charset=UTF-8");
238   die('Сенсор не выбран!');
239
240 }  
241
242 ?>