chart = {
let clicked = false
const margin = {top: 0, right: 0, bottom: 0, left: 0};
const width = 850 - margin.left - margin.right;
const height = width*0.85 - margin.top - margin.bottom;
const svg = d3.select('.chart')
.append('svg')
.attr('width', width)
.attr('height', height)
const g = svg.append('g')
.attr('class', 'map')
const projection = d3.geoMercator()
.center([78.9629, 20.5937])
.scale(width*2)
.translate([ width/2, height/2 + 20 ])
const path = d3.geoPath().projection(projection);
const zoom = d3.zoom()
.scaleExtent([1, 20])
.on('zoom', zoomed);
svg.call(zoom)
function zoomed() {
g.attr('transform', d3.event.transform);
}
let Tooltip = d3.select(".chart")
.append("div")
.attr("class", "tooltip")
let mouseover = function(d) {
clicked = false
Tooltip
.html(
"<p>" + d.properties.NAME_1 + "</p>"
)
.style("left", (path.centroid(d)[0] + 25).toString() + "px")
.style("top", (path.centroid(d)[1] - 25).toString() + "px")
.style("opacity", 1)
}
let mouseleave = function(d) {
if(clicked){ // prevent tooltip from hiding if marker is clicked
Tooltip.style("opacity", 1)
} else {
Tooltip.style("opacity", 0)
}
}
ready(districts, states)
function ready(districts, states) {
let map = g.append('g')
let statesG = map.selectAll('g.state')
.data(states.features)
.enter().append("g")
.attr("class", 'state')
statesG
.append('path')
.attr('class', 'state-path')
.attr('d', path)
.style('fill', 'lightgray')
.style('stroke', 'gray')
.style('stroke-width', 0.5)
.style('opacity', 1)
.style('cursor', 'pointer')
.on("click", function(){
mouseover
clicked = !clicked
})
.on("mouseover", mouseover)
.on("mouseleave", mouseleave)
let districtsG = map.selectAll('g.district')
.data(districts.features)
.enter().append("g")
.attr("class", 'district')
districtsG
.append('path')
.attr('class', 'district-path')
.attr('d', path)
.style('fill', 'black')
.style('stroke', 'none')
.style('stroke-width', 0)
.style('opacity', 1)
// only append labels for selected districts
districtsG
.append('text')
.attr('class', 'district-text')
.attr("transform", function (d) { return "translate(" + path.centroid(d) + ")"; })
.attr('dy', '-1.25em')
.style("font-family", "courier")
.style("font-size", 10)
.style("fill", "black")
.attr("text-anchor", "middle")
.attr("alignment-baseline", "central")
.text(function (d) {
if(d.properties.NAME_2 === "Bangalore Urban"){
return "Bangalore"
} else if(d.properties.NAME_2 === "Greater Bombay"){
return "Mumbai"
} else {
return d.properties.NAME_2;
}
})
}
return svg.node();
}