Javascript 用不同的图表类型替换NVD3图表
我正在尝试用pieChart NVD3模型替换discreteBarChart。 是否有方法清理/更新图表或元素内容 Html代码:Javascript 用不同的图表类型替换NVD3图表,javascript,html,d3.js,nvd3.js,Javascript,Html,D3.js,Nvd3.js,我正在尝试用pieChart NVD3模型替换discreteBarChart。 是否有方法清理/更新图表或元素内容 Html代码: <div id="chart"> <svg></svg> 以及我的atempt以替换现有图表: function updateToPie(key1, key2) { var width = 500, height = 500; chart = nv.models.pieChart().x(fu
<div id="chart">
<svg></svg>
以及我的atempt以替换现有图表:
function updateToPie(key1, key2) {
var width = 500,
height = 500;
chart = nv.models.pieChart().x(function(d) {
return d[key1]
}).y(function(d) {
return d[key2]
}).color(d3.scale.category10().range()).width(width).height(height);
d3.select("#chart svg").datum(chartData[0].values).transition()
.duration(1200).attr('width', width).attr('height', height)
.call(chart);
chart.dispatch.on('stateChange', function(e) {
nv.log('New State:', JSON.stringify(e));
});
return chart;
}
但是,这仅与现有条形图重叠。任何帮助都将不胜感激。谢谢您可以通过运行
d3.selectAll("#chart svg > *").remove();
添加新图表之前。您可以通过运行
d3.selectAll("#chart svg > *").remove();
添加新图表之前。您可以尝试类似d3.selectAllchart svg>*.remove;的操作;。正是我所需要的,它成功了,谢谢。很酷,我将添加它作为参考答案。您可以尝试类似d3.selectAllchart svg>*.remove;的方法;。正是我所需要的,它起作用了,谢谢。酷,我会补充这一点作为参考答案。