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.