Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw the triangle symbol?

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 = svgContainer.append('g').attr('transform','translate('+ xOffset +','+ 30 +')');
var arc = d3.symbol().type('triangle');

var line = group.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)+",0)"; }); 

The error:

Uncaught TypeError: type.apply(...).draw is not a function

like image 870
El Dude Avatar asked Jan 04 '23 06:01

El Dude


1 Answers

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>
like image 121
Gerardo Furtado Avatar answered Jan 22 '23 09:01

Gerardo Furtado