<?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: Mila Frerichs</title>
    <description>The latest articles on DEV Community by Mila Frerichs (@milafrerichs).</description>
    <link>https://dev.to/milafrerichs</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%2F197726%2Fa4b1baf3-34f5-4620-ad02-9c3c51c9e38b.jpg</url>
      <title>DEV Community: Mila Frerichs</title>
      <link>https://dev.to/milafrerichs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/milafrerichs"/>
    <language>en</language>
    <item>
      <title>Comparison of Mapbox GL JS vs. D3.js</title>
      <dc:creator>Mila Frerichs</dc:creator>
      <pubDate>Tue, 09 Jul 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/milafrerichs/comparison-of-mapbox-gl-js-vs-d3-js-2ood</link>
      <guid>https://dev.to/milafrerichs/comparison-of-mapbox-gl-js-vs-d3-js-2ood</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BEedcr5I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/civicvision/image/upload/f_auto%2Cq_auto%2Cw_auto%2Cdpr_auto%2Cc_limit/geospatial-d3/marketing/website/mapbox-d3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BEedcr5I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/civicvision/image/upload/f_auto%2Cq_auto%2Cw_auto%2Cdpr_auto%2Cc_limit/geospatial-d3/marketing/website/mapbox-d3.png" alt="mapbox left, d3 right"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you want to create an online-map from your data you have a ton of options available.&lt;br&gt;&lt;br&gt;
When I teach d3 or talk to people what tools they’re using the questions that comes up a lot is: &lt;strong&gt;“How do d3 and mapboxGL compare?”.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
That is the question that I want to answer on this site.&lt;br&gt;&lt;br&gt;
I’m not only comparing the two libraries on a conceptiual basis but on a code basis as well.&lt;br&gt;&lt;br&gt;
Let’s start with one of the more confusing aspects:&lt;/p&gt;


&lt;h1&gt;
  
  
  The name
&lt;/h1&gt;

&lt;p&gt;The definition of both libraries are as follow:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mapbox GL JS is a JavaScript library for interactive, customizable vector maps on the web. It takes map styles that conform to the Mapbox Style Specification, applies them to vector tiles that conform to the Mapbox Vector Tile Specification, and renders them using WebGL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mapbox GL JS is part of Mapbox GL which is a cross-platform ecosystem and Mapbox is a company specializing in location data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;D3 (or D3.js) is a JavaScript library for visualizing data using web standards. D3 helps you bring data to life using SVG, Canvas and HTML.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h1&gt;
  
  
  Conceptional
&lt;/h1&gt;

&lt;p&gt;D3.js and Mapbox GL JS (or Mapbox for short for the sake of writing less ;) ) are conceptionally very different. Mapbox is a library to create so-called “Slippy-Maps” and is in good company with Leaflet, OpenLayers and GoogleMaps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Slippy Map is, in general, a term referring to modern web maps which let you zoom and pan around (the map slips around when you drag the mouse).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This quote from OpenStreetMap explains it very well :)&lt;br&gt;&lt;br&gt;
Although using this definition d3 maps can be slippy maps as well. You can add zoom and pan to them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A core component of Slippy Maps is that the images should be served as tiles on a grid. Tiling images is an efficient way to browse large amounts of raster and vector map data that would be much too large to render as a single map image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This explanation from the Planet Developer Resource “Slippy Maps 101” is more technical but brings it more to the point. These maps (used to) load images from a server and tile them to make them faster to load. Nowadays vector tiles have replaced the images, but they still get served as tiles to load and render faster.&lt;/p&gt;


&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;Let’s look at some code. Or to be exact the complete code to render above maps. Mapbox map is on the left. The d3 map on the right.&lt;/p&gt;
&lt;h2&gt;
  
  
  Mapbox
&lt;/h2&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var url = "https://gist.githubusercontent.com/milafrerichs/78ef5702db2dc514fc2bed465d58406b/raw/f1366ee2a83a9afb1dd2427e9cbd4cd3db8d87ca/bundeslaender_simplify200.geojson";
mapboxgl.accessToken = 'YOUR_MAPBOX_TOKEN';
var map = new mapboxgl.Map({
  container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: [13.79,53.545], 
    zoom: 5
  });
  map.on('load', function () {
    map.addSource('bb', { type: 'geojson', data: url });
    map.addLayer({
      'id': 'berlin',
      'type': 'fill',
      'source': 'bb',
      'paint': {
        'fill-color': '#088',
        'fill-opacity': 0.8
      }
    });
    map.addLayer({
      'id': 'berlin-stroke',
      'type': 'line',
      'source': 'bb',
      'paint': {
        'line-width': 1,
        'line-color': '#000',
      }
    });
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;a href="https://codesandbox.io/s/github/milafrerichs/mapping_examples/tree/master/mapbox/simple-map"&gt;Open in CodeSandbox&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  D3
&lt;/h2&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var url = "https://gist.githubusercontent.com/milafrerichs/78ef5702db2dc514fc2bed465d58406b/raw/f1366ee2a83a9afb1dd2427e9cbd4cd3db8d87ca/bundeslaender_simplify200.geojson";
d3.json(url).then(function(bb) {
  var bbox = d3.select('#d3-map').node().getBoundingClientRect()
  var width = bbox.width;
  var height = bbox.height;
  var projection = d3.geoEqualEarth();
  projection.fitExtent([[20, 20], [width, height]], bb);
  var geoGenerator = d3.geoPath().projection(projection);
  var svg = d3.select("#d3-map").append('svg')
      .style("width", "100%")
      .style("height", "100%");
  svg.append('g').selectAll('path')
  .data(bb.features)
  .enter()
    .append('path')
    .attr('d', geoGenerator)
    .attr('fill', '#088')
    .attr('stroke', '#000');
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;a href="https://codesandbox.io/s/github/milafrerichs/mapping_examples/tree/master/d3/simple-map"&gt;Open in CodeSandbox&lt;/a&gt;&lt;/p&gt;


&lt;h1&gt;
  
  
  Amount of Code
&lt;/h1&gt;

&lt;p&gt;The first difference you see is that the code for mapbox is almost twice as long as the d3 code. Which is a bit suprising. But the reason is that the styling takes a lot of effort and is formatted in a readable way.&lt;/p&gt;
&lt;h3&gt;
  
  
  Winner: D3
&lt;/h3&gt;


&lt;h1&gt;
  
  
  Ease of Setup
&lt;/h1&gt;

&lt;p&gt;Both are relatively easy to setup. You either download their code and integrate it. Or use a CDN like I did on this page.&lt;/p&gt;
&lt;h2&gt;
  
  
  Mapbox setup:
&lt;/h2&gt;

&lt;p&gt;You need to include the javascript code and the CSS for mapbox. You need to include the stylesheet to make sure the navigation elements are visible. Furthermore, you need to signup at mapbox.com to get an API token to use it. But that is free, just your email as payment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://unpkg.com/mapbox-gl@0.49.0" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;link href="https://unpkg.com/mapbox-gl@0.49.0/dist/mapbox-gl.css" rel="stylesheet" type="text/css"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  D3 setup:
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://unpkg.com/d3@5.9.7/dist/d3.min.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Winner: D3
&lt;/h3&gt;




&lt;h1&gt;
  
  
  Basemap
&lt;/h1&gt;

&lt;p&gt;The biggest visible difference is the lack of a basemap for d3. But I made this on purpose. The power of d3 is that it does not include a basemap by default. You want the data to be the center of your map, not the basemap.&lt;/p&gt;

&lt;p&gt;This is one of the more important pieces, you choose the libraries based on your needs and d3 and mapbox serve different needs. Mapbox and other basemap-based libraries shine when you need more information than just the data. For example street locations, more labels, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Winner: Mapbox
&lt;/h3&gt;




&lt;h1&gt;
  
  
  Projections
&lt;/h1&gt;

&lt;p&gt;The second big difference is the projection of these maps. Mapbox uses &lt;strong&gt;Mercator&lt;/strong&gt; as their default and you cannot change the projection.&lt;/p&gt;

&lt;p&gt;In D3 you choose the projection yourself. You can pick from a list of 14 different projections or can create your own. &lt;a href="https://github.com/d3/d3-geo-projection"&gt;D3 projection Resource&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var projection = d3.geoEqualEarth();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Winner: D3
&lt;/h3&gt;




&lt;h1&gt;
  
  
  Styling
&lt;/h1&gt;

&lt;p&gt;Mapbox styling is done via &lt;a href="https://docs.mapbox.com/mapbox-gl-js/style-spec/"&gt;Mapbox Style Spec&lt;/a&gt; while D3 styling is done via normal SVG attributes and CSS.&lt;/p&gt;

&lt;p&gt;As you might have noticed we needed to create two layers in mapbox to style the fill as well as the stroke/outline of the feature.&lt;br&gt;&lt;br&gt;
That is one of the many limitations of the style specification. But you can do a lot with the style spec and do not need to think to hard about the implementation.&lt;/p&gt;

&lt;p&gt;In d3 you have the power of SVG and CSS to style almost anything. And it is more convenient. In both cases you need to learn how you style elements.&lt;/p&gt;
&lt;h2&gt;
  
  
  Mapbox:
&lt;/h2&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map.addLayer({
      'id': 'berlin',
      'type': 'fill',
      'source': 'bb',
      'paint': {
        'fill-color': '#088',
        'fill-opacity': 0.8
      }
    });
    map.addLayer({
      'id': 'berlin-stroke',
      'type': 'line',
      'source': 'bb',
      'paint': {
        'line-width': 1,
        'line-color': '#000',
      }
    });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  D3:
&lt;/h2&gt;

&lt;p&gt;In d3 we only need two lines of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.attr('fill', '#088')
  .attr('stroke', '#000');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Winner: D3
&lt;/h3&gt;




&lt;h1&gt;
  
  
  Centering the map
&lt;/h1&gt;

&lt;p&gt;While Mapbox relies on you, the creator, to set the zoom, center and so forth in advance, d3.js allows you to use your data to set the extent.&lt;/p&gt;

&lt;p&gt;You can let mapbox figure out the best zoom and center as well, but it is more difficult and I did not include this currently. I might extent this post in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mapbox
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;center: [13.79,53.545], 
  zoom: 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  D3
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;projection.fitExtent([[20, 20], [width, height]], bb);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Winner: D3
&lt;/h3&gt;




&lt;h1&gt;
  
  
  Concepts you need to learn
&lt;/h1&gt;

&lt;p&gt;There are definetely fewer concepts you need to learn or understand to use mapbox.js. You just need to know that there are layers and sources, call the appropriate functions (look at examples &amp;amp; the docs :) ) and read the mapbox style spec and you’re good to go.&lt;/p&gt;

&lt;p&gt;With d3, you should know the basics of HTML, learn a little bit of SVG and then know which d3 functions to call (look at examples, the docs).&lt;/p&gt;

&lt;h3&gt;
  
  
  Winner: mapbox
&lt;/h3&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;As I mentioned in the beginngin of this post, both d3 and mapbox.js serve somewhat different purposes. But they do have similar features.&lt;/p&gt;

&lt;p&gt;So how do they stack up against each other. Let’s find out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mapbox&lt;/strong&gt; : 2&lt;br&gt;&lt;br&gt;
&lt;strong&gt;D3&lt;/strong&gt; : 5&lt;/p&gt;

&lt;p&gt;I declare D3 as the winner of this comparison. But keep in mind, that if you need a basemap, mapbox is probably easier to setup.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post was first published at &lt;a href="https://mappingwithd3.com/mapbox-d3"&gt;https://mappingwithd3.com/mapbox-d3&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://mappingwithd3.com/?lsc=devto"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g3p4fd1k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/civicvision/image/upload/f_auto%2Cq_auto%2Cw_auto%2Cdpr_auto%2Cc_limit/geospatial-d3/marketing/banner/Learn_how_to_create_maps_with_d3.tiff" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mapbox</category>
      <category>d3js</category>
      <category>visualization</category>
      <category>javascript</category>
    </item>
    <item>
      <title>SVG Basics for d3</title>
      <dc:creator>Mila Frerichs</dc:creator>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <link>https://dev.to/milafrerichs/svg-basics-for-d3-4617</link>
      <guid>https://dev.to/milafrerichs/svg-basics-for-d3-4617</guid>
      <description>&lt;h1&gt;
  
  
  SVG Basics for d3
&lt;/h1&gt;

&lt;p&gt;When you start learning or using d3 you will come across SVGs. That‘s what most people use when they create a visualization with d3. And it‘s the easier choice and the more documented one. So you need to understand a few basic concepts of SVG to make better use of d3 and learn faster.&lt;/p&gt;

&lt;p&gt;In this short post I will show you three elements of SVG that are worth knowing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Groups
&lt;/h2&gt;

&lt;p&gt;You‘ve probably come across a visualization where the person used a &lt;code&gt;g&lt;/code&gt; element within their svg.&lt;br&gt;&lt;br&gt;
The &lt;code&gt;g&lt;/code&gt; stands for group and is an easy way to structure your svg. Especially if you have any kind of interaction or want to update your chart it‘s easier to group your svg elements into groups. You can select them easily and add just one class to the group. It makes it easy to style them as well, when you have groups.&lt;/p&gt;

&lt;h2&gt;
  
  
  The path element
&lt;/h2&gt;

&lt;p&gt;The most common used element with d3 is the path element. It‘s also the most flexible element. You can create almost any shape with it.&lt;br&gt;&lt;br&gt;
We can do that with the &lt;code&gt;d&lt;/code&gt; attribute of the path. The good thing is, you don‘t really need to know how it works. You just need to be aware that it exists and that you will use it with d3 to create custom elements. For mapping we mostly just use the path element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Translation
&lt;/h2&gt;

&lt;p&gt;This is the part where a lot of people struggle with. And I honestly did not tried too hard to understand it for a long time but just used it the way other people were using it.&lt;br&gt;&lt;br&gt;
But it’s actually quite simple. So here is a short explanation of it:&lt;br&gt;&lt;br&gt;
With &lt;code&gt;transform="translate(20,20)"&lt;/code&gt; you‘re moving that element 20 pixel to the right and 20 pixel to the bottom. Why? Because in the browser the coordinate system start in the top left corner of an element.&lt;br&gt;&lt;br&gt;
Why is that useful?&lt;br&gt;&lt;br&gt;
Because now you can move elements that have no x,y attributes. Like a group for example.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this short post.&lt;/p&gt;




&lt;p&gt;This post appeared first on: &lt;a href="https://mappingwithd3.com/resources/d3/svg-basics/"&gt;https://mappingwithd3.com/resources/d3/svg-basics/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>d3js</category>
      <category>svg</category>
    </item>
  </channel>
</rss>
