Graph updating every n seconds

Asked

Viewed 106 times

0

I am using Google Charts and I need to make a chart that updates both the values of the x axis and the y axis every n seconds.

I need 50 data to be used for each update, so I used one SELECT max(id) FROM tabela_arduino to read the last value entered in the database. I tried calling this value in for to plot the graph, but the graph is not generated. Could someone help solve this?

<?php

mysql_connect("localhost", "root", "") or
die("Não foi possível conectar: " . mysql_error());
mysql_select_db("banco_arduino");

$result = mysql_query("SELECT max(id) FROM tabela_arduino");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   printf("ID: %s", $row["max(id)"]); 
}

echo  mysql_free_result($result);

$i=0;
$sql = "select * from tabela_arduino";
$resultado = mysql_query($sql);

?>


function desenharGrafico(){

    var dados=new google.visualization.DataTable();

    dados.addColumn('number', 'Registro');
    dados.addColumn('number', 'Talalla');

    dados.addRows(<?php echo $i ?>);

    <?php 
    $k = $i;

    //mudanças a partir  daqui
         $p = $row['max(id)'];

    for ($i = ($p-50); $i<$p; $i++){
    ?>
        dados.setValue(<?php echo $i ?>, 0, '<?php echo $id[$i] ?>');
        dados.setValue(<?php echo $i ?>, 1, <?php echo $sensor1[$i] ?>);

    <?php
    }
    ?>

    var options ={
        title: 'Gráfico ECG',
        width: 1280, height: 480,
        colors:['red'],
    legend: {position: 'bottom'}
    };

    var grafico = new google.visualization.LineChart(document.getElementById('curve_chart'));

    grafico.draw(dados, options);
};
  • You could implement a setInterval(), in JS, that calls php code by ajax. This worked for me when I wanted to make a simple chat.

  • I’m almost there, but I’m having a problem putting the last generated value into the for. I think my $p variable and $Row doesn’t have the same format, maybe that’s why the graph isn’t plotted. Is there any way to convert this $Row ? $p = $Row['max(id)'];

No answers

Browser other questions tagged

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