【问题标题】:SVG path element created by d3 (d3.svg.arc) disappearing when mask is applied应用蒙版时,由 d3 (d3.svg.arc) 创建的 SVG 路径元素消失
【发布时间】:2016-07-09 11:59:09
【问题描述】:

我正在尝试将纹理(例如条纹)放在由 d3 arc 函数创建的 svg 路径元素上。 我找到了这个示例 (https://bl.ocks.org/jfsiii/7772281),这正是我想要的(使用 css 应用蒙版),但是当我应用到由 d3 arc 函数创建的路径元素时,路径消失了。

我做了一个 jsfiddle 来显示问题。我使用了 Mike Bostock (http://bl.ocks.org/mbostock/3887235) 的饼图示例,并应用了另一个示例中的掩码。 我正在尝试将遮罩应用于饼图(5-13 岁的切片)并且它没有显示。 我什至认为这是 svg 路径元素的问题,但如果我在 svg(jsfiddle 上的蓝色矩形)上显式创建路径,则掩码有效。

有人知道为什么会这样吗? d3 arc 函数中是否缺少任何配置?我应该做的任何步骤,但我没有?我真的很想通过 css 使用掩码。

我正在应用掩码的代码部分:

// selecting slice with population (4499890)
d3.select('#id_4499890').classed('hbar',true);

jsfiddle 显示问题。

谢谢!

【问题讨论】:

    标签: javascript css d3.js svg


    【解决方案1】:

    你的面具是一个rect,在y方向上占据0到100%的空间,你的弧path虽然在y方向上占据了-46到-125的空间,两者根本不重叠.

    所以,简单地开始你的 rect more 否定:

    defs.append("mask")
        .attr("id","mask-stripe")
        .append("rect")
        .attr("x","-200")
        .attr("y","-200")
        .attr("width","100%")
        .attr("height","100%")
        .attr("fill",'url(#pattern-stripe)');
    

    更新fiddle


    完整代码:

    //adding the pattern
    var defs = d3.select("#svgPattern").append("defs");
      defs.append("pattern")
        .attr("id","pattern-stripe")
        .attr("width","4")
        .attr("height","4")
        .attr("patternUnits","userSpaceOnUse")
        .attr("patternTransform","rotate(45)")
        .append("rect")
        .attr("width","2")
        .attr("height","4")
        .attr("transform","translate(0,0)")
        .attr("fill","white");
    
      defs.append("mask")
        .attr("id","mask-stripe")
        .append("rect")
        .attr("x","-200")
        .attr("y","-200")
        .attr("width","100%")
        .attr("height","100%")
        .attr("fill",'url(#pattern-stripe)');
    
    function drawChart(){
      var width = 660,
          height = 300,
          radius = Math.min(width, height) / 2;
    
      var color = d3.scale.ordinal()
          .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
    
      var arc = d3.svg.arc()
          .outerRadius(radius - 10)
          .innerRadius(0);
    
      var labelArc = d3.svg.arc()
          .outerRadius(radius - 40)
          .innerRadius(radius - 40);
    
      var pie = d3.layout.pie()
          .sort(null)
          .value(function(d) { return d.population; });
    
      function type(d) {
        d.population = +d.population;
        return d;
      }
    
      var svg = d3.select("#svgPattern")
          .attr("width", width)
          .attr("height", height)
        	.append("g")
          .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
    
        var g = svg.selectAll(".arc")
            .data(pie(data))
          	.enter().append("g")
            .attr("class", "arc");
    
        g.append("path")
            .attr("d", arc)
            .attr("id", function(d) { return 'id_'+d.data.population; })
            .style("fill", function(d) { return color(d.data.age); });
    
        g.append("text")
            .attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")";})
            .attr("dy", ".35em")
            .text(function(d) { return d.data.age; });
    
          // selecting slice with population (4499890)
          d3.select('#id_4499890').classed('hbar',true);
    }
    
    var data = [
      {
        "age": "<5",
        "population": 2704659
      },
      {
        "age": "5-13",
        "population": 4499890
      },
      {
        "age": "14-17",
        "population": 2159981
      },
      {
        "age": "18-24",
        "population": 3853788
      },
      {
        "age": "25-44",
        "population": 14106543
      },
      {
        "age": "45-64",
        "population": 8819342
      },
      {
        "age": "≥65",
        "population": 612463
      }
    ];
    
    drawChart();
    .arc text {
      font: 10px sans-serif;
      text-anchor: middle;
    }
    
    .arc path {
      stroke: #fff;
    }
    
    .hbar {
      mask: url(#mask-stripe)
    }
    .thing-2{
      fill: green;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <!-- 
      Pie chart from: http://bl.ocks.org/mbostock/3887235 
      Mask example from: https://bl.ocks.org/jfsiii/7772281
      --> 
    <svg id="svgPattern" >
      <!-- bar chart -->
      <rect class="hbar thing-2" x="0" y="0" width="50" height="100"></rect>
      <rect class="hbar thing-2" x="51" y="50" width="50" height="50"></rect>
      <rect class="hbar thing-2" x="102" y="25" width="50" height="75"></rect>
      <path d="M0,150 150,150 150,200 0,200" class="hbar" fill="blue" />
      
    </svg>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 2019-02-26
      • 2014-08-03
      • 2014-03-14
      • 1970-01-01
      相关资源
      最近更新 更多