How to create a choropleth Map or Geographic HeatMap in React
Tags: javascript, react, heatmap, choropleth
Published: 2018-12-15 17:54:55

---
title: How to create a choropleth Map or Geographic HeatMap in React
published: true
description: 
tags: JavaScript, React, Heatmap, Choropleth
---
<img src="https://thepracticaldev.s3.amazonaws.com/i/mpqts5lv196bfx8zj08a.png" />A Choropleth Map is a thematic map in which areas are shaded or patterned in proportion to the measurement of the statistical variable being displayed on the map, such as population density or per-capita income.In this guide we’ll be creating a Choropleth map of Canada, But it can be used as a guide to create a similar map of any country. We’ll be using Datamap for this purpose. I am assuming that you are familiar with React.<ol><li><h4>Create a new project using create-react-app</h4> <br /><code>$ create-react-app map-example</code><p>Go inside the project folder and start the development server -</p><code>$ cd map-example<br/> $ yarn start </code><br /></li><li><h4>Install datamaps using yarn or npm</h4><p>Datamaps is intended to provide some data visualizations based on geographical data. It’s SVG-based, can scale to any screen size, and includes everything inside of 1 script file. It heavily relies on the amazing D3.js library.</p><code>$ yarn add datamaps</code></li><li><h4>Get the topoJson file of the country for which the map has to be created</h4><p>The topoJson file contains the geometric data to plot the map of the country. To get the TopoJson file of a specific country, follow these steps:</p><ul><li> Go to https://github.com/markmarkoh/datamaps/tree/master/dist</li><li>Find the datamaps.{xyz}.js file for the country xyz. xyz is the 3 character code for any country. Since we are creating a map for Canada, we’ll find the file named datamaps.can.js in the above repository (“can” is the 3 character code for Canada).</li><li> Copy the contents of the file and paste it in the browser console (You can open the console by pressing F12 in the browser).</li><li><p> Execute the following code after running the above code in the browser.
</p><p> <code>copy(Datamap.prototype.canTopo);</code> </p><p> It will copy the data returned by Datamap.prototype.canTopo function to the clipboard. (You can replace “can” with any other country code if you are creating the map for another country).</li><li> Create a new file named Canada.topo.json inside the src/components directory in the project folder.</li><li> Paste the contents copied from the browser console.<br /> If the state codes contains dot(.) in the topo json, then you need to remove the dot from the code e.g, if your state code is CA.AL, remove CA. part to get 2-digit ISO code AL. If the states code are already in 2-digit ISO or do’t have dot(.) then don’t do any modification follow next steps. Objects country name in {xyz}.topo.json should be same as we have declared in the Datamap scope. e.g, for Canada, in canada.topo.json we have <code>{“type”:”Topology”, <br /> “objects”:{“can”:{“type”:”GeometryCollection”}}}</code><p> and we have provided scope as ‘canada’ in the map component in next step. So this case ‘can’ in canada.topo.json must be as ‘canada’ i.e.</p> <code> {“type”:”Topology”,<br/> “objects”:{“canada”:{“type”:”GeometryCollection”}}} </code></li></ul></li><li><h4> Create the ChoroplethMap component</h4><p> This will be the component which will render the choropleth map (or geographic heatmap) in the DOM. <br /> Create a new file named ChoroplethMap.js inside src/components directory in the project folder. <br /> The contents of the file should be as below -</p> 
    {% gist https://gist.github.com/WebDevRaj/3f3561844ee9858d45019c956edea5f2 %}
<p> Now, we’ll walk through the code above - Starting with the usual react stuff, we have imported all the required packages as well as the topo json fiile.</p><p> Inside the componentDidMount lifecycle method, we have configured and rendered the choropleth Choropleth map using datemaps and d3.</p><p> We have transformed the data coming as the props to the format that Datamaps expects it to be in by finding the minimum and the maximum value from the data and then generating the colour palette using d3 scale. After that, we have created the map using new Datamap() and passing all the configurations and the data to plot the map.</p><p> Note that we have overridden the setProjection method to define the properties of the map like the center and the scale.The center of the map varies according to the country.</p><p> We have set [-106.3468, 68.1304] to locate center point for Canada in the world map. That means Latitude = -106.3468 E and Longitude = 68.1304 N. Remember Latitude and Longitude are always East and North. For western countries, Latitude are in West so make it convert as Negative of East. e.g 102.3421 W ==> -102.3421 E.</p><p> After that, we have rendered a div with the id=”choropleth_map”, which is used by Datamap to render the map.</p></li><li><h4> Import and render the ChoroplethMap component in App.js</h4><p>Now inside App.js component, we have to import the ChoroplethMap component that we created in the above steps and render it inside a div so that the App component looks like below -</p> {% gist https://gist.github.com/WebDevRaj/81da1318570dd465cae68b8980f7c6a3 %} <p> We have defined some sample data in the state and passed it to the map component as props. Also, we have set the height and width of the container div through inline styles. This is important as the map component has the height and width set to 100% of the parent element.</p><p>Now run <code>$ react start</code> in the terminal and test the app.</p><p>You can clone and test the project from the following repository on github -</p> {% github https://github.com/WebDevRaj/choropleth-map %}</li></ol>References : 
<ul>
    <li> <a href="https://www.npmjs.com/package/datamaps">npm/datamaps</a></li>
 <li><a href="https://www.wikipedia.org/">Wikipedia</a></li> <p>Originally published on medium - <a href="https://medium.com/@WebDevRaj/how-to-create-a-choropleth-map-or-a-geographic-heatmap-in-react-9965ddf7a87f"> How to create a choropleth Map or a Geographic HeatMap in React </a></p>