15

I'm unable to figure out myself or find a proper example on how to perform live updates in jqPlot in a similar way as shown in this highcharts example.

doberkofler
  • 9,511
  • 18
  • 74
  • 126
  • +1 I like what you are trying to achieve. Do you already have some code? Could you share it at http://jsfiddle.net? – Boro May 05 '12 at 12:48
  • I posted an example on jsFiddle in my original post. – doberkofler May 05 '12 at 17:40
  • Yes I have noticed the example. Though in my previous comment I was referring to the code you wrote for `jqPlot` so we can start working from what you did with `jqPlot`. – Boro May 07 '12 at 11:09
  • http://stackoverflow.com/a/19471949 - great answer with test :) – Jacob Sobus Sep 26 '14 at 06:45

3 Answers3

22

Based on this, I prepared the following example:

$(document).ready(function() {
    var plot1 = $.jqplot('chart1', [new Array(1)], {
        title: 'Live Random Data',
        series: [
            {
            yaxis: 'y2axis',
            label: '',
            showMarker: false,
            fill: false,
            neighborThreshold: 3,
            lineWidth: 2.2,
            color: '#0571B6',
            fillAndStroke: true}
        ],
        axes: {
            xaxis: {
                renderer: $.jqplot.DateAxisRenderer,
                tickOptions: {
                    formatString: '%H:%M:%S'
                },
                numberTicks: 10
            },
            y2axis: {
                min: 100,
                max: 150,
                tickOptions: {
                    formatString: '%.2f'
                },
                numberTicks: 15
            }
        },
        cursor: {
            zoom: false,
            showTooltip: false,
            show: false
        },
        highlighter: {
            useAxesFormatters: false,
            showMarker: false,
            show: false
        },
        grid: {
            gridLineColor: '#333333',
            background: 'transparent',
            borderWidth: 3
        }
    });

    var myData = [];
    var x = (new Date()).getTime() - 101000;
    var y;
    var i;
    for ( i = 0; i < 100; i++) {
        x += 1000;
        y = Math.floor(Math.random() * 100);
        myData.push([x, y]);
    }

    plot1.series[0].data = myData;
    plot1.resetAxesScale();
    plot1.axes.xaxis.numberTicks = 10;
    plot1.axes.y2axis.numberTicks = 15;
    plot1.replot();

    function updateSeries() {
        myData.splice(0, 1);
        x = (new Date()).getTime();
        y = Math.floor(Math.random() * 100);
        myData.push([x, y]);

        plot1.series[0].data = myData;
        plot1.resetAxesScale();
        plot1.axes.xaxis.numberTicks = 10;
        plot1.axes.y2axis.numberTicks = 15;
        plot1.replot();
    }

    window.setInterval(updateSeries, 1000);
});

See this jsfiddle to test it out.

flaviut
  • 2,007
  • 3
  • 23
  • 32
Fracu
  • 835
  • 1
  • 13
  • 28
7

I added an example on JSFiddle jsfiddle.net/meccanismocomplesso/QAr4r/ which reproduces the example you linked.

I've tried to keep the topic as more general as possible. If you need more explanation read this article about that.

var plot1 = $.jqplot('myChart', [data], options);
...
plot1.series[0].data = data; // update the graph
Matthew Franglen
  • 4,441
  • 22
  • 32
3
var plot1;

function updatePlot(data){
plot1 = $.jqplot('myChart', [data], options);
}


function reDrawPlot(data){`
updatePlot(data);
plot1.replot();
}

....
initialize plot
plot1 = $.jqplot('myChart', [data], options);
....


call function reDrawPlot with the new data as a parameter
Nick Prusov
  • 146
  • 7