简体   繁体   English

D3 V4 旭日图布局弧度计算

[英]D3 V4 Sunburst diagram layout arc calculation

This is the code to compute the coordinates of the sunburst nodes:这是计算森伯斯特节点坐标的代码:

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); });

Where:哪里:

x: the minimum x-coordinate of the node position x:节点位置的最小 x 坐标

y: the minimum y-coordinate of the node position y:节点位置的最小y坐标

dx: the x-extent of the node position dx:节点位置的 x 范围

dy: the y-extent of the node position dy: 节点位置的 y 范围


However, in the recently realeased version v4, the space-filling layouts d3.treemap and d3.partition now output x0, x1, y0, y1 on each node instead of x0, dx, y0, dy然而,在最近发布的 v4 版本中,空间填充布局 d3.treemap 和 d3.partition 现在在每个节点上输出 x0, x1, y0, y1 而不是 x0, dx, y0, dy

node.x0 - the left edge of the rectangle node.x0 - 矩形的左边缘

node.y0 - the top edge of the rectangle node.y0 - 矩形的上边缘

node.x1 - the right edge of the rectangle node.x1 - 矩形的右边缘

node.y1 - the bottom edge of the rectangle node.y1 - 矩形的底边

What would be the correspinding code for v4 as the following does not produce the correct layout? v4 的相应代码是什么,因为以下不会产生正确的布局?

var arc = d3.arc()
    .startAngle(function(d) { return d.x0; })
    .endAngle(function(d) { return d.x0 + (d.x1 - d.x0); })
    .innerRadius(function(d) { return d.y0; })
    .outerRadius(function(d) { return d.y0 + (d.y1 - d.y0); });

See codepen代码笔

在此处输入图片说明

See codepen代码笔

var data = {...}

var width = 960;
var height = 700;
var radius = Math.min(width, height) / 2;
var color = d3.scaleOrdinal(d3.schemeCategory20c)

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

var partition = d3.partition()
    .size([360, radius])
    .padding(0)
    //.round(true); //this will produce weird leaf node size if true

var root = d3.hierarchy(data, function(d) { return d.children })
    .sum( function(d) {             
        if(d.children) {
            return 0
        } else {                        
            return 1
        }
     })
    .sort(null);

partition(root)

var xScale = d3.scaleLinear()
    .domain([0, radius])
    .range([0, Math.PI * 2])
    .clamp(true);

var arc = d3.arc()
    .startAngle(function(d) { return xScale(d.x0) })
    .endAngle(function(d) { return xScale(d.x1) })
    .innerRadius(function(d) { return d.y0 })
    .outerRadius(function(d) { return d.y1 })

var path = g.selectAll('path')
    .data(root.descendants())
    .enter().append('path')
        .attr("display", function(d) { return d.depth ? null : "none"; })
        .attr("d", arc)
        .attr("fill-rule", "evenodd")
        .style('stroke', '#fff')
        .style("fill", function(d) { return color((d.children ? d : d.parent).data.name); })

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

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