Friday, September 5, 2025
HomeLanguagesJavascriptD3.js geoMercator() Function

D3.js geoMercator() Function

D3.js is a JavaScript library for producing dynamic, interactive data visualizations in web browsers. It makes use of Scalable Vector Graphics, HTML5, and Cascading Style Sheets standards. The geoMercator() function in d3.js is used to draw The spherical Mercator projection.

Syntax:

d3.geoMercator()

Parameters: This method does not accept any parameters.

Returns: This method creates the Mercator projection from given json data.

Example #1: The following example draws Mercator projection of world with center at (0, 0) and 0 rotation.




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            content="width=device-width, 
                initial-scale=1.0"
        />
    </head>
  
    <body>
        <div style="width: 700px; height: 500px;">
            <center>
                <h3 style="color: black;"></h3>
            </center>
  
            <svg width="600" 
                 height="450"></svg>
        </div>
        <script src="https://d3js.org/d3.v4.js"></script>
        <script src=
                "https://d3js.org/d3-geo-projection.v2.min.js">
      </script>
        <script>
            var svg = d3.select("svg"),
                width = +svg.attr("width"),
                height = +svg.attr("height");
  
            // Mercator projection
            // Center(0, 0) with 0 rotation
            var gfg = d3
                .geoMercator()
                .scale(width / 2.5 / Math.PI)
                .rotate([0, 0])
                .center([0, 0])
                .translate([width / 2, height / 2]);
  
            // Loading the json data
            // Used json file stored at
             /datageojson/master/world.json*/
            d3.json(
              function (data) {
                // Drawing the map
                svg.append("g").selectAll(
                  "path").data(data.features).enter().append(
                  "path").attr("fill", "black").attr(
                  "d", d3.geoPath().projection(gfg)).style(
                  "stroke", "#ffff");
            });
        </script>
    </body>
</html>


Output:

Mercator  projection of World with no rotation and centered at (0, 0)

Example #2: The following example draws Mercator projection of world after altering the center and rotation.




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            content="width=device-width, 
                initial-scale=1.0"
        />
    </head>
  
    <body>
        <div style="width: 700px; height: 600px;">
            <center>
                <h3 style="color: black;"></h3>
            </center>
  
            <svg width="500" height="450"></svg>
        </div>
        <script src="https://d3js.org/d3.v4.js"></script>
        <script src=
             "https://d3js.org/d3-geo-projection.v2.min.js">
      </script>
        <script>
            var svg = d3.select("svg"),
                width = +svg.attr("width"),
                height = +svg.attr("height");
  
            // Mercator  projection
 // Center(20, 20) and 90 degree rotation w.r.t Y axis
            var gfg = d3
                .geoMercator()
                .scale(width / 2.5 / Math.PI)
                .rotate([90, 0])
                .center([20, 20])
                .translate([width / 2, height / 2]);
  
            // Loading the json data
            // Used json file stored at 
            /datageojson/master/world.json*/
            d3.json(
              function (data) {
                // Draw the map
                svg.append("g").selectAll(
                  "path").data(data.features).enter().append(
                  "path").attr("fill", "Turquoise").attr(
                  "d", d3.geoPath().projection(gfg)).style(
                  "stroke", "#ffff");
            });
        </script>
    </body>
</html>


Output:

Mercator projection with 90 degree rotation w.r.t  Y axis  and centered at (20, 20)

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS