如何在带有d3.js的Sunburst图表上定位文本标签 [英] How to position text labels on a Sunburst chart with d3.js

查看:230
本文介绍了如何在带有d3.js的Sunburst图表上定位文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,试图将文本放置在基于d3.js的Sunburst图表的楔形内。 text 元素似乎没有定位即使在缩放..

I am facing a problem trying to position text inside the wedges of a Sunburst chart which is based on d3.js.The text elements seem to be not positioned as desired even on zooming..

    var slices = svg.selectAll(".form")
            .data(function(d) { return data_slices; })
            .enter()
            .append("g");

        slices.append("path")
            .attr("d", arc)
            .attr("id",function(d,i){return d[2]+""+i;})
            .style("fill", function(d) { return color(d[2]);})
            .on("click",animate)
            .attr("class","form")
            .append("svg:title")
            .text(function(d) { return Math.round(d[0]*100)/100 +" , "+ Math.round(d[1]*100)/100; });

       //Something needs to change below....
      slices.append("text")
            .style("font-size", "10px")
            .attr("x", function(d) { return y(d[1]); })
            .attr("transform", function(d) { return "rotate(" + this.parentNode.getBBox().width + ")"; })
            .attr("dx", "6") // margin
            .attr("dy", ".35em") // vertical-align
            .text(function(d){return d[2]})
            .attr("pointer-events","none");

这是图表的小提琴

小提琴

有什么可能问题?任何人都可以告诉我或指导我如何定位< text> 里面svg < path>

What can be possible problem ? and can anyone please tell me or guide me as to how to position the <text> inside svg <path>.Looks like the solution is a minor tweak to this, but i am not able to get to it even after trying for a long time..

任何帮助/注释(可选)在解决方案将非常感谢...感谢在前进..

Any help/comment in the direction of a solution would be greatly appreciated...Thanks in Advance..

推荐答案

我认为这接近什么您的目标是: http://jsfiddle.net/4PS53/3/

I think this comes close to what you aimed to achieve: http://jsfiddle.net/4PS53/3/

所需的更改如下:

function getAngle(d) {
    // Offset the angle by 90 deg since the '0' degree axis for arc is Y axis, while
    // for text it is the X axis.
    var thetaDeg = (180 / Math.PI * (arc.startAngle()(d) + arc.endAngle()(d)) / 2 - 90);
    // If we are rotating the text by more than 90 deg, then "flip" it.
    // This is why "text-anchor", "middle" is important, otherwise, this "flip" would
    // a little harder.
    return (thetaDeg > 90) ? thetaDeg - 180 : thetaDeg;
}

slices.append("text")
    .style("font-size", "10px")
    .attr("x", function(d) { return d[1]; })
     // Rotate around the center of the text, not the bottom left corner
    .attr("text-anchor", "middle")
     // First translate to the desired point and set the rotation
     // Not sure what the intent of using this.parentNode.getBBox().width was here (?)
    .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")" + "rotate(" + getAngle(d) + ")"; })                                    
    .attr("dx", "6") // margin
    .attr("dy", ".35em") // vertical-align
    .text(function(d){return d[2]})
    .attr("pointer-events","none");

现在为了使它与缩放工作,参考点改变,我们需要一点基础设施。

Now to make it work with zooming, where the reference point changes, we need a bit more infrastructure.


  1. 创建 g.form-container ,而不仅仅是 path.form visible / invisible。这意味着我们不必担心使标签单独消失。 (我已添加 form-container 类。)

  2. 计算新点<计算 centroid rotation 。这有点棘手,但不是太难:

  1. Make the g.form-container and not only the path.form visible/invisible. This means that we do not have to worry about making the labels disappear separately. (I have added the form-container class.)
  2. Calculate the new point and calculate the centroid and rotation for it. This is a bit more tricky, but not too difficult:

function change_ref(data_point, reference_point) {
    return [
        get_start_angle(data_point, reference_point),
        get_stop_angle (data_point, reference_point),
        data_point[2],
        get_level      (data_point, reference_point)
    ];
}

// And while doing transitioning the `text.label`:

svg.selectAll('.label')
.filter(
    function (b)
    {
        return b[0] >= new_ref[0] && b[1] <= new_ref[1] && b[3] >= new_ref[3];
    }
).transition().duration(1000)
 .attr("transform", function(b) {
     var b_prime = change_ref(b, d);
     return "translate(" + arc.centroid(b_prime) + ")" + 
            "rotate("    + getAngle(b_prime) +     ")"; 
 })

我已经添加了标签文本

更新演示: http://jsfiddle.net/4PS53/6/

但是,我认为可能有更好的方式来呈现这些数据,尤其是。如果您允许缩放和平移:如果有足够的空间,D3将圆弧标签放在饼图中

However, I have argued that there might be better ways of presenting this data, esp. if you are allowing zooming and panning: D3 put arc labels in a Pie Chart if there is enough space

这篇关于如何在带有d3.js的Sunburst图表上定位文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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