【问题标题】:Modifying SVG path opacity and it's marker修改 SVG 路径不透明度及其标记
【发布时间】:2019-08-12 20:13:40
【问题描述】:

我正在尝试对使用 D3 以编程方式定义的路径进行一些修改。我想做的改变很简单,修改路径的不透明度。我遇到的问题是路径本身会改变,而结束标记不会改变,我不太确定如何做到这一点。

标记是这样定义的:

    // define arrow markers for graph links
    svg.append('svg:defs').append('svg:marker')
        .attr('id', 'end-arrow')
        .attr('viewBox', '0 -5 10 10')
        .attr('refX', 6)
        .attr('markerWidth', 3)
        .attr('markerHeight', 3)
        .attr('orient', 'auto')
        .append('svg:path')
            .attr('d', 'M0,-5L10,0L0,5')
            .attr('fill', '#CCCCCC');

路径:

        // Create the links between the nodes
    var links = svg.append("g")
                    .selectAll(".link")
                    .data(data.links)
                    .enter()
                    .append("path")
                        .attr("class", "link")
                        .attr("d", sankey.link())
                        .style('marker-end', "url(#end-arrow)")
                        .style("stroke-width", function (d) { return Math.max(1, d.dy); })
                        .sort(function (a, b) { return b.dy - a.dy; });

我用来更改路径的代码,它不会更新标记:

d3.selectAll("path.link")
      .filter(function (link) {
          // Find all the links that come to/from this node
          if (self.sourceLinksMatch(self, link, node)) {
              return true;
          }

          if (self.targetLinksMatch(self, link, node)) {
              return true;
          }

          return false;
      })
     .transition()
     .style("stroke-opacity", 0.5);

任何人都可以建议我可能需要更改什么来修改标记结束样式吗?

【问题讨论】:

    标签: svg d3.js


    【解决方案1】:

    修改不透明度而不是描边不透明度有效.. 所以

    d3.selectAll("path.link")
      .transition()
      .style("stroke-opacity", 0.5);
    

    变成

    d3.selectAll("path.link")
      .transition()
      .style("opacity", 0.5);
    

    【讨论】:

    • 如果您有多个具有相同预定义标记的路径,这将不起作用
    • "opacity" 可以覆盖“填充”和“描边”不透明度。单独设置"stroke-opacity""fill-opacity"比较安全。
    【解决方案2】:

    您应该能够对标记路径定义执行相同的操作:

    d3.selectAll("marker path")
      .transition()
      .style("stroke-opacity", 0.5);
    

    【讨论】:

    • 我可以这样做——尽管我实际上并不想手动再次选择它们。我刚刚发现如果我设置“不透明度”而不是笔画不透明度,那么它也会正确选择标记......
    【解决方案3】:

    您可以为箭头标记设置定义预设名称

    // build the arrow.
    svg.append("svg:defs").selectAll("marker")
        .data(["HELPS","HELPED_BY","DAMAGES","REPELS","FAMILY", "KINGDOM"])      // Different link/path types can be defined here
      .enter().append("svg:marker")    // This section adds in the arrows
        .attr("id", String)
        .attr("viewBox", "0 -5 10 10")
        .attr("refX", 15)
        .attr("refY", -1.5)
        .attr("markerWidth", 6)
        .attr("markerHeight", 6)
        .attr("orient", "auto")
      .append("svg:path")
        .attr("d", "M0,-5L10,0L0,5");
    
    // add the links and the arrows
    var path = svg.append("svg:g").selectAll("path")
        .data(force.links())
      .enter().append("svg:path")
        .attr("class", function(d) { return "link " + d.type; })
        .attr("marker-end", function(d) { return "url(#" + d.type +")"; });
    

    并用CSS配置各自的样式

    marker#HELPS{fill:green;}
    path.link.HELPS {
      stroke: green;
    }
    
    marker#HELPED_BY{fill:#73d216;}
    path.link.HELPED_BY {
      stroke: #73d216;
    }
    
    marker#DAMAGES{fill:red;}
    path.link.DAMAGES {
      stroke: red;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多