norman2

PHP/Google Chart API/Mysql data

Mar 3rd, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. <?php
  2. require "conf.php";
  3. try {
  4.     $conn = new PDO("mysql:host=localhost;dbname=$dbname", $dbuser, $dbpass);
  5.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  6.     $result = $conn->query('SELECT date_format(Czas,"%Y") as rok,
  7.                 date_format(Czas,"%m")-1 as miesiac,
  8.                 date_format(Czas,"%d") as dzien,
  9.                 date_format(Czas,"%H") as godzina,
  10.                 date_format(Czas,"%i") as minuta,
  11.                  temp1 FROM temperatury_log where Czas >= DATE_SUB(NOW(), INTERVAL 1 DAY)');
  12.     $licznik = 0;
  13.     $min = 100; // heheszka taka
  14.     $max = 0;
  15.     $cialko = 'data.addRows([ ';
  16.     foreach ($result as $r) {
  17.         $cialko.='[new Date(' . $r['rok'] . ', ' . $r['miesiac'] . ', ' . $r['dzien'] . ', ' . $r['godzina'] . ', ' . $r['minuta'] . '), ' . $r['temp1'] . '],';
  18.         if ($r['temp1'] > $max) {
  19.             $max = $r['temp1'];
  20.         };
  21.         if ($r['temp1'] < $min) {
  22.             $min = $r['temp1'];
  23.         };
  24.         $licznik++;
  25.     }
  26.     substr_replace($cialko, "", -1);
  27.     $cialko.=']);';
  28. } catch (PDOException $e) {
  29.     echo 'ERROR: ' . $e->getMessage();
  30. }
  31. ?>
  32. <html>
  33.     <head>
  34.         <!--Load the Ajax API-->
  35.         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  36.         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  37.         <script type="text/javascript">
  38.             google.load('visualization', '1', {packages: ['corechart']});
  39.             google.setOnLoadCallback(drawChart);
  40.             function drawChart() {
  41.                 var data = new google.visualization.DataTable();
  42.                 data.addColumn('datetime', 'Czaaas');
  43.                 data.addColumn('number', 'Temperatura');
  44. <?php echo $cialko; ?>
  45.                 var options = {
  46.                     title: 'Temperatury z ostatnich 24 godzin',
  47.                     width: 1300,
  48.                     height: 400,
  49.                     curveType: 'function',
  50.                     pointSize: 5,
  51.                     hAxis: {
  52.                         format: 'HH:mm',
  53.                         gridlines: {count: <?php echo $licznik; ?>}
  54.                     },
  55.                     vAxis: {
  56.                         gridlines: {color: 'none'},
  57.                         minValue: <?php $min--;echo number_format($min, 0, '', ''); ?>,
  58.                         maxValue: <?php $max++;echo number_format($max, 0, '', ''); ?>
  59.                     }
  60.                 };
  61.                 var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
  62.                 chart.draw(data, options);
  63.             }
  64.         </script>
  65.     </head>
  66.     <body>
  67.         <div id="chart_div"></div>
  68.     </body>
  69. </html>
Add Comment
Please, Sign In to add comment