Javascript 在 d3.js 中制作弧线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12717652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:06:09  来源:igfitidea点击:

making an arc in d3.js

javascriptcanvassvgd3.jsdata-visualization

提问by mheavers

I am using the javascript library d3.js (http://d3js.org/) to create canvas data visualizations. I'm trying to make an arc, but it's not accepting the data parameters from my array. Does anyone know what I'm doing wrong? This is my code:

我正在使用 javascript 库 d3.js ( http://d3js.org/) 创建画布数据可视化。我正在尝试创建一个圆弧,但它不接受来自我的数组的数据参数。有谁知道我做错了什么?这是我的代码:

var chartConfig = { "canvasSize" : 800 }

var radius = chartConfig.canvasSize / 2;
var pi = Math.PI;

var vis = d3.select("#chart").append("svg")
    .attr("width", radius * 2)
    .attr("height", radius * 2)
    .append("g")
    .attr("transform", "translate(" + radius + "," + radius + ")");

var arcData = [
  {aS: 0, aE: 45,rI:radius/2,rO:radius}
];
var arc = vis.selectAll("arc").data(arcData).enter().append("arc");
arc.attr("innerRadius", function(d){d.rI}).attr("outerRadius",function(d){d.rO}).attr("class","arc");

function degToRad(degrees){
  return degrees * (pi/180);
}

enter image description here

在此处输入图片说明

回答by Bill

There is no arcelement in SVG, you need to define the appropriate path element. Luckily there is a d3helper function to do this.

arcSVG 中没有元素,需要定义合适的路径元素。幸运的是,有一个d3辅助函数可以做到这一点。

var arc = d3.svg.arc()
    .innerRadius(50)
    .outerRadius(70)
    .startAngle(45 * (Math.PI/180)) //converting from degs to radians
    .endAngle(3) //just radians

vis.append("path")
    .attr("d", arc)
    .attr("transform", "translate(200,200)")

Working example at http://jsfiddle.net/g0r9n090/;

http://jsfiddle.net/g0r9n090/ 上的工作示例;