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>