DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Livecoding: A Map of Global Migrations, Part 2

Livecoding: A Map of Global Migrations, Part 2

In this post, we continue to build our migration patterns map using some basic geometry and a little code to turn our lines into curves.

Swizec Teller user avatar by
Swizec Teller
·
Apr. 12, 17 · Web Dev Zone · Tutorial
Like (0)
Save
Tweet
3.22K Views

Join the DZone community and get the full member experience.

Join For Free

In our last post, we built a colorful map and discovered that a marathon is 138,435 feet long. I don't know why Google thinks that's a useful answer to "How long is a marathon?”

Our map shows migrations into the selected country and uses color to show migration magnitude. The more people that moved into a country in 2015, the warmer the color of that country and its curve. Or rather, the more to the right on the d3.interpolateWarm color scale.

I wouldn't describe green as warmer than purple, to be honest, I'm not sure what exactly interpolateWarm is meant to correspond to. But the way we used it, to the right is more and to the left is less. Shrug.

Guess we'll have to add a legend next week.

But first, the curves. Last week, we discovered that a curve between two points is a straight line. We fixed that by adding a middle point calculated using high school geometry.

Our new Curve component looks like this:

const Curve = ({ start, end, color }) => {
    const line = d3.line()
                   .curve(d3.curveBasis),
          [x1, y1] = start,
          [x2, y2] = end,
          middle = [(x1 + x2)/2, (y1 + y2)/2-200];

    return (
        <path d={line([start, middle, end])}
              style={{stroke: color,
                      strokeWidth: '1.6px',
                      strokeOpacity: '0.7',
                      fillOpacity: 0}} />
    );
};

A line generator with a curve interpolator, some geometry for the middle point pulled up by -200 pixels, and returns a <path>element of a given color, and some other styling. We got the -200number by fiddling around until it looked good.

That and some looping created a map with very many curves all over it. It was not very useful.

nikivansevdon suggested we add some sort of filtering, and daemon92 had great ideas around colors and highlighting source countries on the map itself. Thanks guys!

So we used react-select to make a drop down and spent far too much time rejigging our calculations to make the map aware of them. This is where having Redux or MobX becomes useful: when you want to move things around.

With those, you have data and calculations in a global state somewhere. With our haphazard approach, most logic gets slapped into the nearest component that uses it. It works great until you want to move something, then you're in a world of pain.

Watch the stream recording from about 1 hour and 20 minutes onwards. You'll see what I mean. Copy-pasting. So much copy-pasting.

You can see the final code on Github.

It came out quite well I think.

Next week, we'll add useful text info, a color legend, and add some animation to make the visualization quicker to understand. I want to add something that shows directionality for those curves.

If we can get special regions like World, Developed countries, etc. to work, that would be great too.

Visualization (graphics) Data (computing) GitHub Google (verb) Sort (Unix) Drops (app) POST (HTTP) Stream (computing)

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Complete Guide to Generated Columns in MySQL
  • How Does the Internet Speed Test Work?
  • Python Class Attribute: Class Attribute vs. Instance Attribute
  • Machine Learning in Cybersecurity

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo