简体   繁体   English

两次调用一个方法时仅呈现一个SVG组件

[英]Only one SVG component rendered when calling a method twice

I use a function to set up event handlers for a click and as it gets fired, the rendition goes as supposed to (fold in on the outer control and fold out on the inner one). 我使用一个函数来为单击设置事件处理程序,并在单击时触发其渲染,渲染按预期进行(在外部控件上折叠,在内部控件上折叠)。 Then, clicking it again, the process is retracted. 然后,再次单击它,将撤消该过程。 However, the next time I perform the operation, only the outer component changes its size while the inner one does not get affected. 但是,下次执行该操作时,只有外部组件会更改其大小,而内部组件不会受到影响。

function pieClickOuter(target) {
  var pie = d3.layout.pie()
    .startAngle(0).endAngle(2 * Math.PI)
    .value(function (d) { return d.val; });
  var out = d3.svg.arc().innerRadius(90).outerRadius(99);
  var org = d3.svg.arc().innerRadius(1).outerRadius(1);
  var sub = d3.svg.arc().innerRadius(10).outerRadius(80));

  d3.selectAll("#chart .sector path")
    .transition().duration(1500).attr("d", out);

  var grx = _.chart.selectAll(".subSector")
    .data(pie(getData())).enter().append("g")
    .attr("class", "subSector")
    .on("click", pieClickInner);

  grx.append("path")
    .attr("d", org).style("fill", function (d) { return colors(d.value); });
  grx.selectAll("#chart .subSector path")
    .transition().duration(1000).attr("d", sub);
}

function pieClickInner() {
  d3.selectAll("#chart .sector path")
    .transition().duration(1500)
    .attr("d", d3.svg.arc().innerRadius(80).outerRadius(99));

  outerPieEvents(d3.selectAll("#chart .sector"));

  d3.selectAll("#chart .subSector path")
    .transition().duration(1000)
    .attr("d", d3.svg.arc().innerRadius(1).outerRadius(1));
}

I cannot for my life see why. 我一生都看不到原因。 According to the console output, all the steps are executed, so it seems that the events are re-set up correctly. 根据控制台输出,所有步骤均已执行,因此似乎事件已正确设置。 Still, the inner component seems to disobey. 仍然,内部组件似乎不服从。

See this fiddle 看到这个小提琴

I believe that you want something like this . 我相信你想要这样的东西

I changed only 1 name: 我只更改了1个名称:

var grx = _.chart.selectAll(".subSector")

to

var grx = _.chart.selectAll(".foo")//or any other name

so, we don't select what already exists. 因此,我们不选择已经存在的内容。

The problem with this approach is that your SVG will have more and more groups each click. 这种方法的问题在于,您的SVG每次点击都会有越来越多的组。 But you can avoid this removing them in your pieClickInner() : 但是您可以避免在pieClickInner()其删除:

d3.selectAll("#chart .subSector path")
    .transition().duration(1000)
    .attr("d", d3.svg.arc().innerRadius(1).outerRadius(1)).remove();
d3.selectAll("#chart .subSector text").attr("opacity", 0).remove();
d3.selectAll("g.subSector").transition().duration(1000).remove();

I, personally, don't like remove() , i'd simply rebind the data. 我个人不喜欢remove() ,我只是简单地重新绑定数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM