1 <?php // content="text/plain; charset=utf-8"
3 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);
5 include ('config_local.php');
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');
13 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
15 die('Ð
\9dе могу подключитьÑ
\81Ñ
\8f к БД');
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";
26 $cachefilename = NULL;
28 $db -> exec('SET CHARACTER SET utf8');
30 $type = $_REQUEST['type'];
31 $sensor=$_REQUEST['sensor'];
32 $param=$_REQUEST['param'];
34 if ($type and $param) {
36 $sensor = intval($sensor);
37 $param = intval($param);
40 'select s_description from sensors where id='.$sensor
44 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
45 $sensor_name = $row['s_description'];
49 'select st.st_dot_color,st.st_line_color,st.st_description,u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id'
53 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
54 $param_name = $row['st_description'];
55 $from_unit = $row['id'];
56 $unit_group = $row['unit_group'];
57 $dot_color = $row['st_dot_color'];
58 $line_color = $row['st_line_color'];
61 if (!empty($_COOKIE['unit_'.$unit_group])) {
62 $to_unit=intval($_COOKIE['unit_'.$unit_group]);
68 'select u.name_short,u.prec from units u where u.id='.$to_unit
72 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
73 $param_unit = $row['name_short'];
74 $precision = $row['prec'];
80 if ($type == 'last24') {
83 'select unix_timestamp(timestamp) as x,unitconv(value,'.$from_unit.','.$to_unit.') as y from sensor_values where timestamp>adddate(now(), -1) and sensor_id='.$sensor.' and parameter_id='.$param.' order by timestamp'
92 } elseif ($type == 'last24small') {
95 'select unix_timestamp(timestamp) as x,unitconv(value,'.$from_unit.','.$to_unit.') as y from sensor_values where timestamp>adddate(now(), -1) and sensor_id='.$sensor.' and parameter_id='.$param.' order by timestamp'
104 } elseif ($type == 'range') {
106 $curr = intval(date('YmdHis'));
108 $from = intval($_REQUEST['fromdate']);
109 $to = intval($_REQUEST['todate']);
113 $cachefilename='meteo.'.$sensor.'.'.$param.'.'.$to_unit.'.'.$from.'-'.$to.'.'.$img_format;
118 'select unix_timestamp(timestamp) as x,unitconv(value,'.$from_unit.','.$to_unit.') as y from sensor_values where timestamp>=str_to_date("'.$from.'","%Y%m%d%H%i%s") and timestamp<=str_to_date("'.$to.'","%Y%m%d%H%i%s") and sensor_id='.$sensor.' and parameter_id='.$param.' order by timestamp'
129 $g = new Graph($sizex,$sizey);
131 if ($cachefilename) {
132 if ($g->cache->IsValid($cachefilename)) {
134 $g->cache->StreamImgFile($g->img,$cachefilename);
140 $g->SetupCache($cachefilename,$timeout);
147 while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
149 $xdata[] = $row['x'];
150 $ydata[] = $row['y'];
155 for ($i = 0; $i < count($xdata); ++$i) {
161 for ($j = $i; $j < count($xdata); ++$j) {
163 $delta = abs($xdata[$i]-$xdata[$j]);
164 if ($delta > $maxdelta) { break; }
166 $weight = 1-$delta/$maxdelta;
167 $total_weight += $weight;
168 $sum += $weight*$ydata[$j];
172 for ($j = $i-1; $j >=0 ; --$j) {
174 $delta = abs($xdata[$i]-$xdata[$j]);
175 if ($delta > $maxdelta) { break; }
177 $weight = 1-$delta/$maxdelta;
178 $total_weight += $weight;
179 $sum += $weight*$ydata[$j];
183 $new_val = $sum/$total_weight;
184 $f_ydata[$i] = $new_val;
189 $g->graph_theme = null;
191 $g->img->SetAntiAliasing();
193 // We need a datlin scale since we provide both
194 // x and y coordinates for the data points, but x is unix timestamp.
195 $g->SetScale('datlin');
197 $g->xaxis->SetLabelAngle(90);
199 $g->xaxis->HideLabels(True);
201 $g->xaxis->SetPos("min");
202 $g->yaxis->SetLabelFormat("%0.".$precision."f");
203 # $g->xaxis->scale->SetTimeAlign( HOURADJ_1 );
206 if ($type!="last24small") {
208 // We use a scatterplot to illustrate the original
210 $splot = new ScatterPlot($ydata,$xdata);
214 $splot->mark->SetFillColor($dot_color);
215 $splot->mark->SetColor($dot_color);
216 $splot->mark->SetType(MARK_FILLEDCIRCLE);
217 $splot->mark->SetSize(2);
221 $fplot = new LinePlot($f_ydata,$xdata);
223 $fplot->SetColor($line_color);
224 $fplot->SetWeight(2);
226 $g->SetMargin(50,30,$topheight,$bottomheight);
228 $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
229 $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
231 $g->SetMarginColor('lightgray');
234 $g->xgrid->SetLineStyle('dotted');
236 $g->ygrid->SetLineStyle('dotted');
238 // Add the plots to the graph and stroke
243 header("Content-Type: text/html; charset=UTF-8");
244 die('СенÑ
\81ор не выбран!');