【问题标题】:Cannot rotate text d3js无法旋转文本d3js
【发布时间】:2012-12-19 18:51:12
【问题描述】:

我正在尝试向partition-sunburst example 添加文本。我遵循了 Google Group 上给出的提示,并且能够添加文本。现在我想旋转它。新的角度似乎是正确的,但所有的文字都集中在一个点上。

这是我添加和旋转文本的一段代码:

var text = vis.data([json]).selectAll("text")
    .data(partition.nodes)
      .enter().append("svg:text")
        .attr("x", function(d) { return radius * Math.cos(d.x + d.dx/2); } )
        .attr("y", function(d) { return radius * Math.sin(d.x + d.dx/2); } )
        .attr("dy", ".31em")
        .style("font-size", "5px")
        .attr("transform", function(d) {
          var angle = (2 * Math.PI - d.x) * (180/Math.PI);
          var rotation = "rotate(" + angle + ")";
          console.log(rotation);
          console.log("d.x=" + d.x + ", d.y=" + d.y);
          return rotation;
        })
        .text(function(d) { return d.name; });

这是我得到的图像:

这是完整的脚本:

var width = 1000,
    height = 1000,
    radius = 350,
    x = d3.scale.linear().range([0, 2 * Math.PI]),
    y = d3.scale.pow().exponent(1.3).domain([0, 1]).range([0, radius]),
    padding = 5,
    color = d3.scale.category20c();

var vis = d3.select("#chart").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var partition = d3.layout.partition()
    .sort(null)
    .size([2 * Math.PI, radius * radius])
    .value(function(d) { return 1; });

var arc = d3.svg.arc()
    .startAngle(function(d) { return d.x; })
    .endAngle(function(d) { return d.x + d.dx; })
    .innerRadius(function(d) { return Math.sqrt(d.y); })
    .outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });

d3.json("fracking.json", function(json) {
  var path = vis.data([json]).selectAll("path")
      .data(partition.nodes)
    .enter().append("svg:path")
      .attr("display", function(d) { return d.depth ? null : "none"; })
      // hide inner ring
      .attr("d", arc)
      .attr("fill-rule", "evenodd")
      .style("stroke", "#fff")
      .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
  .each(stash);

   var text = vis.data([json]).selectAll("text")
    .data(partition.nodes)
      .enter().append("svg:text")
        .attr("x", function(d) { return radius * Math.cos(d.x + d.dx/2); } )
        .attr("y", function(d) { return radius * Math.sin(d.x + d.dx/2); } )
        .attr("dy", ".31em")
        .style("font-size", "5px")
        .attr("transform", function(d) {
          var angle = (2 * Math.PI - d.x) * (180/Math.PI);
          var rotation = "rotate(" + angle + ")";
          console.log(rotation);
          console.log("d.x=" + d.x + ", d.y=" + d.y);
          return rotation;
        })
        .text(function(d) { return d.name; });

  d3.select("#size").on("click", function() {
    path
        .data(partition.value(function(d) { return d.size; }))
      .transition()
        .duration(1500)
        .attrTween("d", arcTween);
    d3.select("#size").classed("active", true);
    d3.select("#count").classed("active", false);
  });
  d3.select("#count").on("click", function() {
    path
        .data(partition.value(function(d) { return 1; }))
      .transition()
        .duration(1500)
        .attrTween("d", arcTween);
    d3.select("#size").classed("active", false);
    d3.select("#count").classed("active", true);
  });
});

// Stash the old values for transition.
function stash(d) {
  d.x0 = d.x;
  d.dx0 = d.dx;
}

// Interpolate the arcs in data space.
function arcTween(a) {
  var i = d3.interpolate({x: a.x0, dx: a.dx0}, a);
  return function(t) {
    var b = i(t);
    a.x0 = b.x;
    a.dx0 = b.dx;
    return arc(b);
  };
}

【问题讨论】:

  • 在提出此类问题时,您应该为您的示例创建一个jsFiddle。它使回答问题的人更容易看到您的代码示例正在运行并且易于编辑。
  • 问题是关于旋转文本。正在制作的图表类型无关紧要。请不要添加该标签。

标签: javascript svg d3.js label sunburst-diagram


【解决方案1】:

我认为您需要指定旋转中心。试试这个:

var text = vis.data([json]).selectAll("text")
.data(partition.nodes)
  .enter().append("svg:text")
    .attr("x", function(d) { return radius * Math.cos(d.x + d.dx/2); } )
    .attr("y", function(d) { return radius * Math.sin(d.x + d.dx/2); } )
    .attr("dy", ".31em")
    .style("font-size", "5px")
    .attr("transform", function(d) {
      var angle = (2 * Math.PI - d.x) * (180/Math.PI);
      var rotation = "rotate(" + angle + "," + d3.select(this).attr("x") + "," d3.select(this).attr("y") +")";
      console.log(rotation);
      console.log("d.x=" + d.x + ", d.y=" + d.y);
      return rotation;
    })
    .text(function(d) { return d.name; });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2013-08-16
    • 2021-05-11
    • 2013-05-18
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多