Dynamic chart does not update

4

I'm using the HTML5 RGraph library and should update dynamically along with the bank.

However, I would like the chart to put the last imputed data in the database, so if I entered the value 10 in the database, the graph line would stay in 10 until I enter another value, such as 25, and the graph would give a jump.

But the graph is not making that immediate jump, if the last value is 10, and I say 25, it stays at 10 unless I refresh the page.

Here is the snippet of code:

function update ()
{
    // A global

    l$.ajax({
        type: "POST",
        url: "consulta.php",
        data: {},
        dataType: 'json',
        success: function (last)
        {
            line.originalData[0].push(last);
            line.originalData[0].shift();
            RGraph.SVG.redraw();
        }
    });
    setTimeout(function () { update() }, 50);
}
update();

Query.php file:

<?php 
 include("salvateste.php");
 $execute_again = mysqli_query($conexao, "Select * FROM tabelapi order by Evento desc"); 
 $lone = mysqli_fetch_object($execute_again); 
 $mostra_tudao[0] = $lone->Amperes; 
 echo json_encode($mostra_tudao[0]);
?>
    
asked by anonymous 31.10.2017 / 02:15

1 answer

2

This is happening because the PHP code that is in the same file that renders the graphic, PHP is processed only once at the moment of page loading, so to process it repeatedly, it is necessary to call again and again.

The solution to your problem is an ajax request that brings the data, simply define a setInterval that makes a request every X seconds, and fill in the graph with this data:

The function:

function update ()
{
    $.ajax({
        type: "POST",
        url: "consulta.php",
        data: {},
        dataType: 'json',
        success: function (last)
        {
             line.originalData[0].push(last);
             line.originalData[0].shift();
             RGraph.SVG.redraw();
        }
    });
}

The PHP file that returns the number:

<?php 
$execute_again = mysqli_query($conexao, "Select * FROM tabelapi order by Evento desc"); 
$lone = mysqli_fetch_object($execute_again); 
$mostra_tudao[$i] = $lone->Amperes; 
echo json_encode($mostra_tudao);
?>

And in the file where the graph renders, at the end of the file, add a setInterval by calling the function (I left 1 second, the time does not, but do not leave too fast because it is unnecessary):

 setTimeout(function () { update() }, 1000);
    
31.10.2017 / 12:06