我有一个按钮,可以在图表上画一条线和该线上的一些点。我有第二个按钮可以删除线和点。我正在尝试对这两个功能使用相同的按钮。
我有一些困难。有没有人见过这样的事情?
这是我的两个功能。提前致谢!
 //Draws line and dots
 d3.select(".button1").on("click", function(){
  var path = svg.append("path")   // Add the valueline path. 
          .attr("class", "line")
          .attr("d", valueline(data))
          .attr("stroke", "steelblue")
          .attr("stroke-width", "5")
          .attr("fill", "black");
svg.selectAll("dot")
    .data(data)
.enter().append("circle")
    .attr("class", "firstDots")
    .attr("r", 5)
    .attr("cx", function(d) { return x(d.date); })
    .attr("cy", function(d) { return y(d.close); })
      var totalLength = path.node().getTotalLength();
  path
    .attr("stroke-dasharray", totalLength + "30" * 30)
    .attr("stroke-dashoffset", totalLength)
    .transition()
      .duration(2000)
      .ease("linear")
      .attr("stroke-dashoffset", 0);
     //Removes line and dots
     d3.select(".button2").on("click", function(){
      path.remove();
  var removeDot = svg.selectAll(".firstDots")
                .remove();
     });
});
起初我尝试在每个点击事件上来回传递按钮的类,它适用于绘制和删除。但只有一次,所以我无法第二次画线。