Public
Edited
Sep 13, 2022
3 forks
Insert cell
Insert cell
init(data).node()
Insert cell
data = d3.json("https://geojson.cn/api/data/100000.json")
Insert cell
function init(data) {
const projection = d3.geoMercator().fitExtent(
[
[0, 0], //左上角坐标
[width, height], //右下角坐标
],
data
);

var path = d3.geoPath().projection(projection);
var svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);
svg
.selectAll("path")
.data(data.features)
.enter()
.append("path")
.on("mouseover", function () {
d3.select(this).style("fill", "#7c3aed");
d3.select(this).style("stroke-width", 2);
})
.on("mouseout", function () {
d3.select(this).style("fill", "white");
d3.select(this).style("stroke-width", 1);
})
// @ts-ignore
.attr("d", path)
.attr("fill", (d) => {
if (d.properties.name.indexOf("广东") > -1) {
return "#0ea5e9";
} else {
return "white";
}
})
.attr("stroke", "black")
.attr("stroke-width", 1)
.append("title")
.text((d) => d.properties.name);
return svg
}
Insert cell
width = 700
Insert cell
height = 700;
Insert cell
d3.version
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