D3.js Flashcards
What’s the basic approach to add a bunch of divs based on an array of data?
d3.select(‘body’).selectAll(‘div’).data(someData).enter().append(‘div’)
If you’re adding elements to a selection with append or insert, how can you add a class?
.attr(‘class’, ‘my-class’) or else .attr(‘class’, d => d.className) // if data has className as a property.
If you append an ‘svg’ and then append a ‘rect’, how can you now append a bunch of circles to ‘svg’ so they aren’t placed inside of ‘rect’?
Create a new selection. d3.select(‘svg’).selectAll(‘circle’).data(…
How does d3.scaleLinear() work?
var xScale = d3.scaleLinear().domain( [ dataLow,dataHigh ] ).range( [ renderLow, renderHigh ] )
How could you fill up the horizontal space in a bar chart (assuming vertical space mapped to the data)?
Use d3.scaleBand() along with domain, range, and optional padding.
What ordinal scales does d3 provide?
scaleOrdinal, scaleBand, scalePoint, and category scales (that work with scaleOrdinal for colors)