function renderMap() {
const width = 800;
const height = 800;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
const path = d3.geoPath();
const projection = d3.geoIdentity()
.reflectY(true)
.fitSize([width, height], geoData);
path.projection(projection);
const values = mapData.map(d => d.value);
const meanValue = d3.mean(values);
const extent = d3.extent(values);
const maxAbs = d3.max([Math.abs(extent[0] - meanValue), Math.abs(extent[1] - meanValue)]);
const colorScale = d3.scaleDiverging()
.domain([meanValue - maxAbs, meanValue, meanValue + maxAbs])
.interpolator(d3.interpolateRdBu);
const colorMap = Object.fromEntries(mapData.map(d => [d.state, d.value]));
svg.selectAll("path")
.data(geoData.features)
.join("path")
.attr("d", path)
.attr("fill", d => colorScale(colorMap[d.properties.ST_NM] || 0))
.attr("stroke", "#000")
.attr("cursor", "pointer")
.on("click", (event, d) => {
mutable selectedState = d.properties.ST_NM;
});
// Add title
svg.append("text")
.attr("x", width-180)
.attr("y", 30)
.attr("text-anchor", "middle")
.attr("font-size", "38px")
.attr("font-weight", "bold")
.text("State's Per capita Income Map");
// Add subhead
svg.append("text")
.attr("x", width-230)
.attr("y", 70)
.attr("text-anchor", "middle")
.attr("font-size", "18px")
.selectAll("tspan")
.data([
"India’s per capita GSDP shows wide regional",
"disparities. Southern & western states lead,",
"while some populous northern states ",
"continue to lag economically."
])
.enter()
.append("tspan")
.attr("x",width-230)
.attr("dy", (d, i) => i === 0 ? 0 : 20) // Line spacing
.text(d => d);
// Legend dimensions (vertical)
const legendHeight = 200;
const legendWidth = 10;
const legendX = width - 60; // push to right
const legendY = height / 2 - legendHeight / 2;
// Create vertical gradient
const defs = svg.append("defs");
const gradient = defs.append("linearGradient")
.attr("id", "legend-gradient")
.attr("x1", "0%")
.attr("x2", "0%")
.attr("y1", "100%")
.attr("y2", "0%");
gradient.selectAll("stop")
.data([
{ offset: "0%", color: colorScale(0) },
{ offset: "50%", color: colorScale(meanValue) },
{ offset: "100%", color: colorScale(meanValue + maxAbs) }
])
.join("stop")
.attr("offset", d => d.offset)
.attr("stop-color", d => d.color);
// Draw the vertical bar
svg.append("rect")
.attr("x", legendX)
.attr("y", legendY)
.attr("width", legendWidth)
.attr("height", legendHeight)
.style("fill", "url(#legend-gradient)");
// Add vertical axis beside the legend
const legendScale = d3.scaleLinear()
.domain([0, meanValue + maxAbs])
.range([legendY + legendHeight, legendY]); // y-axis: bottom → top
const legendAxis = d3.axisRight(legendScale)
.tickValues([
0,
meanValue,
meanValue + maxAbs
])
.tickFormat(d3.format(".2f"));
svg.append("g")
.attr("transform", `translate(${legendX + legendWidth}, 0)`)
.call(legendAxis)
.selectAll("text")
.style("font-size", "16px");
const labeledStates = ["Maharashtra", "Karnataka", "Uttar Pradesh", "Tamil Nadu", "Madhya Pradesh", "Rajasthan", "Himachal Pradesh", "West Bengal"];
svg.selectAll("text.state-label")
.data(geoData.features.filter(d => labeledStates.includes(d.properties.ST_NM)))
.enter()
.append("text")
.attr("class", "state-label")
.attr("x", d => path.centroid(d)[0])
.attr("y", d => path.centroid(d)[1])
.attr("text-anchor", "middle")
.attr("alignment-baseline", "central")
.style("font-size", "16px")
.style("fill", "white")
.style("pointer-events", "none")
.text(d => d.properties.ST_NM);
return svg.node();
}