技术控

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

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

[复制链接]
依恋ヽ那段情 发表于 2016-10-4 13:17:30
132 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-1 (Jackson,pictures,content,problem,seconds)
    Into this picture:

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.
     [code]class CountyMap extends Component {
    // Setup default D3 objects
    // projection - defines our geo projection, how the map looks
    // geoPath - calculates d attribute of so it looks like a map
    // quantize - threshold scale with 9 buckets
    constructor(props) {
        super(props);

        this.projection = d3.geoAlbersUsa()
                            .scale(1280);
        this.geoPath = d3.geoPath()
                         .projection(this.projection);
        this.quantize = d3.scaleQuantize()
                          .range(d3.range(9));

        this.updateD3(props);
    }

    // update D3 objects when props update
    componentWillReceiveProps(newProps) {
        this.updateD3(newProps);
    }

    // Re-center the geo projection
    // Update domain of quantize scale
    updateD3(props) {
        this.projection.translate([props.width / 2, props.height / 2]);

        if (props.medianIncomes) {
            this.quantize.domain([10000, 75000]);
        }
    }

    // If no data, do nothing (we might mount before data loads into props)
    render() {
        if (!this.props.usTopoJson) {
            return null;
        }else{
            // Translate topojson data into geojson data for drawing
            // Prepare a mesh for states and a list of features for counties
            const us = this.props.usTopoJson,
                  statesMesh = topojson.mesh(us, us.objects.states, (a, b) => a !== b),
                  counties = topojson.feature(us, us.objects.counties).features;

            // Loop through counties and draw components
            // Add a single for state borders
            return (
               
                    {counties.map((feature) =>                         feature={feature}
                        key={feature.id}
                        quantize={this.quantize}
                        data={_.find(this.props.medianIncomes, {countyId: feature.id})} />)}

                                                   stroke: '#fff',
                             strokeLinejoin: 'round'}} />
               

            );
        }
    }
}[/code]     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:
     [code]// Combine array of colors and quantize scale to pick fill colo
// Return a element
const County = ({ data, geoPath, feature, quantize }) => {
    let color = BlankColor;

    if (data) {
        color = ChoroplethColors[quantize(data.medianIncome)];
    }

    return ()
};[/code]     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
你女儿在我手上,我不是你女婿。
回复 支持 反对

使用道具 举报

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

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

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

返回顶部 返回列表