<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luca Faggianelli</title>
    <description>The latest articles on DEV Community by Luca Faggianelli (@lucafaggianelli).</description>
    <link>https://dev.to/lucafaggianelli</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F465636%2Fe6bd266b-0822-4c05-982f-4f6c0396df5f.jpg</url>
      <title>DEV Community: Luca Faggianelli</title>
      <link>https://dev.to/lucafaggianelli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucafaggianelli"/>
    <language>en</language>
    <item>
      <title>Interactive SVG + JS map</title>
      <dc:creator>Luca Faggianelli</dc:creator>
      <pubDate>Wed, 25 Nov 2020 18:15:15 +0000</pubDate>
      <link>https://dev.to/lucafaggianelli/interactive-svg-js-map-2ind</link>
      <guid>https://dev.to/lucafaggianelli/interactive-svg-js-map-2ind</guid>
      <description>&lt;p&gt;Now and then, on several web applications projects, I need to use maps to display data, sometimes it's a world map, sometimes a country map with regions and provinces, often I need a &lt;em&gt;click-on-region&lt;/em&gt; event handler and I always need to display some data on the map as colors or numbers. You know what I'm talking about right? Not a Google Maps map, but a simple and plain map with country borders.&lt;/p&gt;

&lt;p&gt;Of course there are libraries for this purpose, also free ones, but not so many and they're pretty heavy as they bring a lot of functionalities, whereas I just need a simple map, so I like to make my own map, also because it's fun 🙂. Let's make it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Get started
&lt;/h2&gt;

&lt;p&gt;The first step is to find a map in SVG format, I typically use &lt;a href="https://simplemaps.com/resources/svg-maps"&gt;simplemaps&lt;/a&gt; they have world and regional maps and they're free to use (check the license for details).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When I speak of &lt;em&gt;regions&lt;/em&gt; I refer to geographical regions, that is any of country, region, province, state, area, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we're ready to go, these are the features we're going to implement at the end of the article:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Load the SVG map and render it into the HTML page&lt;/li&gt;
&lt;li&gt;Color the map regions based on supplied data&lt;/li&gt;
&lt;li&gt;Attach mouse events listeners (for click and hover) to regions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So let's start with step 1 and part of 2:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/lucafaggianelli/embed/pobYPLq?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Yeah so easy! I only used a tiny library for SVG manipulation called &lt;a href="https://svgjs.com/docs/3.0/"&gt;Svg.js&lt;/a&gt;, even if it's not mandatory, it makes life a bit easier.&lt;br&gt;
In the codepen above we load the map SVG file content with the &lt;code&gt;fetch()&lt;/code&gt; method and we render it into the HTML, then we manipulate the map adding labels and changing the colors just for &lt;em&gt;demo&lt;/em&gt; purpose.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please note&lt;/em&gt; that I'm selecting the map regions with &lt;code&gt;draw.find('path')&lt;/code&gt; but if you use another map, the regions may be represented by &lt;code&gt;&amp;lt;g&amp;gt;&lt;/code&gt; elements or may be inside nested into other elements.&lt;/p&gt;

&lt;p&gt;OK let's make this thing a bit more useful, I found Belgian population data (2019) on Wikipedia and we're going to plot it on our map, plus we'll make it interactive!&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/lucafaggianelli/embed/GRjKxjJ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Better? Now we have real data and each region's color represent its population, the colors are shades between green and white, whereas the most populated region (Antwerp province) has a mix of 100% of green and 0% of white, while the least populated (Luxembourg province) has more white than green: this is called a heatmap. As for the interaction, if you hover a region, you get a popup and the region color changes. The color effect is done with CSS to demonstrate that you can style an SVG element (and its children) with plain CSS.&lt;/p&gt;

&lt;p&gt;This is pretty basic, but it's good starting point for creating a fully featured map.&lt;br&gt;
I'm going to extend this demo with more features and I'm going to package it into an NPM library, so stay tuned!&lt;/p&gt;

&lt;p&gt;Well now I'm curious, do you use maps and for what? Do you use them in apps you develop or in blog post or whatever? What library do you use?&lt;br&gt;
Questions and feedbacks are highly appreciated!&lt;/p&gt;

&lt;p&gt;Luca&lt;/p&gt;

</description>
      <category>svg</category>
      <category>map</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
