Fundamentals of Rendering Data as an SVG Bar Graph with D3, scaleBand, and scaleLinear

InstructorJason Brown

Share this video with your friends

Send Tweet

In this lesson we'll start by using d3-scale to build up a scaleBand for our x data and a scaleLinear for our y data points to allow them to map and extrapolate our data correctly to renderable bars. We'll add in padding between each bar, and then using the svg rect will render a collection of bars.

Cory Armbrecht
~ 6 years ago

Could you tell me the reason for the xSelector (and also ySelector) in this example? I don't understand why you would need to have a selector wrapping the datum that you're going to access anyways:

d => d.x vs d => xSelector(d)

Within xScale(), I don't understand scaleBand().domain(). Shouldn't domain be the length of an array? The x position of the <rect> I would think would be determined by i or data.length, not by a set of numbers like yScale (the numbers of d.y correlate to the heights of the <rect>), but index determines x position or offset, otherwise they wouldn't be spaced evenly. Thanks for any insight here.