Livecoding: A choropleth in React.js

#Livecoding: A #choropleth in #Reactjs   by @dzone

  • You’re looking at a choropleth map of median household incomes in the United States that I built with React and D3v4.
  • Buffalo County in South Dakota is the poorest county in the US with a median household income of $21,658.
  • More about that later when we compare this median household data to that dataset of salaries in the software industry.
  • After loading our datasets – a TopoJSON of US counties and states (geo info) and a table of median household incomes per county – we start with a component.
  • It looks like this:

    With some setup and a bit of data loading, those two components create a choropleth map of median household incomes in the United States.

In this tutorial, you’ll learn how to normalize datasets in React.js, by creating a choropleth map of US counties, organized by median income. Read on for more!

@AscentPeople: #Livecoding: A #choropleth in #Reactjs by @dzone

I just spent 3 hours normalizing datasets. Then I wrote a script that did it in less than 5 seconds. I am not a smart man.

Did you know there were 32 states with a Washington county in the US? And there are 24 Jackson counties? 16 Wayne counties… 12 Marshalls…

County names are only unique per-state, not per-all-of-the-country. Now you know. And now I know, too!

Fixing that problem let me turn this picture from my live coding session:

Into this picture:

The latter has less gray and more blue. That’s good. It means there are fewer counties that didn’t match our dataset. Some remain. I don’t know how to fix those.

You’re looking at a choropleth map of median household incomes in the United States that I built with React and D3v4.

Buffalo County in South Dakota is the poorest county in the US with a median household income of $21,658. City of Falls Church County in Virginia is the richest with $125,635. The richest part of the country is about 6x richer than the poorest.

These are medians we’re talking about, not maximums. In both cases, 90% of households fall within a few thousand dollars of the median.

More about that later when we compare this median household data to that dataset of salaries in the software industry. That should be fun.

We cribbed off of Mike Bostock’s choropleth example and modified it for React.

component. It draws the overall map and deals with calculating the quantize threshold scale for colors.

The component is about 50 lines, so I added comments to each method.

I hope that makes sense. It follows my standard React+D3js approach.

For the counties, we can use a stateless functional component that gets all relevant data through props. It looks like this:

With some setup and a bit of data loading, those two components create a choropleth map of median household incomes in the United States. Watch the video to see how it all fits together.

Livecoding: A choropleth in React.js