Advertisement
Guest User

Untitled

a guest
Jan 4th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4.  
  5. // REPLACE with your Database name
  6. $dbname = "pzem_data";
  7. // REPLACE with Database user
  8. $username = "xxxxxxxxxxxx";
  9. // REPLACE with Database user password
  10. $password = "xxxxxxxxxxxx";
  11.  
  12. // Create connection
  13. $conn = new mysqli($servername, $username, $password, $dbname);
  14. // Check connection
  15. if ($conn->connect_error) {
  16.     die("Connection failed: " . $conn->connect_error);
  17. }
  18.  
  19. $sql = "SELECT id, value1, value2, reading_time FROM Sensor order by reading_time desc limit 40";
  20.  
  21. $result = $conn->query($sql);
  22.  
  23. while ($data = $result->fetch_assoc()){
  24.     $sensor_data[] = $data;
  25. }
  26.  
  27. $readings_time = array_column($sensor_data, 'reading_time');
  28.  
  29. // ******* Uncomment to convert readings time array to your timezone ********
  30. $i = 0;
  31. foreach ($readings_time as $reading){
  32.     // Uncomment to set timezone to - 1 hour (you can change 1 to any number)
  33.     $readings_time[$i] = date("d/m/Y  H:i:s", strtotime("$reading + 0 hours"));
  34.     // Uncomment to set timezone to + 4 hours (you can change 4 to any number)
  35.     //$readings_time[$i] = date("Y-m-d H:i:s", strtotime("$reading + 4 hours"));
  36.     $i += 1;
  37. }
  38.  
  39. $value1 = json_encode(array_reverse(array_column($sensor_data, 'value1')), JSON_NUMERIC_CHECK);
  40. $value2 = json_encode(array_reverse(array_column($sensor_data, 'value2')), JSON_NUMERIC_CHECK);
  41. $reading_time = json_encode(array_reverse($readings_time), JSON_NUMERIC_CHECK);
  42.  
  43. /*echo $value1;
  44. echo $value2;
  45. echo $value3;
  46. echo $reading_time;*/
  47.  
  48. $result->free();
  49. $conn->close();
  50. ?>
  51.  
  52. <!DOCTYPE html>
  53. <html>
  54. <meta name="viewport" content="width=device-width, initial-scale=2">
  55.   <script src="https://code.highcharts.com/highcharts.js"></script>
  56.   <style>
  57.     body {
  58.       min-width: 310px;
  59.       max-width: 1920px;
  60.     }
  61.     h2 {
  62.       font-family: Arial;
  63.       font-size: 2.5rem;
  64.       text-align: center;
  65.     }
  66.   </style>
  67.   <body>
  68.     <div id="chart-temperature" class="container"></div>
  69. <script>
  70.  
  71. var value1 = <?php echo $value1; ?>;
  72. var value2 = <?php echo $value2; ?>;
  73. var reading_time = <?php echo $reading_time; ?>;
  74.  
  75. var chartT = new Highcharts.Chart({
  76.   chart:{
  77.     height: 900,
  78.     renderTo : 'chart-temperature'
  79.   },
  80.   title: {
  81.     text: 'Letture Contatori Appartamenti'
  82.   },
  83.   series: [{
  84.     data: value1,
  85.     name: 'Piano Terra'
  86.   }, {
  87.     data: value2,
  88.     name: 'Piano Primo'
  89.   }],
  90.   plotOptions: {
  91.     line: { animation: false,
  92.       dataLabels: { enabled: true }
  93.     }
  94.   },
  95.   legend: {
  96.     layout: 'vertical',
  97.     floating: true,
  98.     backgroundColor: '#FCFFC5',
  99.     borderColor: '#C98657',
  100.     borderWidth: 1,
  101.     align: 'left',
  102.     verticalAlign: 'top',
  103.     y: 60,
  104.     x: 90
  105.   },
  106.   xAxis: {
  107.     type: 'datetime',
  108.     dateTimeLabelFormats: {
  109.       day: "%e. %b",
  110.       month: "%b '%y",
  111.       year: "%Y"
  112.     },
  113.     categories: reading_time,
  114.     labels: {
  115.       rotation: -45
  116.     }
  117.   },
  118.   yAxis: {
  119.     title: { text: 'Energia (kWh)' }
  120.   },
  121.   credits: { enabled: false }
  122. });
  123.  
  124. </script>
  125. </body>
  126. </html>
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement