Percent of adults over 25 with at least a bachelor's degree:

Our brains, however, are wired to parse differences in length faster than differences in color. That means you'll figure out a bar chart more quickly than a choropleth map, and you won't be confused by other other signals (area, position or shape, for example).

Charts (bar and otherwise) end up being slightly more complicated to resize than maps, if only because there are more moving parts. In the chart above, I have a top and bottom axis, labels, bars representing data (with another set as background) and ticks representing the median data point. Each of those needs to be adjusted when the screen size changes.

Ultimately, the process is roughly the same as making a responsive map, there's just more stuff to keep track of.

How to make a responsive bar chart:

Size and scale SVG elements based on thier containers.

Reset scales when the window resizes.

Resize all the things.

1. Size and scale SVG elements based on thier containers.

Just as before, start with a responsive framework and set initial sizes based on containers. This lets you (mostly) control your layout with CSS and class names, instead of having to keep CSS and JavaScript in sync.

Do this, and your charts will load with the correct size. Here's a code snippet:

varmargin={top:30,right:10,bottom:30,left:10},width=parseInt(d3.select('#chart').style('width'),10),width=width-margin.left-margin.right,barHeight=20,percent=d3.format('%');// scales and axesvarx=d3.scale.linear().range([0,width]).domain([0,.4]);// hard-coding this because I know the data// ordinal scales are easier for uniform bar heights// I'll set domain and rangeBands after data loadsvary=d3.scale.ordinal();varxAxis=d3.svg.axis().scale(x).tickFormat(percent);

I'm using Mike Bostock's margin conventions; throughout my code, I can use width without further adjustment. I'll set a height variable later, since it's data-dependent and will be a multiple of barHeight.

Reset scales when the window resizes

Spend enough time with D3, and you start to realize that scales are everything. Get your scales right and everything is easier. Misplace a number or get a calculation wrong and your charts fall apart.

Fortunately, this is pretty simple. Again, I'm catching the resize event on window and running a function:

I'm calculating width based on the container element and margins, as I did before, and updating my x-scale's range. After that, I just go down my list above and resize.

First, I'm resizing my svg element. That's a little ugly, because chart is actually a g element translated by margin.left and margin.top, so I need to re-select the parent node. (It would be nice if D3 had a parent accessor, like jQuery.)

Next, I resize the two rect elements. Background is easy; it's a full-width bar (and can be skipped if you don't like having a background offset). For the foreground bar, I'm recalculating width based on the updated x-scale. Simple enough. Rememeber, data is already bound, which helps with next step.

I need to calculate the median again. I could have set this as a global when I loaded data, but I have enough globals in my lifecode. Fortunately, as above, I can grab my dataset out of the chart with chart.selectAll('.bar').data() and calculate a median from that. My median ticks are line elements, so I set the x1 and x2 attibutes again.

Last up: axes. This is even easier. Just select the container elements (in my case, .x.axis.top and .x.axis.bottom) and call the axis object again. Here, I have two version--top and bottom--so I set the orientation each time.

That's it. There are a lot of little steps, but nothing here should be too much trouble, even for more complicated charts.