1

I am attempting to draw a map in d3.js but unfortunately my map is not drawn properly for this projection i have to use an API and Geojson.I have been trying for hours with all sorts of different code to try and get it to display, but I always end up with a blank HTML page. I am a new to d3.js and am pretty amateur. Here is the code I have been trying to get to work,

<!DOCTYPE html>
<html lang="en">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>India Pc</title>

  <style>
    svg {
      background: #f7f7f7;
    }
    
    path:hover {
      stroke-width: 3px;
      stroke: #FFF;
    }
    
    div.tooltip {
      position: absolute;
      text-align: left;
      padding: 0.5em;
      font-size: 12px;
      color: #222;
      background: #FFF;
      border-radius: 2px;
      pointer-events: none;
      box-shadow: 0px 0px 2px 0px #a6a6a6;
    }
    
    #chart {
      margin: 0 auto;
      width: 1000px;
    }
  </style>
</head>

<body>
  <div id="chart">

  </div>

  <script src="http://d3js.org/d3.v4.min.js"></script>
  <script 
  <script src="https://d3js.org/d3-geo-projection.v2.min.js"></script>


  <script>
    var width = 1000;
    var height = 700;
    var div = d3.select("body")
      .append("div")
      .attr("class", "tooltip")
      .style("opacity", 0);
    var svg = d3.select("#chart")
      .append("svg")
      .attr("width", width)
      .attr("height", height);



    const projection = d3.geoMercator()
      .translate([width / 2, height / 2])

      .center([0, 0])
      .scale([width / 1])
      .precision(1);
    const path = d3.geoPath()
      .projection(projection);

    d3.json("https://api.meradesh.org/api/AllData/GetLokSabha2019PCData", getdata)

    function getdata(error, result) {

      result.data.features.forEach((element, index) => {

        if (element.geometry.coordinates === "NA" || element.geometry.coordinates === null) {} else result.data.features[index].geometry.coordinates = JSON.parse(element.geometry.coordinates);

      });
      if (error) throw error;
      console.log(result.data.features);

      svg.append("g")
        .selectAll("path")
        .data(result.data.features)
        .enter()
        .append("path")
        .style("fill", function(d) {
          return d.properties.color
        })

        .attr("d", path)
        .on('mouseover', function(d, i) {
          d3.select(this).transition().duration(300).style("opacity", 5);
          div.transition().duration(300)
            .style("opacity", 1)

          div.html((d.properties.state + " , " + d.properties.district +
              "<br> Constituency Name : " + d.properties.constituencyName +
              "<br> Year : " + d.properties.year +
              "<br> Electors : " + d.properties.electors +
              "<br> Voters : " + d.properties.voters +
              "<br> id : " + d.properties.id

            ))
            .style("left", (d3.event.pageX) + "px")
            .style("top", (d3.event.pageY - 30) + "px");
        })


    };
  </script>
</body>

</html>

Any guidance as to what I may be doing wrong would be greatly appreciated.

  • There are two things causing you grief: one is your polygons are wound incorrectly for use with D3, see [here](https://stackoverflow.com/q/49311001/7106086). Second, you aren't setting your projection correctly, see [here](https://stackoverflow.com/q/42587745/7106086) for an India specific example similar to yours, or [here](https://stackoverflow.com/a/40940028/7106086) for a more generic method: (`projection.fitSize([width,height],result.data)`). Changing these two things results in a working map for me. – Andrew Reid Oct 31 '21 at 04:35
  • This is not showing me all the coordinates of the map please help me out – Mohammad Abass Nov 01 '21 at 04:05
  • I'm not certain how to clarify my comment further, here's an [example](https://jsfiddle.net/nsrp53wk/) implementing fitSize with turf.rewind. – Andrew Reid Nov 01 '21 at 21:00

0 Answers0