如何刷新jqplot条形图而不重绘图表
我有一个 jqplot 条形图,我希望当用户更改下拉列表上的值时更改图表数据。这可行,但问题是每次用户更改值时,条形图都会重新绘制。
如何更新或重新加载栏而不重新绘制整个内容?是否需要设置任何属性值?
图表数据根据 ajax 调用发生变化:
$.ajax({
url: '/Home/ChartData',
type: 'GET',
data: { Id: Id },
dataType: 'json',
success: function (data) {
$.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis));
}});
function CreateBarChartOptions(xAxis) {
var optionsObj = {
title: 'Stat',
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
ticks: xAxis
},
yaxis: { min: 0 }
},
series: [{ label: 'A' }, { label: 'B'}],
seriesDefaults: {
shadow: true,
renderer: $.jqplot.BarRenderer,
rendererOptions: {
barPadding: 8,
barMargin: 10
}
},
};
return optionsObj;
}
非常感谢您的回复。谢谢。
I have a jqplot bar chart and I want the chart data to be changed when the user changes the value on a drop-down list. That works, but the problem is the bar chart redraws, one over another, each time the user changes the values.
How can I update or reload the bars without drawing the whole thing again? Is there any property value to be set?
Chart data changes according to an ajax call:
$.ajax({
url: '/Home/ChartData',
type: 'GET',
data: { Id: Id },
dataType: 'json',
success: function (data) {
$.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis));
}});
function CreateBarChartOptions(xAxis) {
var optionsObj = {
title: 'Stat',
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
ticks: xAxis
},
yaxis: { min: 0 }
},
series: [{ label: 'A' }, { label: 'B'}],
seriesDefaults: {
shadow: true,
renderer: $.jqplot.BarRenderer,
rendererOptions: {
barPadding: 8,
barMargin: 10
}
},
};
return optionsObj;
}
A reply would be highly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您想要做的是在绘制新图表时调用 jqPlot 的 .replot() 方法。将您的 ajax 调用更改为如下所示:
What you want to do is call jqPlot's .replot() method when you draw the new chart. Change your ajax call to look like this:
尝试将图表对象作为脚本中的全局变量:
然后成功重置数据、axesScale 和重新绘制为:
Ref: https://groups.google.com/group/jqplot-users/browse_thread/thread/59df82899617242b?pli=1
Try having your chart object as a global variable in your script as:
Then on success reset the data, axesScale and replot as:
Ref: https://groups.google.com/group/jqplot-users/browse_thread/thread/59df82899617242b?pli=1
每次重新绘制图形之前,只需销毁现有的1。
Each time before redrawing the graph, just destroy the existing1.
我花了一段时间才找到脚本生成数据的答案,所以我将把它发布在这里。我使用了上述代码的组合。
我在脚本文件中创建了一个名为plot3 的全局变量。然后创建了以下函数。当使用重绘布尔值调用它时,它确定我是否需要销毁并重绘或第一次绘制。
第一段代码的作用是从我的 jqgrid 获取数据(正在不同的函数中更新),并更新数组。第二位确定我的间隔刻度,在 x 轴上取决于我的数据长度。
Took me a while to find an answer for the script generated data, so I'm going to post this right here. I used a combination of the above code.
I created a global var, named plot3 within my script file. Then created the below function. When this is called with a redraw boolean, it determines if I need to destroy and redraw or draw for the first time.
What first bit of code does is gets data from my jqgrid, (which is being updated in a different function), and updates the arrays. The second bit, determines my interval ticks, on the x-axis dependent upon my length of data.
以下是如何在不重新加载页面的情况下使用新数据动态更新绘图的完整示例:
这是此人帖子的简化版本:JQPlot 使用动态ajax数据自动刷新图表
Here is a full example of how to dynamically update the plot with new data without reloading the page:
It's a simplified version of this guy's post: JQPlot auto refresh chart with dynamic ajax data
$('#chart).html('');
图表是创建图表的 DIV。
这很有效,没什么花哨的。
$('#chart).html('');
chart is the DIV where chart is created.
this does the trick, nothing fancy by effective.
也许这会有所帮助。另一方面,我在重新绘图工作方面遇到了问题,但我正在使用数据渲染器。
Maybe this will help. I on the other hand is having problem with getting replot to work at all, but i'am using a dataRenderer.
希望这有帮助
hope this helps
我得到的最好的方法是,在绘制新图表之前,先清除您正在绘制的 div。
The best method I got is, the div in which you're drawing, clear that before you draw your new graph.