Skip to content
Advertisement

Getting live feed in Google charts

Rooky in need for some help here!

I am trying to set up Google chart connected to a database that will be populated during the whole day. So this chart needs to have a live feed.

I got below scrip and query. The Query works, and gives the feedback I need. Assuming that some magic in the ‘var data’ line as shown below must be added.

I am not sure about this part:

var data = google.visualization.arrayToDataTable([
     ['COUNT', 'Total'],
     ['Successes', '100'],
     ['Failures', '1'],
     ['Incompletes', '3'],

And:
echo "['".$row['Successes']."','".$row['Failures']."','".$row['Incompletes']."']";

I have tried various options, but can’t get it working.

If I am doing it like this:

```var data = google.visualization.arrayToDataTable([
         ['Successes', 'Failures', 'Incompletes'],```

&&

```echo "['".$row['Successes']."','".$row['Failures']."','".$row['Incompletes']."']";```

I do get a chart, but with wrong data.

The xx, yy and zz value in the example should be the live feed from the query.

  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load("current", {packages:["corechart"]});
      google.charts.setOnLoadCallback(drawResultChart);
      google.charts.setOnLoadCallback(drawVendorChart);

      function drawResultChart() {
        var data = google.visualization.arrayToDataTable([
         ['Count', 'Total'],
         ['Successes', 'xx'],
         ['Failures', 'yy'],
         ['Incompletes', 'zz'],

          <?php
          $query = "SELECT COUNT(CASE WHEN Success = '1' THEN 1 ELSE NULL END) AS Successes, COUNT(CASE WHEN Success = '0' AND EndTime > '1970-01-01 00:00:00' THEN 1 ELSE NULL END) AS Failures, COUNT(CASE WHEN Success = '0' AND EndTime = '1970-01-01 00:00:00' THEN 1 ELSE NULL END) As Incompletes, COUNT(*) AS Total FROM DiskOperationLog";

          
          $exec = mysqli_query($dbhandle,$query);
                while ($row =mysqli_fetch_array($exec)){

          echo "['".$row['Successes']."','".$row['Failures']."','".$row['Incompletes']."']";
          //echo '.$row['Total'].';'.$row['Successes'].';'.$row['Failures'].';'.$row['Incompletes'].';
  
}

         
          ?> 
        ]);

        var options = {
          title: 'Wipe Results',
          is3D: true,
        };

        var chart = new google.visualization.PieChart(document.getElementById('ResultChart'));
        chart.draw(data, options);
      }

    </script>
  </head>
  <body>
    <!--Table and divs that hold the pie charts-->
    <table class="columns">
    <tr>
    <td><div id="ResultChart" style="width: 450px; height: 250px;"></div></td>
    <td><div id="VendorChart" style="width: 450px; height: 250px;"></div></td>
    </tr>
  </body>
</html>```


  [1]: https://i.stack.imgur.com/yuRXp.png

Advertisement

Answer

This solved it for me

 <?php  
 while($row = mysqli_fetch_array($result))  
  {  
     echo "['".$row["Successes"]."', ".$row[0]."],";
     echo "['".$row["Failures"]."', ".$row[1]."],"; 
  }  
?> 

In this case I left “incompletes” out of the list as I do not need this anyway.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement