davidjmorin

Javascript Issue with Google Charts

Nov 14th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. I am currently trying to get a line chart with data from a DB. I have the data showing and am able to retrieve a single line but the issue is when I add multiple columns it is only showing the last column data.
  2. The goal of this is to pull the temp from the data and display it, which is working but like i said only for one column in the set.
  3. With the above script I only see 1 line in my chart and it is always the last one, in this case Stoneham. Also when I hover over the line it only shows Stoneham data. What am I missing in this script?
  4.  
  5.  
  6. <!doctype html public "-//w3c//dtd html 3.2//en">
  7. <html>
  8. <head>
  9. <title>Core Temp of Dashboards</title>
  10. </head>
  11. <body >
  12. <?Php
  13. require "config.php";// Database connection
  14.  
  15. if($stmt = $connection->query("SELECT name,temp, CurrentTime FROM test WHERE currentTime = '2019-11-14 08:55:02' ORDER BY currentTime DESC ")){
  16.  
  17.   echo "No of records : ".$stmt->num_rows."<br>";
  18. $php_data_array = Array(); // create PHP array
  19.   echo "<table>
  20. <tr> <th>Location</th><th>IP</th><th>Temp</th><th>Date</th><th>Last Boot</th><th>Exp Vrv</th></tr>";
  21. while ($row = $stmt->fetch_row()) {
  22.    echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td></tr>";
  23.    $php_data_array[] = $row; // Adding to array
  24.    }
  25. echo "</table>";
  26. }else{
  27. echo $connection->error;
  28. }
  29. //print_r( $php_data_array);
  30. // You can display the json_encode output here.
  31. echo json_encode($php_data_array);
  32.  
  33. // Transfor PHP array to JavaScript two dimensional array
  34. echo "<script>
  35.        var my_2d = ".json_encode($php_data_array)."
  36. </script>";
  37. ?>
  38.  
  39.  
  40. <div id="curve_chart"></div>
  41.  
  42. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  43. <script type="text/javascript">
  44.  
  45.       // Load the Visualization API and the corechart package.
  46.       google.charts.load('current', {packages: ['corechart', 'line']});
  47.       google.charts.setOnLoadCallback(drawChart);
  48.  
  49.       function drawChart() {
  50.  
  51.         // Create the data table.
  52.         var data = new google.visualization.DataTable();
  53.         data.addColumn('string', 'temp');
  54.         data.addColumn('number', 'Andover');
  55.             data.addColumn('number', 'Thomaston');
  56.             data.addColumn('number', 'Beverly');
  57.             data.addColumn('number', 'Bedford');
  58.             data.addColumn('number', 'Salem');
  59.         data.addColumn('number', 'Stoneham');
  60.         for(i = 0; i < my_2d.length; i++)
  61.       data.addRow([
  62.         my_2d[i][2],
  63.         parseInt(my_2d[i][1]),
  64.         parseInt(my_2d[i][1]),
  65.         parseInt(my_2d[i][1]),
  66.         parseInt(my_2d[i][1]),
  67.         parseInt(my_2d[i][1]),
  68.         parseInt(my_2d[i][1])
  69.       ]);
  70.  
  71.        var options = {
  72.             title: 'Current Temps',
  73.                 width: 500,
  74.            height: 300,
  75.            legend: { position: 'right' },
  76.            tooltip: {trigger: 'selection'},
  77.            vAxis: {title: 'Temp'  }
  78.         };
  79.  
  80.         var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
  81.         chart.draw(data, options);
  82.        }
  83.     ///////////////////////////////
  84. </script>
  85. </body></html>
Add Comment
Please, Sign In to add comment