【问题标题】:Remove Path Fills Completely in D3在 D3 中完全删除路径填充
【发布时间】:2018-02-08 01:00:54
【问题描述】:

我目前正在构建一个平台,用于映射一些需要在地图中的节点之间画线的数据。现在,我已经创建了这些行 (Picture 1 (fill: none))。此外,我打算在我已经完成的行中添加一个点击侦听器。不幸的是,该路径似乎有一个填充区域,该区域也侦听事件并与其他路径重叠,这使得无法单击其下方的路径 (Picture 2 default fill attr)。我试图设置fill: none(如第一张图片所示),但似乎只删除了颜色,而不是填充区域。 这是我添加行的代码:

function createPath(company) {

  //Create Paths Line
  var linePathGenerator = d3.line()
    .x(function(d) {
      return d.x;
    })
    .y(function(d) {
      return d.y;
    })
    .curve(d3.curveMonotoneX);

  // Add Path to the Line Layer
  var svgPath = layerArc.append("path")
    .attr("stroke", companyConfig[company].color)
    .attr("stroke-width", "1.5px")
    .attr("fill", "none")
    .style("opacity", 0.6);


  var d = linePathGenerator(adjency[company])
  var line = svgPath.attr("d", d)

  // Click Listener 
  line
    .on('click', function() {
      if (!d3.select(this).classed("active")) {
        layerArc.selectAll('path')
          .attr("stroke-width", "1.5px")
          .style("opacity", 0.6);
        d3.select(this)
          .attr("stroke-width", 3)
          .style("opacity", 1)
          .attr("class", "active")
        d3.selectAll(".nodes svg circle")
          .style("stroke", 'none')
        var circles = d3.selectAll(".nodes svg")
        circles = circles.filter(function(d) {
          return d.value.company == company
        })
        circles.select('circle').style("stroke", 'white')
          .style("stroke-width", 2)
      } else {
        layerArc.selectAll('path')
          .attr("stroke-width", "1.5px")
          .style("opacity", 0.6)
          .attr("class", "deactive")
        d3.selectAll(".nodes svg circle")
          .style("stroke", 'none')
      }
    })

  var totalLength = svgPath.node().getTotalLength();

  svgPath
    .attr("stroke-dasharray", totalLength + " " + totalLength)
    .attr("stroke-dashoffset", totalLength)
    .transition()
    .duration(4000)
    .ease(d3.easeLinear)
    .attr("stroke-dashoffset", 0)

}

有人知道如何完全去除填充物吗?或关于我如何以相同方式绘制线条但没有路径的任何其他建议?

【问题讨论】:

  • Stack sn-p 仅用于运行代码,我相应地编辑了您的问题。
  • 谢谢@Gerardo Furtado,这是我的第一篇文章

标签: javascript google-maps d3.js svg gis


【解决方案1】:

在我看来,您只是希望侦听器在用户单击路径的 stroke 时触发,而不是在其 fill 上(出现混淆是因为您'正在使用术语“line”,这意味着SVG中的另一件事)。

如果正确,解决方案很简单,您只需将这些路径的pointer-events 设置为visibleStrokestroke

.attr("pointer-events", "visibleStroke");

这是一个演示,您可以在填充或路径外单击,没有任何反应,单击它的笔触并触发侦听器:

var svg = d3.select("svg");
var path = svg.append("path")
  .attr("d", "M50,20 L250,20 L250,130 L50,130 Z")
  .style("fill", "teal")
  .style("stroke", "black")
  .style("stroke-width", 4)
  .attr("pointer-events", "visibleStroke")
  .on("click", function() {
    console.log("clicked")
  });
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>

此外,值得一提的是,您的代码中存在一些问题:如果您将填充设置为 none,则默认的 pointer-events 应该触发。这是一个演示:

var svg = d3.select("svg");
var path = svg.append("path")
  .attr("d", "M50,20 L250,20 L250,130 L50,130 Z")
  .style("fill", "none")
  .style("stroke", "black")
  .style("stroke-width", 4)
  .on("click", function() {
    console.log("clicked")
  });
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2023-03-22
    • 1970-01-01
    • 2019-12-20
    相关资源
    最近更新 更多