我正在为图表使用 JqPlot,我的问题是我想在不同的点击事件上加载不同的数据。
但是一旦创建图表并首次加载数据;我不知道当另一个事件触发时如何加载数据,这意味着我想重用图表对象并想在事件被触发时加载/重新绘制数据......
chartObj.data = [graphData]
我正在为图表使用 JqPlot,我的问题是我想在不同的点击事件上加载不同的数据。
但是一旦创建图表并首次加载数据;我不知道当另一个事件触发时如何加载数据,这意味着我想重用图表对象并想在事件被触发时加载/重新绘制数据......
chartObj.data = [graphData]
这似乎可以重新绘制数据。
chartObj.series[0].data = [[0, 4], [1, 7], [2, 3]];
chartObj.replot();
虽然这是一个老问题。
由于接受的答案对我不起作用,我也无法在 jqPlot 文档中找到解决方案。我来到了这个解决方案
var series = [[1,2],[2,3]];
chartObj.replot({data:series});
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")
}
该行
if (ap || !L.isEmptyObject(an)) {
this.reInitialize(ap, an)
}
向我们展示了 ap 需要一个真实值才能将其作为第一个参数传递给内部重新初始化函数。定义为var ap = an.data || null;
它就这么简单,但不幸的是没有记录在我能找到的任何地方
请注意,如果您想重绘 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)
jqplot 允许快速的动态数据更新。
根据文档(数据部分),“不应在选项对象中指定数据......”(小提琴)
plot1.replot({data: [storedData]}); // data should not be passed this way
“......但作为第二个参数传递给 $.jqplot() 函数。”
if (plot1) plot1.destroy();
plot1 = $.jqplot('chart1', [storedData]); // similar speed to replot
小提琴表明这可以通过类似的性能来完成。
空图表 div,在渲染图表之前
$('#graphDiv').empty();
plot = $.jqplot('graphDiv', [graphValues], graphOptions);
重绘这可以更改绘图数据和属性,然后完全清除绘图并重绘。