Dynamic graph does not update

Asked

Viewed 296 times

4

I’m using the library Rgraph graphics HTML5, and should update dynamically together with the bank.

However, I would like the last data imputed in the bank to be placed on the chart, so if I inserted the value 10 in the bank, the line of the graph would stay at 10 until I insert another value, like 25, and the graph would jump.

But the chart is not making that jump immediately, if the last value is 10, and I inputo 25, it stays at 10 unless I update the page.

Follow the code excerpt:

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 file.php:

<?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]);
?>

1 answer

2


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

The solution for your problem is an ajax request that brings the data, in a simple way, define a setInterval that makes a request every X seconds, and fill in the chart 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 it renders the graph, at the end of the file, add a setInterval calling the function (I left 1 second, the time does not matter, but do not leave too fast because it is unnecessary):

 setTimeout(function () { update() }, 1000);
  • In line line.originalData[0]. push(last); What value goes instead of last?

  • Actually I had put erradom, fixed the answer already give a look at the update, you return last within Function. changed one word from Function(line) to Function(last).

  • I made the changes you suggested, yet nothing happens, now the graph doesn’t plot anything, am I doing something wrong? .php <? php &#xA;include("salvateste.php");&#xA;$execute_again = mysqli_query($conexao, "Select * FROM tabelapi order by Evento desc"); &#xA;$lone = mysqli_fetch_object($execute_again); &#xA;$mostra_tudao[0] = $lone->Amperes; &#xA;echo json_encode($mostra_tudao[0]);&#xA;?>

  • Function update: Function update () { // A global l$. ajax({ type: "POST", url: "query.php", data: {}, dataType: 'json', Success: Function (last) { line.originalData[0].push(last); line.originalData[0].shift(); Rgraph.SVG.redraw(); } }); setTimeout(Function () { update() }, 50); } update();

  • @Feliperodrigues updates the question with the code you are using now formatted, it is very bad to read here in the comment

  • I updated it, you can see some error in the code?

  • From a.log(last) console inside Success:Function(last){} and see if the result is just a number, another observation, you should render the graph with the plugin’s draw function before executing updates: https://www.rgraph.net/demos/svg-line-dynamic.html

  • I put the console.log(last) and kept returning me the empty chart, I disabled the other functions and nothing happened, this second thing you told me to do I did not understand very well.

Show 4 more comments

Browser other questions tagged

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