Welcome to d3aday! If this is your first time, we recommend reading the about page before jumping in.
The Inspiration
The inspiration for this exercise was the stacked bar chart showing Democratic primary candidate endorsements on Fivethirtyeight.com
I like this chart a lot. It’s intuitive, clear, gets the big picture across at a glance, but allows for drilling down into the details as well. Also, floating heads are always hilarious.
The Exercise
Stacked bar charts (and similar things like streamgraphs) can be a bit of a challenge. The D3.js API for stacks took me some time to wrap my head around. But it’s definitely doable.
In addition to the official D3.js documentation on stacks, I found Bostock’s Tidy Stacked Area Chart example helpful as well.
For this exercise, we’ll use some data I generated on high calorie foods. Each food has a breakdown of macronutrients, and the chart should show total calories as well. The data can be found here.
I lifted the color pallette directly from the inspiration chart. Hey, it’s a great pallette.
A Solution
My solution looks like this:
Lentil soup - the next avocado?
The code is below, but I encourage you to try your hand at this exercise on your own. Complete code and working example (with tooltip rollovers for the stacks) can be found on codepen.