【问题标题】:d3.js - how to arrange the `squre` box around the `circle` properlyd3.js - 如何正确排列“圆形”周围的“方形”框
【发布时间】:2015-06-02 02:37:40
【问题描述】:

我正在尝试在circle 周围安排squares,但我无法获得正确的输出。

谁能帮帮我?

// largely based on http://bl.ocks.org/4063550

// some made-up data
var data = [2,2,2,2,2,2];

// tree-ify our fake data
var dataTree = {
    children: data.map(function(d) { return { size: d }; })
};

// basic settings
var w = 300,
    h = 300,
    maxRadius = 75;


// size scale for data
var radiusScale = d3.scale.sqrt().domain([0, d3.max(data)]).range([0, maxRadius]);

// determine the appropriate radius for the circle
var roughCircumference = d3.sum(data.map(radiusScale)) * 2,
    radius = roughCircumference / (Math.PI * 2);

// make a radial tree layout
var tree = d3.layout.tree()
    .size([360, radius])
    .separation(function(a, b) {
        return radiusScale(a.size) + radiusScale(b.size);
    });

// make the svg
var svg = d3.select("body").append("svg")
    .attr("width", w )
    .attr("height", h )
    .append("g")
    .attr("transform", "translate(" + (w / 2 ) + "," + (h /2) + ")");

var c = svg.append('circle').attr({r:75})

// apply the layout to the data
var nodes = tree.nodes(dataTree);

// create dom elements for the node
var node = svg.selectAll(".node")
      .data(nodes.slice(1)) // cut out the root node, we don't need it
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) {
          console.log(d.x);
          return "rotate(" + (d.x - 90) + ") translate(" + d.y + ")";
      })

node.append("rect")
    .attr({
    width: 25,
    height:25,
    fill : 'red',
    "transform":function(d) {
         return "rotate(" + (-1 * d.x + 90) + ") translate(" +0+ ")";
      }
});
node.append("text")
.attr({"transform":function(d) {
         return "rotate(" + (-1 * d.x + 90) + ")";
      },
       "text-anchor": "middle"
      })
.text("testing a word");
svg {
    border:1px solid gray;
}

circle {
   fill: steelblue;
   stroke: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
我正在寻找这样的输出:

【问题讨论】:

  • 您需要更好地表达您的问题。你期望什么样的输出?如果您希望得到任何帮助,我们将需要有关您想要什么的更多详细信息。
  • 让我添加我需要的输出。
  • @SiddharthShukla - 为我的要求添加了一张图片。我同意这里缺少一些计算。
  • 这是您今天就这个问题提出的第三个问题。对于安排,我已经向您指出了一个完全相同的问题。您能否解释一下您是如何“无法获得正确的输出”的?
  • 我无法根据圆的中心点定位方形。我很抱歉重复这个有多个要求的问题。因为如果我不创建逻辑,它就会发生。由于时间不多了,所以我急于融入项目。

标签: javascript d3.js


【解决方案1】:

可使用的示例代码。 我假设要绘制 8 个节点,以便将圆分成 8 段。每个正方形放置在 Pi/4 弧度的距离处。您可以将 x,y 计算为 xSin , y Cos。然后您需要将矩形转换为以 x,y 为中心而不是左上角。

// largely based on http://bl.ocks.org/4063550

// some made-up data
var data = [2,2,2,2,2,2,2,2];

// tree-ify our fake data
var dataTree = {
    children: data.map(function(d) { return { size: d }; })
};

// basic settings
var w = 300,
    h = 300,
    maxRadius = 75;


// size scale for data
var radiusScale = d3.scale.sqrt().domain([0, d3.max(data)]).range([0, maxRadius]);

// determine the appropriate radius for the circle
var roughCircumference = d3.sum(data.map(radiusScale)) * 2,
    radius = roughCircumference / (Math.PI * 2);

// make a radial tree layout
var tree = d3.layout.tree()
    .size([360, radius])
    .separation(function(a, b) {
        return radiusScale(a.size) + radiusScale(b.size);
    });

// make the svg
var svg = d3.select("body").append("svg")
    .attr("width", w )
    .attr("height", h )
    .append("g")
    .attr("transform", "translate(" + (w / 2 ) + "," + (h /2) + ")");

var c = svg.append('circle').attr({r:75})
var r = 75;
// apply the layout to the data
var nodes = tree.nodes(dataTree);

// create dom elements for the node
var node = svg.selectAll(".node")
      .data(nodes.slice(1)) // cut out the root node, we don't need it
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d,i) {     		  
          return "translate(" + (r * Math.sin(Math.PI * i * 0.25)) + "," + (r * Math.cos(Math.PI * i * 0.25)) + ")";
      })

node.append("rect")
    .attr({
    width: 25,
    height:25,
    fill : 'red',
    "transform":function(d) {
         return "translate(" +(-12.5)+ ","+ (-12.5) + ")";
      }
});
node.append("text")
.attr({"transform":function(d) {
         return "rotate(" + (-1 * d.x + 90) + ")";
      },
       "text-anchor": "middle"
      })
.text("testing a word");
svg {
    border:1px solid gray;
}

circle {
   fill: steelblue;
   stroke: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

【讨论】:

  • 是的,这是一个比我的更具可扩展性的解决方案。 :D
【解决方案2】:

这是fiddle。 好的,所以这基本上是一些像素级的反复试验translate 操作。这是给node的。

.attr("transform", function(d) {
          console.log(d.x);
          return "rotate(" + (d.x - 90) + ") translate(" + (d.y - 65 ) + ")";
      })

这是rect

.attr({
    width: 25,
    height:25,
    fill : 'red',
    "transform":function(d) {
         return "rotate(" + -(d.x - 90) + ") translate(" +(-10)+ ","+ (-10) + ")";
      }
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2011-07-19
  • 2018-12-02
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
相关资源
最近更新 更多