未加星标

Livecoding #33: A Map of Global Migrations, Part 2

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

This is a Livecoding Recap an almost-weekly post about interesting things discovered while livecoding :movie_camera:. Always under 500 words and with pictures. You can follow my channel, here . New content almost every Sunday at 2pm PDT . There’s live chat, come say hai :wink:

This Sunday, 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?” .


Livecoding #33: A Map of Global Migrations, Part 2

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.


Livecoding #33: A Map of Global Migrations, Part 2

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 .


Livecoding #33: A Map of Global Migrations, Part 2

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 -200 number 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.


Livecoding #33: A Map of Global Migrations, Part 2

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 1h20min onwards. You'll see what I mean. Copy-pasting. So much copy-pasting :grinning:

You can see the final code on Github .

It came out quite well I think.


Livecoding #33: A Map of Global Migrations, Part 2

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.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: GitGuess
分页:12
转载请注明
本文标题:Livecoding #33: A Map of Global Migrations, Part 2
本站链接:http://www.codesec.net/view/533319.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(24)