Javascript 重用 jqplot 对象来加载或重新绘制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4520451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:45:48  来源:igfitidea点击:

reuse jqplot object to load or replot data

javascriptjqueryjqplot

提问by Hunt

I am using JqPlot for charts , my problem is i want to load different data on different click events.

我将 JqPlot 用于图表,我的问题是我想在不同的点击事件上加载不同的数据。

But once the chart is created and loaded with the data for the first time; i don't know then how to load data when another event fires that means i want to reuse the chart object and want to load/replot the data when events get fired something like...

但是一旦图表被创建并第一次加载数据;我不知道如何在另一个事件触发时加载数据,这意味着我想重用图表对象并希望在事件触发时加载/重新绘制数据,例如......

chartObj.data = [graphData]

回答by Moritz Roessler

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

由于接受的答案对我不起作用,我也无法在 jqPlot 文档中找到解决方案。我来到这个解决方案

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

Src: Taking a look at the replotfunction.

src:看看replot功能。

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;

该行 if (ap || !L.isEmptyObject(an)) { this.reInitialize(ap, an) }
向我们展示了 ap 需要一个真值才能将其作为第一个参数传递给内部重新初始化函数。定义为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"

请注意,如果您想重绘 jqPlot 选项中定义的某些内容,例如图例标签,您可以将任何选项传递给 replot 函数。请记住要重新绘制的实际系列必须命名为“数据”

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)

回答by PaulH

jqplot allows for a fast dynamic data update.

jqplot 允许快速动态数据更新。

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."

“...但作为第二个参数传递给 $.jqplot() 函数。”

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

The fiddleshows this can be done with similar performance.

小提琴表演这可以用类似的性能来完成。

回答by Jaykishan

Empty graph div, before rendering the graph

在渲染图形之前清空图形 div

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

回答by Josiah Ruddell

The API has a replot/redraw method

API 有一个 replot/redraw 方法

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

重绘这使绘图数据和属性能够被更改,然后完全清除绘图和重绘。