Doubt Google Charts with Loop

Asked

Viewed 176 times

0

Good morning, I’m using a Google Hart to create a dashboard, only that it’s only bringing me a result my loop isn’t working, someone could help me see what’s wrong ?

<?php
include("conecta.php");
  $consumo_atual = mysql_query("SELECT * FROM Arduino.Leitura");  
    while ($linha = mysql_fetch_array($consumo_atual)) {
     $v_data =$linha["Data"]; 
     $v_consumo =$linha["Consumo"]; 
     } 
  ?> 
 <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(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['DATA', 'COSUMO'],
          ['<?php echo "$v_data"; ?>', <?php echo "$v_consumo"; ?>]
        ]);

        var options = {
          title: 'Perfomace de Consumo',
          hAxis: {title: 'Consumo Atual',  titleTextStyle: {color: '#333'}},
          vAxis: {minValue: 0}
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

I need to bring all the results because this Chart and line.

Follows amendment:

<?php
include("conecta.php");
  $consumo_atual = mysql_query("SELECT * FROM Leitura");  
  $v_data_consumo[] = array('DATA', 'CONSUMO');
  while ($linha = mysql_fetch_array($consumo_atual)) {
  $v_data_consumo[] = array($linha["Data"], $linha["Consumo"]); 
 } 
?>
 <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(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([<?php echo json_encode($v_data_consumo); ?>]);

        var options = {
          title: 'Perfomace de Consumo',
          hAxis: {title: 'Consumo Atual',  titleTextStyle: {color: '#333'}},
          vAxis: {minValue: 0}
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

1 answer

1


That block:

while ($linha = mysql_fetch_array($consumo_atual)) {
     $v_data =$linha["Data"]; 
     $v_consumo =$linha["Consumo"]; 
     } 

must be:

$v_data_consumo[] = array('DATA', 'CONSUMO');
 while ($linha = mysql_fetch_array($consumo_atual)) {
     $v_data_consumo[] = array($linha["Data"], $linha["Consumo"]); 
 } 

Allowing to create 2 arrays, one of date and one of consumption, with the returned values.

The line:

var data = google.visualization.arrayToDataTable([
      ['DATA', 'COSUMO'],
      ['<?php echo "$v_data"; ?>', <?php echo "$v_consumo"; ?>]
    ]);

should also be changed to something like:

 var data = google.visualization.arrayToDataTable(<?php echo json_encode($v_data_consumo); ?>);

Test and pass feedback...

  • I could not understand this error that generated: Deprecated: mysql_connect(): The mysql Extension is deprecated and will be Removed in the Future: use mysqli or PDO Instead in /var/www/html/Arduino/conecta.php on line 5

  • It’s not a mistake, it’s Warning, but it’s not about the code problem, it’s another matter, and it shouldn’t stop the system from running normally. Try replacing the "mysql_connect" function with the "mysqli_connect" function in the file line indicated in the error message.

  • I changed even more not right,my page is returning empty,I added in the question the amendment.

  • Notice that the last change you did wrong and it must be generating error in the browser! You should replace the 4 lines by only one! From the changed code you added, remove 3 lines below that: "var data = google.visualization.arrayToDa..."

  • Putis,every hour a new error,’m no longer understanding... Data column(s) for Axis #0 cannot be of type string

  • Try this line: google.visualization.arrayToDataTable([<?php echo json_encode($v_data_consumption); ?>], false); The false at the end is to recognize the first position of the arrai as titles and not data.

  • same error above.

  • One last attempt on my part, that testing here I saw that may be causing trouble, is to remove the '[' and ']' from the last line of the answer, as I already did in the answer. Try this.

  • Without success, I will read better the google apps library and return.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.