简体   繁体   English

D3.js,CSS3-旋转矩形以使其具有一条直线

[英]D3.js,CSS3 - rotate rectangles to have in a straight line

I'm using D3.js to create a arc and 4 rectangels. 我正在使用D3.js创建一个圆弧和4个矩形。 These Ractangles are dividing Donut into parts. 这些Ractangles将甜甜圈分为几部分。

I want Ractangles to be aligned in straight line 我希望Ractangles沿直线对齐

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},
]
rect = wheel.selectAll("g.rect")
  .data(rectData).enter()
  .append("g")
 .attr("transform",function(d,i){
   var rotate = 90*i;
   return "translate(100,150) rotate("+rotate+")"
 }).attr("class","rect")

rect.append("rect")
  .attr("width",20).attr("height",outerRadius)

wheel.append("path").attr("d",arc)
  .attr("transform","translate("+width/2+","+height/2+")")
  .attr("class","donut")

I'm using transform-origin, but not working. 我正在使用转换起源,但无法正常工作。

http://jsfiddle.net/kmdr72wc/4/ http://jsfiddle.net/kmdr72wc/4/

transform-origin is a CSS property but you are rotating with an SVG attribute. transform-origin是CSS属性,但是您正在使用SVG属性旋转。

SVG rotate allows you to set the origin but for you situation it might just be easier to fix the translate: SVG rotate允许您设置原点,但是根据您的情况,修复转换可能会更容易:

.attr("transform",function(d,i){
  var rotate = 90*i;
  var rotX = 100, rotY = 150;
  if (i === 0){
    rotX -= 10;
  } else if (i === 1){
    rotY -= 10;
  } else if (i === 2){
    rotX += 10;
  } else if (i === 3){
    rotY += 10;
  }
  return "translate("+rotX+","+rotY+") rotate("+rotate+")";
});

Update fiddle here . 在这里更新小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM