使用d3.js旋转文本时使其保持水平 [英] Keeping text horizontal while it's position is rotating with d3.js

查看:83
本文介绍了使用d3.js旋转文本时使其保持水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,我尝试为太阳周围的行星添加标签: http://bl.ocks.org/djvanderlaan/4953593 .到目前为止,我已经设法添加了标签,但是标签的方向随着它们的位置而旋转,同时我想将它们保持水平以使读者感到舒适.我一直在这里找到解决问题的方法的开始:

I'm trying to add labels to the planets around the sun into this example : http://bl.ocks.org/djvanderlaan/4953593. So far, I've managed to add the labels, but the orientation of the labels is rotating with their position, while I want to keep them horizontal for the comfort of the readers. I've been finding a beginning of a solution to my problem here : how to keep text orientation unchanged during rotation in SVG but it's seems very complicated to me (I am a newbie and really not good at trigonometry) and plus, it's not using d3.js. Here is the code that I'am using :

<div id="planetarium">
</div>

<script type="text/javascript">
  var w = 800, h = 600;
  var t0 = Date.now();

  var planets = [
    { R: 300, r:  5, speed: 5, phi0: 90, name : 'Mercure'},
    { R: 150, r: 10, speed: 2, phi0: 190, name : 'Saturne'}
  ];


  var svg = d3.select("#planetarium").insert("svg")
    .attr("width", w).attr("height", h);

  svg.append("circle").attr("r", 20).attr("cx", w/2)
    .attr("cy", h/2).attr("class", "sun")

  var container = svg.append("g")
    .attr("transform", "translate(" + w/2 + "," + h/2 + ")")


  container.selectAll("g.planet").data(planets).enter().append("g")
    .attr("class", "planet").each(function(d, i) {
      var orbit = d3.select(this).append("circle").attr("class", "orbit")
        .attr("r", d.R);
      var planet = d3.select(this).append("circle").attr("r", d.r).attr("cx",d.R)
        .attr("cy", 0).attr("class", "planet");
      var text = d3.select(this).append("text")
        .attr("x", d.R)
        .attr("y", ".31em")
        .text(function(d) { return d.name; });


  d3.timer(function() {
    var delta = (Date.now() - t0);
    planet.attr("transform", function(d) {
      return "rotate(" + d.phi0 + delta * d.speed/200 + ")";
    });
    text.attr("transform", function(d) {
      return "rotate(" + d.phi0 + delta * d.speed/200 + ")";
    });
  });
 });

</script>

这是我的朋克: http://plnkr.co/edit/dJEVXIeR7ly536tcMPWt?p=预览非常感谢您的帮助!

Here is my plunkr : http://plnkr.co/edit/dJEVXIeR7ly536tcMPWt?p=preview Thank you very much for your help !

推荐答案

我终于找到了一个受此示例启发的解决方案:

I've finally found a solution inspired by this example : D3.js: rotate group, keep text the same orientation? Instead of making two different variables for planets and text, I've gathered them in a same rotating group, and then added an inverse rotation on the text, but centered on the planet's center rather than the center of the container. Then I set both phi0 (the positions of the planets at the beginning of the animation) to 0, so that the text would be frozen horizontally. Here is my code :

var planets = [
    { R: 300, r:  5, speed: 5, phi0: 0, name : 'Mercure'},
    { R: 150, r: 10, speed: 2, phi0: 0, name : 'Saturne'}
  ];
//... 
var container = svg.append("g")
    .attr("transform", "translate(" + w/2 + "," + h/2 + ")")

  var orbit = container.selectAll(".orbit")
                       .data(planets)
                       .enter()
                       .append("circle")
                       .attr("class", "orbit")
                       .attr("r", function(d) {return d.R;});
  var planets = container.selectAll(".planet")
                       .data(planets)
                       .enter()
                       .append("g")

    planets.append("circle")
                       .attr("class", "planet")
                       .attr("r", function(d) {return d.r;})
                       .attr("cx", function(d) {return d.R; })
                       .attr("cy", 0);


    planets.append("text")
                       .attr("dx", function(d) {return d.R;})
                       .attr("dy", ".35em")
                       .text(function(d) {return d.name});


  d3.timer(function() {
    var delta = (Date.now() - t0);
    planets.attr("transform", function(d) {
      return "rotate(" + d.phi0 + delta * d.speed/200 + ")";
    });
    planets.selectAll("text").attr("transform", function(d) {
      return "rotate(" + -1*(d.phi0 + delta * d.speed/200) + " " + d.R + " " + 0 + ")";
    });
  });

不确定这是否是一个很好的解决方案,但目前可以正常使用.我保证,我会学三角学的;)

Not sure this is a very good solution, but for the moment it works. I will learn trigonometry though, I promise ;)

这篇关于使用d3.js旋转文本时使其保持水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆