Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of d3 symbols available to us

Where can I find the list of symbols made available to us by d3.js which is referred by this line of code:

 d3.svg.symbol().type(/*Name of the symbol type that is available to us to use*/'triangle')

Some of the available symbols include triangle, diamond. Is there any documentation available anywhere where the entire list is documented.

like image 217
Cute_Ninja Avatar asked Jul 02 '14 19:07

Cute_Ninja


People also ask

How to adding symbols to the chart in d3 js?

Built-in Symbols To use them, assign them in the . type(symbol) method on the symbol generator: var square = d3. symbol().

What is the correct syntax to draw a circle in d3?

var circle = d3. selectAll("circle"); With a selection, we can make various changes to selected elements.

How do you make a triangle in Javascript d3?

I have the following code that draws a triangle in d3: var trianglePoints = xScale(3) + ' ' + yScale(18) + ', ' + xScale(1) + ' ' + yScale(0) + ', ' + xScale(12) + ' ' + yScale(3) + ' ' + xScale(12) + ', ' + yScale(3) + ' ' + xScale(3) + ' ' + yScale(18); console. log(trianglePoints); svg. append('polyline') .


2 Answers

For version 4, there are seven shapes, as opposed to the six in version 3 (referenced in the other answer).

The shapes are contained in the array d3.symbols which contains:

  • d3.symbolCircle
  • d3.symbolCross
  • d3.symbolDiamond
  • d3.symbolSquare
  • d3.symbolStar (new in version 4)
  • d3.symbolTriangle (there is only one triangle in v 4, compared to 2 in v3)
  • d3.symbolWye (a 'y' shaped symbol, new in version 4)

The d3 documentation as usual covers the topic well here.

To show the symbols, and to show how the array can be used to set shapes dynamically, I've attached a snippet below:

var data = [0,1,2,3,4,5,6];
var svg = d3.select('body').append('svg').attr('width',400).attr('height',200);

svg.selectAll('.symbol')
   .data(data)
   .enter()
   .append('path')
   .attr('transform',function(d,i) { return 'translate('+(i*20+20)+','+30+')';})
   .attr('d', d3.symbol().type( function(d,i) { return d3.symbols[i];}) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.js"></script>
like image 144
Andrew Reid Avatar answered Oct 04 '22 00:10

Andrew Reid


The types supported are listed in the D3 documentation: https://github.com/mbostock/d3/wiki/SVG-Shapes#symbol_type. To quote:

  • circle - a circle.
  • cross - a Greek cross or plus sign.
  • diamond - a rhombus.
  • square - an axis-aligned square.
  • triangle-down - a downward-pointing equilateral triangle.
  • triangle-up - an upward-pointing equilateral triangle.

D3 also has an object that stores the symbols available (thanks again, @jshanley). E.g. for D3 3.4.6:

>>> d3.svg.symbolTypes
["circle", "cross", "diamond", "square", "triangle-down", "triangle-up"]
like image 25
mdml Avatar answered Oct 04 '22 01:10

mdml