请选择 进入手机版 | 继续访问电脑版

技术控

    今日:3| 主题:54663
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Livecoding #24: A choropleth in React.js

[复制链接]
依恋ヽ那段情 发表于 2016-10-4 13:17:30
204 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
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:
  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… :confused:
  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 livecoding:
     

Livecoding #24: A choropleth in React.js

Livecoding #24: A choropleth in React.js-1-技术控-Jackson,pictures,content,problem,seconds
    Into this picture:
     

Livecoding #24: A choropleth in React.js

Livecoding #24: A choropleth in React.js-2-技术控-Jackson,pictures,content,problem,seconds
    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. Richest part of the country is about 6x richer than the poorest. :flushed:
  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 this week when we compare this median household data to that dataset of salaries in the software industry. That should be fun ��
  Here’s how it’s built

   We cribbed off of Mike Bostock’s choropleth example and modified it for React.
   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 CountyMap 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.
   
  1. class CountyMap extends Component {
  2.     // Setup default D3 objects
  3.     // projection - defines our geo projection, how the map looks
  4.     // geoPath - calculates d attribute of <path> so it looks like a map
  5.     // quantize - threshold scale with 9 buckets
  6.     constructor(props) {
  7.         super(props);
  8.         this.projection = d3.geoAlbersUsa()
  9.                             .scale(1280);
  10.         this.geoPath = d3.geoPath()
  11.                          .projection(this.projection);
  12.         this.quantize = d3.scaleQuantize()
  13.                           .range(d3.range(9));
  14.         this.updateD3(props);
  15.     }
  16.     // update D3 objects when props update
  17.     componentWillReceiveProps(newProps) {
  18.         this.updateD3(newProps);
  19.     }
  20.     // Re-center the geo projection
  21.     // Update domain of quantize scale
  22.     updateD3(props) {
  23.         this.projection.translate([props.width / 2, props.height / 2]);
  24.         if (props.medianIncomes) {
  25.             this.quantize.domain([10000, 75000]);
  26.         }
  27.     }
  28.     // If no data, do nothing (we might mount before data loads into props)
  29.     render() {
  30.         if (!this.props.usTopoJson) {
  31.             return null;
  32.         }else{
  33.             // Translate topojson data into geojson data for drawing
  34.             // Prepare a mesh for states and a list of features for counties
  35.             const us = this.props.usTopoJson,
  36.                   statesMesh = topojson.mesh(us, us.objects.states, (a, b) => a !== b),
  37.                   counties = topojson.feature(us, us.objects.counties).features;
  38.             // Loop through counties and draw <County> components
  39.             // Add a single <path> for state borders
  40.             return (
  41.                 <g>
  42.                     {counties.map((feature) => <County geoPath={this.geoPath}
  43.                         feature={feature}
  44.                         key={feature.id}
  45.                         quantize={this.quantize}
  46.                         data={_.find(this.props.medianIncomes, {countyId: feature.id})} />)}
  47.                      <path d={this.geoPath(statesMesh)} style={{fill: 'none',
  48.                              stroke: '#fff',
  49.                              strokeLinejoin: 'round'}} />
  50.                 </g>
  51.             );
  52.         }
  53.     }
  54. }
复制代码
    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:
   
  1. // Combine array of colors and quantize scale to pick fill colo
  2. // Return a <path> element
  3. const County = ({ data, geoPath, feature, quantize }) => {
  4.     let color = BlankColor;
  5.     if (data) {
  6.         color = ChoroplethColors[quantize(data.medianIncome)];
  7.     }
  8.     return (<path d={geoPath(feature)} style={{fill: color}} title={feature.id} />)
  9. };
复制代码
    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.
   P.S.: the edited and improved versions of these videos are becoming a video course. Readers of the engineer package ofReact+d3js ES6 get the video course for free when it’s ready.



上一篇:Editorial: How Do You Keep Your Skill Set Relevant?
下一篇:Using Subtests and Sub-benchmarks
271277786 发表于 2016-10-5 12:48:07
妈的,我被人投诉了!客户说我给他的mp3文件没有图像!
回复 支持 反对

使用道具 举报

萨博 发表于 2016-10-12 10:15:11
你女儿在我手上,我不是你女婿。
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表