简体   繁体   English

可缩放的朝阳图,以百分比为单位

[英]Zoomable sunburst chart in percentages

I'm new to D3 and I hope you can help out. 我是D3的新手,希望您能为您提供帮助。 I'm working on a sunburst chart divided into inner and outer layers, where inner layer represents a group and outer layers represent subgroups. 我正在研究一个分为内层和外层的森伯斯特图,其中内层代表一个组,外层代表子组。 Here's a working example for reference: jsfiddle.net/9gpL308y/1/ (and here's the original fiddle I used as a starting point: jsfiddle.net/j9WnB/64/) 这是一个可供参考的工作示例:jsfiddle.net/9gpL308y/1/(这是我用作起点的原始提琴:jsfiddle.net/j9WnB/64/)

Currently, each inner layer adds up to a number based on its categories' values in the outer layer and categories behave the same with their subcategories. 当前,每个内层根据外层中类别的值累加一个数字,并且类别与其子类别的行为相同。 What I need is for each layer to display as percentage (out of 100 that all groups on the same layer should add up to) and scale to appropriate size. 我需要的是将每个图层显示为百分比(同一图层上所有组应累加的100个百分比)并缩放到适当的大小。 Take this image for example: 以这张图片为例:

image of the wanted chart 所需图表的图像

Working example would be extremely helpful. 工作示例将非常有帮助。 I found some topics on this problem but I couldn't get it to work with provided advice. 我找到了有关此问题的一些主题,但无法通过提供的建议使它起作用。 To be honest I still don't understand fully what's going on in here (I was never good at geometry). 老实说,我仍然不完全了解这里发生的事情(我从不擅长几何)。

tl;dr: How to make sunburst chart from the fiddle above display data as percentage and have arcs scale appropriately? tl; dr:如何从上方的小提琴中制作出朝阳图,以百分比显示数据并适当地缩放弧度?

Thanks. 谢谢。

There is a little to do with geometry in this case (except positioning your label, but this is what arc.centroid provides). 在这种情况下,与几何关系不大(定位标签除外,但这就是arc.centroid提供的功能)。 To get your percentages you just need to divide an extent of the child node by the extent of its parent. 要获得百分比,您只需要将子节点的范围除以其父节点的范围即可。

                var center = arc.centroid(d);
                g.append("text")
                 .attr("text-anchor", "middle")
                 .attr("transform", "translate(" + center + ")")
                 .text(function(_) {
                    if (d.parent == null)
                    {
                        return;
                    }
                    var percentage = 100 * d.dx / d.parent.dx; // that's it!
                    return d3.format(",.2f")(percentage) + "%";
                 })

Working example here: http://jsfiddle.net/9gpL308y/2/ 此处的工作示例: http : //jsfiddle.net/9gpL308y/2/

For educational purposes you can dump all the nodes of the partition layout and then find how do they match visual picture. 出于教育目的,您可以转储分区布局的所有节点,然后查找它们如何与视觉图片匹配。 You can also read about those parent , dx , etc. at the documentation on partition layout: https://github.com/d3/d3-3.x-api-reference/blob/master/Partition-Layout.md 您还可以在有关分区布局的文档中阅读有关这些parentdx等的信息: https : //github.com/d3/d3-3.x-api-reference/blob/master/Partition-Layout.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM