Doubts Google Charts with Loop

0

Good morning,  I'm using a google chart to create a panel, but it's only bringing me one result so my loop is not working, could anyone 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><scripttype="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.

Follow the change:

<?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><scripttype="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>
    
asked by anonymous 31.03.2016 / 15:57

1 answer

1

This block:

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

should be:

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

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

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 ...

    
31.03.2016 / 17:08