当应用蒙版时,由d3(d3.svg.arc)创建的SVG路径元素消失

断裂

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

我做了一个jsfiddle来显示问题。我使用了Mike Bostock(http://bl.ocks.org/mbostock/3887235)中的饼图示例,并使用了另一个示例中的蒙版。我正在尝试将蒙版应用于饼图(5-13岁年龄段),但未显示。我什至认为这是svg path元素的问题,但是如果我在svg上显式创建路径(在jsfiddle上为蓝色矩形),则掩码将起作用。

任何人都知道为什么会这样吗?d3 arc函数中缺少任何配置吗?我应该做的任何步骤,但我不是?我真的很想使用CSS的mask。

我在其中应用遮罩的代码部分:

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

的jsfiddle显示的问题。

谢谢!

标记

您的蒙版是rect在y方向上从0到100%的path占位空间,尽管您的圆弧在y方向上是从-46到-125的占位空间,但两者根本不重叠。

所以,简单地启动你的矩形负:

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)');

更新小提琴


完整代码:

//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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章