Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除d3.js中svg的单个元素_D3.js - Fatal编程技术网

删除d3.js中svg的单个元素

删除d3.js中svg的单个元素,d3.js,D3.js,我在d3.js中有以下代码 var svg = d3.select(".Canvas").append("svg").attr( "width", width + margin.left + margin.right).attr("height", height + margin.top + margin.bottom).append("g").attr( "transform", "translat

我在d3.js中有以下代码

var svg = d3.select(".Canvas").append("svg").attr(
            "width", width + margin.left + margin.right).attr("height",
            height + margin.top + margin.bottom).append("g").attr(
            "transform",
            "translate(" + margin.left + "," + margin.top + ")");


    svg.append("g").attr("class", "x axis").attr("transform",
            "translate(0," + height + ")").call(xAxis);

    svg.append("g").attr("class", "y axis").call(yAxisMajor).append("text")
            .attr("transform", "rotate(-90)").attr("y", 6).attr("dy",
                    ".71em").style("text-anchor", "end");
我尝试了以下操作以仅删除y轴,而不是整个svg:

d3.选择(“.Canvas”).selectAll(“svg”).selectAll(“g”).select(“.y轴”).remove()

为什么代码不起作用?

这里有一些简单的方法(请参阅我在小提琴上的注释)


请注意,删除圆后,
svg
g
元素仍然存在。

因为当您将class属性设置为
y轴时,实际上设置了两个类。因此,为了选择和匹配这两个类名,您需要使用
.y.axis
调用
select
。因此,结果应该是:

d3.select(".canvas").selectAll("svg").selectAll("g").select(".y.axis").remove();
至少为我工作过

d3.select(".canvas").selectAll("svg").selectAll("g").select(".y.axis").remove();