Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realtime chart using flot jquery

I'm looking for a way to display a VM current CPU usage using a Flot (Jquery) Chart.

From now, i can draw simple lines but no clue on how to get the graphic move to the left as new data comes in.

<script type="text/javascript">
        var d1 = [ [0,0] ];
         options = {
                lines: {
                    show: true
                },
                points: {
                    show: true
                },
                xaxis: {
                    tickDecimals: 0,
                    tickSize: 1
                },
                grid: {
                    backgroundColor: {
                        colors: ["#fff", "#eee"]
                    }
                }
        };

        function init() {                                              
            $.plot($("#placeholder"), d1, options);
        } /* init Function */

        function update(){
            for (var i = 0; i < 14; i += 0.5) {
                d1.push([i, Math.floor(Math.random()*11)]);
            }
            $.plot($("#placeholder"), [ d1 ], options);
        }
        init();
        $("input.dataUpdate").click(function () {
            update();
        });         
    </script>

Any idea or maybe another plugin that can do the trick ?

edit :

I need to translate the associative array :

 [ [1, (random1)], [2, (random2), [3, (random2) ]

to

 [ [2, (random2)], [3, (random3), [4, (random4) ] (new element 4)

Don't know how to achieve this.

like image 415
Disco Avatar asked Jan 21 '11 12:01

Disco


2 Answers

I was looking at their API and they have a 'setData' function that looks like it allows you to update an existing charts data.

http://flot.googlecode.com/svn/trunk/API.txt

[Update] After looking at the above example in other browsers, the refresh rate when reconstructing the plot from scratch is a bit slow. I noticed undesirable flashes in between updates. Here is a better solution:

var xVal = 0;
var data = [[],[]];
var plot = $.plot( $("#chart4"), data);

function getData(){
    // This could be an ajax call back.
    var yVal1 = Math.floor(Math.random()*11);
    var yVal2 = Math.floor(Math.random()*11);
    var datum1 = [xVal, yVal1];
    var datum2 = [xVal, yVal2];
    data[0].push(datum1);
    data[1].push(datum2);
    if(data[0].length>10){
        // only allow ten points
        data[0] = data[0].splice(1);
        data[1] = data[1].splice(1);
    }
    xVal++;
    plot.setData(data);
    plot.setupGrid();
    plot.draw();
}

setInterval(getData, 1000);

I also put together a blog post about flot describing this in a bit more detail:

http://blog.bobcravens.com/2011/01/web-charts-using-jquery-flot/

Bob

like image 57
rcravens Avatar answered Nov 10 '22 23:11

rcravens


You can use the shift method on the array to remove the first element (i.e. shifting it to the left and decreasing its size by 1 element) and push to add to the end and increasing its size to its original size before the shift e.g.

d1.shift();
d1.push(new_element);

then display the plot again with $.plot(....)

like image 1
Neil Avatar answered Nov 11 '22 00:11

Neil