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 1

Livecoding: A Map of Global Migrations, Part 1

Follow along this video tutorial, part one of a three part series, to learn how to use React to create a global map with lines representing global migration patterns.

Swizec Teller user avatar by
Swizec Teller
·
Apr. 11, 17 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
4.46K Views

Join the DZone community and get the full member experience.

Join For Free

You know, it's really hard to compete with the Super Bowl. So, when this post came out on Superbowl Sunday, it must have been the lowest live coding turnout I've ever seen.

A cumulative of 80 people came to see me live, and that makes me happy, and I still don't know how liveedu.tv calculates that. It doesn't matter. It's a number, it's not zero, and that's great.

Thanks to mrmaru for teaching me how to jump to the end of line ($) and beginning of line (0) in Vim. I sure feel dumb for holding the left and right arrow keys for the past 15 years like an idiot. Why didn't I look that up?

Together, we talked about not understanding football and built this map:

We're going to make it better next week. I know it doesn't look like much yet.

Our (my?) goal is to build a map of global migrations using the UN dataset I mentioned last week. Initially, I wanted to build one of those cool maps that change country sizes to represent a value. No idea how to do that. Yet.

Instead, we're drawing lines. Because I couldn't figure out how to make curves.

In my defense, I was tired and dying from a cold.

Right now, you're seeing global migrations into Djibouti. Lines connect source migrations to their destination. There's nothing about magnitude yet. That comes next time, and I'd love some suggestions on how to do that. Line thickness seems interesting but would probably look weird.

Oh, and the lines are going to be curved. I think what I'm missing is a third point between the source and destination. Something to give D3's curve curveBasis interpolator a reason to make a curve.

Right now we're rendering the migrations layer as a React component, like this:

const Migrations = ({ topology, projection, data, nameIdMap }) => {
    const countries = topojson.feature(topology, topology.objects.countries),
          path = d3.geoPath(projection),
          centroids = _.fromPairs(countries.features
                                           .map(country => [country.id,
                                                            path.centroid(country)]));


    return (
        <g>
            <CountryMigrations data={data[10]} nameIdMap={nameIdMap}
                               centroids={centroids} />
        </g>
    );
};

const CountryMigrations = ({ data, nameIdMap, centroids }) => {
    const line = d3.line()
                   .curve(d3.curveBasis),
          destination = centroids[data.id];

    console.log(data.name);

    const sources =  Object.keys(data.sources)
                           .filter(name => centroids[nameIdMap[name]])
                           .map(name => centroids[nameIdMap[name]]);
    return (
        <g>
            {sources.map((source, i) => (
                <path d={line([destination, source])}
                      style={{stroke: 'black',
                              strokeWidth: '1px'}}
                      key={`${data.id}-${i}`} />
             ))}
        </g>
    )
};

The Migrations component builds a dictionary of country centroids, which gives us a cached mapping from country id to country centroid position. We use these as line anchors.

Then we iterate through the list of countries and render CountryMigration components. Well, in theory. There's just one CountryMigration right now, so it's easier to debug.

Each CountryMigration component creates a d3.line generator, compiles a list of sources, then iterates through them and adds path elements for each line. As you can see, this works, but it doesn't make curves because a curve between two points is a straight line.

That sounds stupidly obvious when I say it out loud.

Join me next time as we curve the curves, add animation to represent migration volume, and maybe some interactivity. Could mouseovers filter countries? Perhaps.

IT Build (game engine) Joins (concurrency library) Debug (command) React (JavaScript library) Filter (software) Coding (social sciences) Holding (law)

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

  • Making Your Own Express Middleware
  • Legacy Modernization and Hybrid Cloud with Kafka in Healthcare
  • A Simple Guide to Rust Data Types
  • 8 Must-Have Project Reports You Can Use Today

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