我有以下代码通过 dojox.charting 创建图表图形:
function createChart() {
var node = dojo.byId("surfaceDiv");
while (node.hasChildNodes())
{
node.removeChild(node.lastChild); // remove all the children graphics
}
var nodes = "<div id='chart1' style='width: 10px; height: 10px;'></div><div id='legend1' ></div>";
dojo.html.set(node, nodes);
var nodeChart = dojo.byId("chart1");
var nodeLegend = dojo.byId("legend1");
var chart1 = new dojox.charting.Chart2D(nodeChart);
// set chart types and point series
chart1.render();
// now to add legend:
var legendNode = new dojox.charting.widget.Legent(
{chart: chart1}, nodeLegend.id));
}
该函数在第一次调用时工作正常;但是,如果再次调用,图表显示正常,但不显示图例。在 Firebug 中,我注意到在 manager.xd.js(第 8 行)中有一个错误提示“尝试使用 id==legend1 注册小部件,但该 id 已注册”。看起来在 dojox 的库中某处缓存了具有相同 id 的先前图例对象。
我想我必须清除之前注册或缓存的任何图例。我该怎么做?
顺便说一句,在我的 html 页面中,我有几个 ancor 链接调用 JavaScript 函数在一个 id="surfaceDiv"的 div 节点中绘制不同的图形,而图例节点"是下一个 id="legendDiv"的 div "。因此,可以再次调用上面的函数。
最佳答案
我正在使用 dojox 1.3.0 并发现以下对我来说工作正常(图例 是一个全局变量)没有任何错误:
if (legend != undefined) {
legend.destroyRecursive(true);
}
legend = new dojox.charting.widget.Legend({chart: chart1,horizontal: true}, 'legend');
//or try this
var myObj = new dojoObject(...);
...
// do whatever we want with it
...
// now we don't need it and we want to dispose of it
myObj.destroy();
delete myObj;
在这种情况下,图例在重新创建之前就被销毁了。
这是关于此主题的另一个链接:http://www.dojotoolkit.org/forum/dojox-dojox/dojox-support/how-unregister-legend-dojox-charting
关于javascript - 如何在 dojox.charting 中注销图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/688662/