1
我已经SVG rect元素,我想用它来将Donut分成几部分。CSS3 - 转换起源不工作在d3.js中的SVG元素
但是这些矩形(辐条)不是直线对齐的。
我使用D3.js,请建议,如果有更好的办法有辐轮效应
Transform Origin on SVG element
rect = wheel.selectAll("g.rect")
.data(rectData).enter()
.append("g")
.attr("transform",function(d,i){
var rotate = 60*i;
var rotX = 100, rotY = 150;
return "translate("+rotX+","+rotY+") rotate("+rotate+")"
})
.attr("class","rect");
var width = 200,
height = 300,
innerRadius = 100,
outerRadius = 80;
var wheel = d3.select("#wheel")
.append("svg")
.attr("width", width)
.attr("height", height)
arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius)
.startAngle(0)
.endAngle(2 * Math.PI)
rectData = [{
x: width/2,
y: height/2
}, {
x: width/2,
y: height/2
}, {
x: width/2,
y: height/2
}, {
x: width/2,
y: height/2
}, {
x: width/2,
y: height/2
}, {
x: width/2,
y: height/2
}]
rect = wheel.selectAll("g.rect")
.data(rectData).enter()
.append("g")
.attr("transform", function(d, i) {
var rotate = 60 * i;
var rotX = 100,
rotY = 150;
return "translate(" + rotX + "," + rotY + ") rotate(" + rotate + ")"
})
.attr("class", "rect");
rect.append("rect")
.attr("width", 20)
.attr("height", outerRadius + 10)
wheel.append("path").attr("d", arc)
.attr("transform", "translate(" + width/2 + "," + height/2 + ")")
.attr("class", "donut")
#wheel {
margin: auto 0;
width: 300px;
height: 300px
}
#wheel .donut {
fill: #F37E36;
}
#wheel rect {
fill: #A2A931;
transform-origin: 90 50;
}
#wheel .rect rect {
transform-origin: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
<div id="wheel"></div>
[难道有人想其中的一个?](http://codepen.io/skyinlayer/pen/EDwsf) – jbutler483 2015-02-09 13:36:02
您需要使用CSS transfrom属性,而不是SVG属性,如果你想在CSS中修改它。 – bravedick 2015-02-09 13:36:29
@ jbutler483看起来不错,但更好,如果与JS,作为车轮辐条将有一些文字 – ravins 2015-02-09 13:45:11