Public
Edited
Sep 15, 2020
2 forks
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
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);
}
// create a tooltip
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();
}
Insert cell
districts = await FileAttachment("india_district_filtered.geojson").json()
Insert cell
states = await FileAttachment("Indian_States.geojson").json()
Insert cell
d3 = require("d3@5", "d3-geo-projection@2")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more