我试图在某些弧线外创建标签。我看过这篇文章: Label outside arc (Pie chart) d3.js 但仍然无法使其发挥作用。不知何故,DOM中只出现一个文本标签,其位置不正确。
这是JSFiddle链接:https://jsfiddle.net/SashimiEthan/rLe9g3bq/
这是我的代码:
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="d3.min.js"></script>
</head>
<body>
<script>
var margin = {top: 10, right: 50, bottom: 10, left: 50},
width = 300,
r = width / 2,
labelr = r + 20,
outerRadius = 150,
innerRadius = outerRadius - 30;
numberOfSegments = 12;
radians = (Math.PI * 2) / numberOfSegments;
degrees = 360 / numberOfSegments;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", 530);
for (var j = 0; j < 5; j++) {
var arc = d3.svg.arc()
.innerRadius(innerRadius - j*30)
.outerRadius(outerRadius - j*30)
.startAngle(function (d,i) { return radians * i } )
.endAngle(function (d,i) { return radians * (i + 1) });
var myGroup = svg.append("g")
.attr("transform", "translate(200,200)")
.attr("class","arcgroup")
.selectAll("path").data(d3.range(numberOfSegments))
.enter().append("path")
.attr("class", "seg"+j+"")
.attr("d", arc)
.attr("fill", function(d,i) {
return d3.hsl(i * degrees, 1 - 0.2*j, 0.5)
});
}
var pos = d3.svg.arc()
.innerRadius(innerRadius+30)
.outerRadius(outerRadius+30)
.startAngle(function (d,i) { return radians * i } )
.endAngle(function (d,i) { return radians * (i + 1) });
var label = svg.append("text")
.attr("transform", function(d,i) {
return "translate(" + pos.centroid(d,i) + ")";
})
.attr("text-anchor", "middle")
.text(function(d,i){
return i*degrees + "°";
});
</script>
</body>
</html>
非常感谢!
答案 0 :(得分:2)
您在没有选择的情况下处理svg:您只是在没有附加任何数据的情况下附加单个元素,这就是为什么d=undefined
和i=0
在您的例如transform
功能。
使用您在群组创作中所做的选择:类似
var selection = svg.append("g").attr("transform", "translate(200,200)").
selectAll("text").data(d3.range(numberOfSegments)).enter()
var label = selection.append("text")
.attr("transform", function (d) {
return "translate(" + pos.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function (d) {
return d * degrees + "°";
});
一个演示(标签&#39;定位可以使用一些工作,但这应该让你开始)
var margin = {
top: 10,
right: 50,
bottom: 10,
left: 50
},
width = 300,
r = width / 2,
labelr = r + 20,
outerRadius = 150,
innerRadius = outerRadius - 30;
numberOfSegments = 12;
radians = (Math.PI * 2) / numberOfSegments;
degrees = 360 / numberOfSegments;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", 530);
for (var j = 0; j < 5; j++) {
var arc = d3.svg.arc()
.innerRadius(innerRadius - j * 30)
.outerRadius(outerRadius - j * 30)
.startAngle(function (d, i) {
return radians * i
}).endAngle(function (d, i) {
return radians * (i + 1)
});
var myGroup = svg.append("g")
.attr("transform", "translate(200,200)")
.attr("class", "arcgroup")
.selectAll("path").data(d3.range(numberOfSegments))
.enter().append("path")
.attr("class", "seg" + j + "")
.attr("d", arc)
.attr("fill", function (d, i) {
return d3.hsl(i * degrees, 1 - 0.2 * j, 0.5)
});
}
var pos = d3.svg.arc()
.innerRadius(innerRadius+30)
.outerRadius(outerRadius+30)
.startAngle(function (d) {
return radians * d
}).endAngle(function (d) {
return radians * (d + 1)
});
var selection = svg.append("g").attr("transform", "translate(200,200)").
selectAll("text").data(d3.range(numberOfSegments)).enter()
var label = selection.append("text")
.attr("transform", function (d) {
return "translate(" + pos.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function (d) {
return d * degrees + "°";
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;