reuse jqplot object to load or replot data

百般思念 提交于 2019-11-30 08:46:01

Though this is an old question.

As the accepted Answer didn't work for me and i couldn't find a solution in the jqPlot docs either. I came to this solution

var series = [[1,2],[2,3]];
chartObj.replot({data:series});

Src: Taking a look at the replot function.

function (am) {
    var an = am || {};
    var ap = an.data || null;
    var al = (an.clear === false) ? false : true;
    var ao = an.resetAxes || false;
    delete an.data;
    delete an.clear;
    delete an.resetAxes;
    this.target.trigger("jqplotPreReplot");
    if (al) {
        this.destroy()
    }
    if (ap || !L.isEmptyObject(an)) {
        this.reInitialize(ap, an)
    } else {
        this.quickInit()
    } if (ao) {
        this.resetAxesScale(ao, an.axes)
    }
    this.draw();
    this.target.trigger("jqplotPostReplot")
}

The line if (ap || !L.isEmptyObject(an)) { this.reInitialize(ap, an) }
shows us it needs a truthy value for ap to pass it as first parameter to the internal reinitialize function. which is defined as var ap = an.data || null;

Its as simple as this but unfortunately not documented anywhere i could find it


Note that if you want to redraw some things defined in your jqPlot options, like legend labels, you can just pass any option to the replot function. Just remember the actual series to replot has to be named "data"

var options = {
     series : [{
            label: 'Replotted Series',
            linePattern: 'dashed'
     }],
   //^^^ The options for the plot
     data : [[1,2],[2,3]]
   //^^^ The actual series which should get reploted
}
chartObj.replot (options)

jqplot allows for a fast dynamic data update.

According to the documentation (data section), "Data should NOT be specified in the options object ..." (fiddle)

plot1.replot({data: [storedData]}); // data should not be passed this way

"... but be passed in as the second argument to the $.jqplot() function."

if (plot1) plot1.destroy();
plot1 = $.jqplot('chart1', [storedData]); // similar speed to replot

The fiddle shows this can be done with similar performance.

Empty graph div, before rendering the graph

$('#graphDiv').empty();
plot = $.jqplot('graphDiv', [graphValues], graphOptions);

The API has a replot/redraw method

Redraw This enables plot data and properties to be changed and then to comletely clear the plot and redraw.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!