1 <?php // content="text/plain; charset=utf-8"
 
   3 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE);
 
   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('Не могу подключиться к БД');
 
  19 $db -> exec('SET CHARACTER SET utf8');
 
  21 $type = $_REQUEST['type'];
 
  22 $sensor=$_REQUEST['sensor'];
 
  23 $param=$_REQUEST['param'];
 
  25 if ($type and $param) {
 
  27   $sensor = intval($sensor);
 
  28   $param = intval($param);
 
  31     'select s_description from sensors where id='.$sensor
 
  35   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
  36     $sensor_name = $row['s_description'];
 
  40     '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'
 
  44   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
  45     $param_name = $row['st_description'];
 
  46     $from_unit = $row['id'];
 
  47     $unit_group = $row['unit_group'];
 
  48     $dot_color = $row['st_dot_color'];
 
  49     $line_color = $row['st_line_color'];
 
  52   if (!empty($_COOKIE['unit_'.$unit_group])) {
 
  53     $to_unit=intval($_COOKIE['unit_'.$unit_group]);
 
  59     'select u.name_short from units u where u.id='.$to_unit
 
  63   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
  64     $param_unit = $row['name_short'];
 
  70   if ($type == 'last24') { 
 
  73       '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'
 
  78   } elseif ($type == 'range') {
 
  80     $from = intval($_REQUEST['fromdate']);
 
  81     $to = intval($_REQUEST['todate']);
 
  84       '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'
 
  93   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
 
 101   for ($i = 0; $i < count($xdata); ++$i) {
 
 107     for ($j = $i; $j < count($xdata); ++$j) {
 
 109       $delta = abs($xdata[$i]-$xdata[$j]);
 
 110       if ($delta > $maxdelta) { break; }
 
 112       $weight = 1-$delta/$maxdelta;
 
 113       $total_weight += $weight;
 
 114       $sum += $weight*$ydata[$j];
 
 118     for ($j = $i-1; $j >=0 ; --$j) {
 
 120       $delta = abs($xdata[$i]-$xdata[$j]);
 
 121       if ($delta > $maxdelta) { break; }
 
 123       $weight = 1-$delta/$maxdelta;
 
 124       $total_weight += $weight;
 
 125       $sum += $weight*$ydata[$j];
 
 129     $new_val = $sum/$total_weight;
 
 130     $f_ydata[$i] = $new_val; 
 
 135   $g = new Graph(640,480);
 
 136   $g->graph_theme = null;
 
 138   //$g->img->SetAntiAliasing();
 
 140   // We need a linlin scale since we provide both
 
 141   // x and y coordinates for the data points.
 
 142   $g->SetScale('datlin');
 
 143   $g->xaxis->SetLabelAngle(90);
 
 144   $g->xaxis->SetPos("min");
 
 146   // We use a scatterplot to illustrate the original
 
 148   $splot = new ScatterPlot($ydata,$xdata);
 
 152   $splot->mark->SetFillColor($dot_color);
 
 153   $splot->mark->SetColor($dot_color);
 
 154   $splot->mark->SetType(MARK_FILLEDCIRCLE);
 
 155   $splot->mark->SetSize(2);
 
 157   $fplot = new LinePlot($f_ydata,$xdata);
 
 159   $fplot->SetColor($line_color);
 
 160   $fplot->SetWeight(2);
 
 162   $g->SetMargin(50,30,40,$height);
 
 163   $g->title->Set($sensor_name.'/'.$param_name.', '.$param_unit);
 
 164   $g->title->SetFont(FF_DV_SANSSERIF,FS_BOLD,12);
 
 165   $g->SetMarginColor('silver');
 
 167   // Add the plots to the graph and stroke
 
 172   header("Content-Type: text/html; charset=UTF-8");
 
 173   die('Сенсор не выбран!');