Very Simple D3: How to Draw an Arc?

2020-03-18 09:02发布

It would be nice to learn D3. After reading many examples, I think I understand it. My first project is to make a color wheel, without transitions for simplicity. But it appears even that is not simple enough for my first project! For project zero, I am trying to get something to show on the screen. Hopefully something I wrote (and dear read has fixed), and not an example.

What did I do wrong? http://jsfiddle.net/aGdMX/1/

var arc = d3.svg.arc()
    .innerRadius(40)
    .outerRadius(100)
    .startAngle(0)
    .endAngle(1)
    ;

var chart = d3.select("body").append("svg:svg")
    .attr("class", "chart")
    .attr("width", 420)
    .attr("height", 420).append("svg:g")
    .attr("transform", "translate(200,200)")
    ;

chart.selectAll("path")
    .data(data)
    .enter().append("svg:path")
    .attr("fill", function(d, i){
        return d3.rgb("black");
    })
    .attr("d", arc)
    ;

Thank you

1条回答
趁早两清
2楼-- · 2020-03-18 09:47

Your example here doesn't have any data defined. If you just want to draw the svg statically, skip the selectAll() and data() bindings:

chart
    .append("svg:path")
    .attr("fill", function(d, i){
        return d3.rgb("black");
    })
    .attr("d", arc)
    ;

Or define some data and use that to drive the drawing:

http://jsfiddle.net/findango/aGdMX/2/

(plus .attr("fill"... should be .style("fill"...)

查看更多
登录 后发表回答