How to draw a *simple* line segment with d3.js?

后端 未结 1 1797
无人及你
无人及你 2021-01-12 08:47

In the documentation for d3.js I cannot find a straightforward way to draw a simple line segment between two points. The only way I can find there to do this is on

相关标签:
1条回答
  • 2021-01-12 09:29

    Simplest is:

    d3.select('svg')
      .append('path')
      .attr({
        d: "M0,0L200,200"
        stroke: '#000'
      });
    

    This is not too bad:

    var simpleLine = d3.svg.line()
    d3.select('svg')
      .append('path')
      .attr({
        d: simpleLine([[0,0],[200,200]]),
        stroke: '#000'
      });
    

    Still....

    Dunno if this is simpler, but it's maybe more direct:

    d3.select('svg')
      .append('line')
      .attr({
        x1: 0,
        y1: 0,
        x2: 200,
        y2: 200,
        stroke: '#000'
      })
    

    (All three examples draw a line from 0,0 to 200,200)

    0 讨论(0)
提交回复
热议问题