next up previous contents
Next: diff_count.php4 Up: Quellcode Previous: showgraf.php4   Contents


graph.php4


<?php
  /*******************************************
  /* Programm graph.php4
  /* displays temperature
  /* (c) Copyright 2002, Jens Bierkandt
  /* e-mail: jens@bierkandt.org
  /* Entstanden im Rahmen meiner Diplomarbeit
  /*******************************************
  */
  error_reporting(63);
  set_time_limit(60);
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  // Date in the past
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "
    GMT");
  // always modified
  header("Cache-Control: no-store, no-cache,
    must-revalidate");
  // HTTP/1.1
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: public");
  $total = time();
  include ("jpgraph-1.6.1/src/jpgraph.php");
  include ("jpgraph-1.6.1/src/jpgraph_line.php");
  include ("/home/schtorch/public_html/diplom/config.php4");
  // Check variables and initialize
  if (!isset($table))
  Header("Location: index.php4"); // Forward user to main
    page to reselect table
  if (!isset($average)) $average = 10;
  if ($average > 999) $average = 999;
  if (!(isset($width_graph) || isset($height_graph))) {
    $width_graph = 600;
    $height_graph = 400;
  };
  if ($width_graph < 600 || $height_graph < 400) {
    $width_graph = 600;
    $height_graph = 400;
  };
  // check if only max and mins with refering sensor
    selected
  if (isset($sendmax)) {
    $b = 0;
    for ($i = 0; $i < sizeof($sendmax); $i++) {
      for ($a = 0; $a < sizeof($sensor); $a++) {
        if ($sendmax[$i] == $sensor[$a]) {
          $max[$b] = $sendmax[$i];
          $b++;
        }
      }
    }
  }
  if (isset($sendmin)) {
    $b = 0;
    for ($i = 0; $i < sizeof($sendmin); $i++) {
      for ($a = 0; $a < sizeof($sensor); $a++) {
        if ($sendmin[$i] == $sensor[$a]) {
          $min[$b] = $sendmin[$i];
          $b++;
        }
      }
    }
  }
  // Initialize connection to DB
  $db = mysql_connect($hostname, $username, $password);
  mysql_select_db($database, $db);
  echo mysql_error();
  // Get number of rows
  // Needed to limit lines to draw
  $result = mysql_query("SELECT COUNT(*) FROM `tmp`", $db);
  $row = mysql_fetch_row($result);
  $rownumber = $row[0];
  //echo $rownumber." ";
  if ($rownumber/$width_graph < 1) $rownumber =
    $width_graph;
  $result = mysql_query("SELECT MIN(id) FROM `tmp`", $db);
  $row = mysql_fetch_row($result);
  $min_id = $row[0];
  // Select data
  $result = "SELECT id, UNIX_TIMESTAMP(timestamp), mictime,
    ";
  for ($a = 0; $a < sizeof($sensor); $a++) {
    $result .= "AVG(`sensor".$sensor[$a]."`),";
  }
  if (isset($max)) {
    for ($a = 0; $a < sizeof($max); $a++) {
      $result .= "MAX(`sensor".$max[$a]."`),";
    }
  }
  if (isset($min)) {
    for ($a = 0; $a < sizeof($min); $a++) {
      $result .= "MIN(`sensor".$min[$a]."`),";
    }
  }
  $result = substr($result, 0, strlen($result)-1);
  $row_step = ceil($rownumber/$width_graph);
  $result .= "FROM `tmp` GROUP BY
    (id-id%($row_step))/$row_step ORDER BY id ASC";
  $result = mysql_query($result, $db);
  echo mysql_error();
  //echo $rownumber/$width_graph;
  $i = 0;
  while($row = mysql_fetch_row($result)) {
    //echo $row[0]."<br>";
    while ($min_id+$i*$row_step < $row[0]) {
      $mictime = $min_id+$i*$row_step;
      $time = date ("H:i:s", substr($mictime, 0,
        strlen($mictime)-1));
      $date = date ("d.M.Y", substr($mictime, 0,
        strlen($mictime)-1));
      $datax[$i] = $time.".".substr($mictime,
        strlen($mictime)-1, strlen($mictime))."\n".$date;
      for ($a = 0; $a < sizeof($sensor); $a++) {
        $ydata[$a][$i] = "";
        $maxdata[$a][$i] = "";
        $mindata[$a][$i] = "";
      }
      //echo $datax[$i]."--<br>";
      $i++;
    }
    $last_id = $row[0];
    $mictime = $row[2];
    $time = date ("H:i:s", $row[1]);
    $date = date ("d.M.Y", $row[1]);
    $datax[$i] = $time.".".($mictime*10)."\n".$date;
    for ($a = 0; $a < sizeof($sensor); $a++) {
      $ydata[$a][$i] = $row[$a+3];
    }
    //echo $datax[$i]." ".$ydata[0][$i]."<br>";
    if (isset($max)) {
      for ($a = 0; $a < sizeof($max); $a++) {
        $maxdata[$a][$i] = $row[sizeof($sensor)+$a+3];
      }
    }
    if (isset($min)) {
      for ($a = 0; $a < sizeof($min); $a++) {
        if (isset($max))
        $mindata[$a][$i] =
          $row[sizeof($sensor)+sizeof($max)+$a+3];
        else
        $mindata[$a][$i] = $row[sizeof($sensor)+$a+3];
      }
    }
    $i++;
  }
  //echo $i;
  $x_tick_diff = 1;
  if (isset($datax[0])) {
    $x_temp = round(($width_graph-120-50)/100);
    $x_tick_diff = (sizeof($datax)-1)/$x_temp;
    //echo $x_tick_diff;
  }
  if ($x_tick_diff < 10) $x_tick_diff = round($x_tick_diff);
  // Create the graph. These two calls are always required
  $graph = new Graph($width_graph, $height_graph, "auto");
  if ($scale != "man") {
    $graph->SetScale("textlin");
  } else {
    $graph->SetScale("textlin", $scale_min, $scale_max);
  }
  //if ($x_tick_diff<10) $graph->img->SetAntiAliasing();
  // Create the linear plots
  $color = array("blue", "green", "orange", "magenta",
    "black", "red");
  for ($a = 0; $a < sizeof($sensor); $a++) {
    if (isset($sensor[$a])) {
      $lineplot[$a] = new LinePlot($ydata[$a]);
      $lineplot[$a]->SetColor($color[$a]);
      $lineplot[$a]->SetWeight(1);
      $lineplot[$a]->SetLegend("Sensor $sensor[$a]");
      $graph->Add($lineplot[$a]);
    }
  }
  if (isset($max)) {
    $color = array("lightblue", "lightgreen",
      "lightsalmon", "magenta", "black", "lightred");
    for ($a = 0; $a < sizeof($max); $a++) {
      if (isset($max[$a])) {
        $maxplot[$a] = new LinePlot($maxdata[$a]);
        $maxplot[$a]->SetColor($color[$a]);
        $maxplot[$a]->SetWeight(1);
        $maxplot[$a]->SetLegend("Max Sensor $max[$a]");
        $graph->Add($maxplot[$a]);
      }
    }
  }
  if (isset($min)) {
    $color = array("darkblue", "darkgreen", "darkorange",
      "magenta", "black", "darkred");
    for ($a = 0; $a < sizeof($min); $a++) {
      if (isset($min[$a])) {
        $minplot[$a] = new LinePlot($mindata[$a]);
        $minplot[$a]->SetColor($color[$a]);
        $minplot[$a]->SetWeight(1);
        $minplot[$a]->SetLegend("Min Sensor $min[$a]");
        $graph->Add($minplot[$a]);
      }
    }
  }
  $graph->legend->Pos(0.03, 0.4, "right", "center");
  $graph->xaxis->SetTickLabels($datax);
  $graph->xaxis->SetTextTickInterval($x_tick_diff);
  //$graph->xaxis->SetTextLabelInterval(2);
  $graph->img->SetMargin(50, 120, 30, 50);
  if (isset($computer)) $graph->title->Set($computer);
  //$graph->xaxis->title->Set("\nDate");
  //$graph->xaxis->SetLabelAngle(90);
  $graph->yaxis->title->Set("C");
  $graph->title->SetFont(FF_FONT1, FS_BOLD);
  $graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD);
  $graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD);
  $graph->yaxis->SetColor("red");
  $graph->yaxis->SetWeight(2);
  $graph->SetShadow();
  //echo "Total: ".(time()-$total)."\n";
  // Display the graph
  $graph->Stroke();
  //echo "Total +graph: ".(time()-$total)."\n";
?>



Jens Bierkandt