PROBLEM UPDATING A REALTIME FLOT GRAPH WITH JQUERY
Hi my friends:
I am trying to create a real-time graph with flot plotting library.
I am doing it this way:
1) The initial data i am retrieving it with this PHP script: "param_pozos_linea2.php". This scripts brings me the data in a string like this:
[[1357296902000,271.9],[
1357296603000,267.26],[
1357296303000,268.3],
[1357296002000,261.6],[
1357295703000,184.91],[
1357295403000,27.09],
[1357295103000,103.2]]
2) The new data i am fetching it with $.get method in jquery ( see actualiza_data() function ), whichs connects to param_pozos_linea3.php" script and brings a new graph point like this: "[1357296902000,271.9]" and inserts it into the "res" array.
3) Then the graph is updated with update() function.
The problem is that the graph is not updating automatically, i am not sure if i am doing it well.
I send you the code i am using.
Can you please help me??
Thanks in advance.
- <?php
- include "param_pozos_linea2.php";
- ?>
- <script type="text/javascript">
- $(function () {
-
- var data_inicial=<?php echo $pres_cab_l; ?>;
- data_inicial=data_inicial.reverse();
- function actualiza_data()
- {
- var res=[];
- if (data_inicial.length > 0)
- {
- res = data_inicial.slice(1);
- }
- while (res.length < 144)
- {
- $.get("param_pozos_linea3.php", function (datos){
- res.push(datos);
- });
- }
- return res;
- }
- // control de velocidad
- var updateInterval = 3000;
- $("#updateInterval").val(updateInterval).change(function () {
- var v = $(this).val();
- if (v && !isNaN(+v)) {
- updateInterval = +v;
- if (updateInterval < 1)
- updateInterval = 1;
- $(this).val("" + updateInterval);
- }
- });
- // setup plot
- var options = {
- series: { shadowSize: 0 }, // drawing is faster without shadows
- yaxis: { min: 0, max: 6500 },
- xaxis: { mode:"time",tickLength: 5, timeformat: "%d/%m - %h:%M %p"}
- };
- var plot = $.plot($("#placeholder"), [ <?php echo $pres_cab_l; ?> ], options);
- function update() {
- plot.setData([ actualiza_data() ]);
- plot.draw();
- setTimeout(update, updateInterval);
- }
- update();
- });
- </script>