Public
Edited
Oct 1, 2023
Insert cell
geoJson = await FileAttachment("philippines-region.json").json()
Insert cell
renderData = ({
width: 750,
height: 1000,
margin: 50,
})
Insert cell
{
const svg = d3
.create("svg")
.attr("width", renderData.width)
.attr("height", renderData.height);

const clippedWidth = renderData.width - renderData.margin * 2;
const clippedHeight = renderData.height - renderData.margin * 2;

const geoMercator = d3
.geoMercator()
// the center uses longtitude and latitude
// get Long/Lat data from google maps
.center([128, 36])
.fitSize([clippedWidth, clippedHeight], geoJson);

const pathGen = d3.geoPath(geoMercator);

const stage = svg
.append("g")
.attr("transform", `translate(${renderData.margin},${renderData.margin})`);

const textX = 10;
const textY = 10;
const infoText = stage
.append("g")
.attr("transform", `translate(${textX},${textY})`)
.append("text")
.style("font", "20px sans-serif");
infoText.text("Philippines Region");

const onMouseHover = (d) => {
stage
.selectAll(".geopath")
.filter((td) => td.properties.ADM1_EN === d.properties.ADM1_EN)
.attr("fill", "pink");
infoText.text(d.properties.ADM1_EN);
};
const color = "#69b3a2" //overlay
const onMouseLeave = (d) => {
stage
.selectAll(".geopath")
.filter((td) => td.properties.ADM1_EN === d.properties.ADM1_EN)
.attr("fill", "color");
infoText.text("Hover on a region");
};

const tEnter = (enter) =>
enter
.append("path")
.attr("d", pathGen)
.attr("stroke", "white")
.attr("fill", color)
.classed("geopath", true)
.on("mouseenter", onMouseHover)
.on("mouseleave", onMouseLeave);
const tUpdate = null;
const tExit = null;
stage
.selectAll(".geopath")
.data(geoJson.features)
.join(tEnter, tUpdate, tExit);

return svg.node();
}
Insert cell
d3 = require("d3@v5")
Insert cell
topojson = require("topojson-server@3", "topojson-client@3", "topojson-simplify@3")
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