我试图将纹理(例如条纹)放在 d3 arc 函数创建的 svg 路径元素上。 我找到了这个示例( https://bl.ocks.org/jfsiii/7772281 ),这正是我想要的(使用 css 应用 mask ),但是当我应用于 d3 arc 函数创建的路径元素时,路径消失了。
我做了一个jsfiddle来展示这个问题。我使用了 Mike Bostock 的饼图示例 (http://bl.ocks.org/mbostock/3887235),并应用了其他示例中的掩码。 我正在尝试将蒙版应用到饼图(5-13 岁的切片),但它没有显示。 我什至认为这是 svg 路径元素的问题,但如果我在 svg 上创建显式路径(jsfiddle 上的蓝色矩形),则掩码可以工作。
有人知道为什么会发生这种情况吗?我在 d3 arc 函数中缺少任何配置吗?有哪些步骤是我应该做而我没有做的?我真的很想使用 css 的 mask。
我应用蒙版的代码部分:
// selecting slice with population (4499890)
d3.select('#id_4499890').classed('hbar',true);
jsfiddle显示问题。
谢谢!
最佳答案
您的掩码是一个矩形
,在 y 方向上占据从 0 到 100% 的空间,而您的弧路径
在 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)');
更新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>
关于javascript - 由 d3 (d3.svg.arc) 创建的 SVG 路径元素在应用蒙版时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281586/