简体   繁体   English

计算D3中的弧长

[英]Calculating a the length of an arc in D3

I am working with a sunburst graph in D3 and need to calculated the width of any given segment. 我正在使用D3中的森伯斯特图,需要计算任何给定线段的宽度。 In the below diagram how can I get the actual width in pixels of the line AB for Arc1? 在下图中,如何获取Arc1的AB线的实际宽度(以像素为单位)?

在此处输入图片说明

Presumably if I know the innerRadius for an arc (which will be the same length for A and B) I can plot an imaginary triangle and use the Law of Cosines 如果我知道一个圆弧的innerRadius (对于A和B的长度相同),我可以绘制一个虚构的三角形并使用余弦定律

c2 = a2 + b2 - 2ab cos C c2 = a2 + b2-2ab cos C

But I'd need the angle C . 但是我需要角度C This be retrieved in D3 by getting the difference in start and end angles. 通过获取起始角度和终止角度的差值,可以在D3中进行检索。

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

var y = d3.scaleLinear()
    .range([0, this.radius])
    .clamp(true);

var.arc = d3.arc()
    .startAngle(function(d) {
        return x(d.x0);
    })
    .endAngle(function(d) {
        return x(d.x1);
    });

function getRingRadius() {
    // for example
    return 50;
}

function measureSegment(d) {
    var a = getRingRadius() * (d.depth || 0), b = a;
    var C = arc.startAngle()(d) - arc.endAngle()(d);

    return Math.sqrt((a*a + b*b) - 2*a*b*Math.cos(C));
}

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

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