How to draw the triangle symbol?

后端 未结 1 1646
梦毁少年i
梦毁少年i 2021-01-13 14:28

I am trying to implement the triangle example yet it fails with d3 v4 for me

triangles = [];
triangles.push({x : 250});
triangles.push({x : 350});

var group         


        
相关标签:
1条回答
  • 2021-01-13 14:52

    Instead of...

    type("triangle")
    

    ... it has to be:

    type(d3.symbolTriangle)
    

    Here is the list of the symbols in D3 v4.x.

    And here is the demo:

    var svg = d3.select("svg");
    
    triangles = [];
    triangles.push({
      x: 150
    });
    triangles.push({
      x: 50
    });
    
    var arc = d3.symbol().type(d3.symbolTriangle);
    
    var line = svg.selectAll('path')
      .data(triangles)
      .enter()
      .append('path')
      .attr('d', arc)
      .attr('fill', 'red')
      .attr('stroke', '#000')
      .attr('stroke-width', 1)
      .attr('transform', function(d) {
        return "translate(" + d.x + ",30)";
      });
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <svg></svg>

    The same code, rotating the triangles (as you asked in your comment):

    var svg = d3.select("svg");
    
    triangles = [];
    triangles.push({
      x: 150
    });
    triangles.push({
      x: 50
    });
    
    var arc = d3.symbol().type(d3.symbolTriangle);
    
    var line = svg.selectAll('path')
      .data(triangles)
      .enter()
      .append('path')
      .attr('d', arc)
      .attr('fill', 'red')
      .attr('stroke', '#000')
      .attr('stroke-width', 1)
      .attr('transform', function(d) {
        return "translate(" + d.x + ",30) rotate(180)";
      });
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <svg></svg>

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