javascript - D3 : Rotating labels in the third and fourth quad in sunburst example

标签 javascript d3.js label sunburst-diagram

我是第一次使用 D3 和 javascript 来绘制森伯斯特,以下示例位于 http://bl.ocks.org/mbostock/http://bl.ocks.org/mbostock/4063423 .第三个和第四个四边形中的标签是颠倒的。我在这里找到了咖啡轮示例 http://www.jasondavies.com/coffee-wheel/并尝试将其纳入我的示例,但未能成功旋转标签。我确实在这里找到了类似的帖子 How to properly rotate text labels in a D3 sunburst diagram ,但无法解决问题,因为其中一个链接无效。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

    path {
        stroke: #fff;
        fill-rule: evenodd;
    }

    text {
        font-family: Arial, sans-serif;
        font-size: 12px;
    }

</style>
<body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>

        var width = 1600,
        height = 1300,
        radius = Math.min(width, height) / 2;

        var x = d3.scale.linear()
        .range([0, 2 * Math.PI]);

        var y = d3.scale.linear()
        .range([0, radius]);

        var color = d3.scale.category20c();

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

        var partition = d3.layout.partition()
        .value(function(d) { return d.size; });

        var arc = d3.svg.arc()
        .startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
        .endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
        .innerRadius(function(d) { return Math.max(0, y(d.y)); })
        .outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });

        d3.json("ex.json", function(error, root) {
            var g = svg.selectAll("g")
            .data(partition.nodes(root))
            .enter().append("g");

            var path = g.append("path")
            .attr("d", arc)
            .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
            .on("click", click);

            var text = g.append("text")
            .attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
            .attr("x", function(d) { return y(d.y); })
            .attr("dx", "6") // margin
            .attr("dy", ".35em") // vertical-align
            .text(function(d) { return d.name; });

            function click(d) {
                // fade out all text elements
                text.transition().attr("opacity", 0);

                path.transition()
                .duration(750)
                .attrTween("d", arcTween(d))
                .each("end", function(e, i) {
                      // check if the animated element's data e lies within the visible angle span given in d
                      if (e.x >= d.x && e.x < (d.x + d.dx)) {
                          // get a selection of the associated text element
                          var arcText = d3.select(this.parentNode).select("text");
                          // fade in the text element and recalculate positions
                          arcText.transition().duration(750)
                          .attr("opacity", 1)
                          .attr("transform", function() { return "rotate(" + computeTextRotation(e) + ")" })
                          .attr("x", function(d) { return y(d.y); });
                      }
                });
            }
        });

        d3.select(self.frameElement).style("height", height + "px");

        // Interpolate the scales!
        function arcTween(d) {
            var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
            yd = d3.interpolate(y.domain(), [d.y, 1]),
            yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
            return function(d, i) {
                return i
                ? function(t) { return arc(d); }
                : function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
            };
        }

        function computeTextRotation(d) {
            return (x(d.x + d.dx / 2) - Math.PI / 2) / Math.PI * 180;
        }

    </script>

ex.json

[{
 "name": "aaa",
 "size": 5000,
 "children":
    [
        {
        "name": "aaab",
        "size": 2952,
        "children": [
                  {"name": "xxx", "size": 45},
                  {"name": "xxy", "size": 29},
                  {"name": "xxz", "size": 28},
                  {"name": "xxa", "size": 4}
                  ]
        },

        {
        "name": "aaac",
        "size": 251,
        "children": [
                        {
                        "name": "ddd",
                        "size": 7,
                        "children": [
                                     {"name": "ppp", "size": 4},
                                     {"name": "qqq", "size": 2}
                                     ]
                        },
                        {"name": "xxt", "size": 4},
                        {"name": "xxu", "size": 1},
                        {"name": "xxv", "size": 1}
                     ]
        },
        {"name": "aaad","size": 222}
     ]
}][1]

最佳答案

将您的 computeTextRotation 函数更改为以下内容:

    function computeTextRotation(d) {
        var ang = (x(d.x + d.dx / 2) - Math.PI / 2) / Math.PI * 180;
        return (ang > 90) ? 180 + ang : ang;
    }

并将旋转点设置为相应圆弧的质心位置:

   var text = g.append("text")
        .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")rotate(" + computeTextRotation(d) + ")"; })
        .attr('text-anchor', function (d) { return computeTextRotation(d) > 180 ? "end" : "start"; })
        .attr("dx", "6") // margin
        .attr("dy", ".35em") // vertical-align
        .text(function(d) { return d.name; });

示例:http://jsfiddle.net/Hm49x/

使用您的数据:http://plnkr.co/edit/tct95toQ2IjyUkQJQPB9?p=preview

关于javascript - D3 : Rotating labels in the third and fourth quad in sunburst example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393427/

相关文章:

Javascript 按键计算对象数组的百分比总计

vba - 在 VBA 中使用动态命名范围进行散点图标记

javascript - 回调函数作用域在 "new"on JS object literal

animation - 使用 D3.js 沿连续路径进行插值

javascript - React中如何实现D3.selectAll.data.enter.append.transition.style

flutter - 如何防止labelText在TextField中分成两行?

Swift:将变量插入标签?

javascript - 使用javascript获取页面上库的大小

javascript - 使用Javascript编写纯文本?

javascript - 有什么方法可以绕过 Canvas 安全异常(外部图像到数据字符串)