简体   繁体   English

如何在给定的地图投影中执行d3.svg.arc?

[英]How can I do d3.svg.arc within a given map projection?

Having troubles finding information to draw a circular sector on a given map projection in D3.js. 在D3.js中无法找到信息以在给定的地图投影上绘制圆形扇形。 I have been using d3.svg.arc , but now I need to transform it to given d3.geo projections. 我一直在使用d3.svg.arc ,但是现在我需要将其转换为给定的d3.geo投影。

Note, this is not for drawing arcs between two points, but circular sectors (think pie wedges). 请注意,这不是在两点之间绘制圆弧,而是用于绘制圆形扇区(请考虑使用扇形楔形)。

You can create a function that calculates the coordinates along the desired arc in spherical coordinates (making use of the d3.geo.rotation function to save yourself the headache of figuring out the correct equations) and returns a LineString object: 您可以创建一个函数来计算沿球面坐标系中所需弧线的坐标(利用d3.geo.rotation函数来避免自己找出正确方程式的麻烦)并返回LineString对象:

function geoArc(center, radius, startAngle, endAngle, steps) {
    coordinates = []
    for (var i = 0; i <= steps; i++) {
        var curAngle = (startAngle + (endAngle - startAngle) * i / steps);
        coordinates[i] = d3.geo.rotation(center)(d3.geo.rotation([0, 0, curAngle])([radius, 0]))
    }
    return {
        type: "LineString",
        coordinates: coordinates
    };
}

This can then be used to create the desired arc like this: 然后可以使用它来创建所需的弧,如下所示:

svg.append("path")
    .datum(geoArc([20, 40], 30, -20, 230, 40))
    .classed("circle", true)
    .attr("d", path);

This generates an arc centered at 20°E, 40°N with a radius of 30°, running from -20° to 230°: 这将产生一个以20°E,40°N为中心,半径为30°的弧,其范围为-20°至230°:

Fiddle 小提琴

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

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