Public
Edited
Dec 31
1 star
Insert cell
Insert cell
viewof height = Inputs.range([0, 800], {label: "Height", step: 1, value:500})
Insert cell
Insert cell
Insert cell
Insert cell
map = {
const svg = d3.create("svg").attr("width", width).attr("height", height);

const viz = svg.append("g");
//.attr("transform", "rotate(90) translate(0 -" + width + ")");

const path = d3.geoPath(clipped);

const outlinePath = viz
.append("path")
.datum(outline)
.attr("id", "outline")
.attr("stroke", "#aaa")
.attr("opacity", 1)
.attr("stroke-width", 1)
.attr("stroke-linejoin", "round")
.attr("fill", "#eee")
.attr("d", path)
.attr("pointer-events", "none");

const landPath = viz
.append("g")
.attr("id", "land")
.selectAll("path")
.data(ne_110m_land.features)
.join("path")
.attr("id", (d) => d.properties.formal_en)
.attr("d", path)
.attr("fill", "#fff")
.attr("fill-opacity", 1)
.attr("stroke", "#043B62")
.attr("stroke-alignment", "inner");

const poli = viz
.append("g")
.attr("id", "land")
.selectAll("path")
.data(unrotatedPolygons.features.filter((d, i) => i === selectedId))
.join("path")
.attr("d", path)
.attr("fill", "none")
.attr("fill-opacity", 1)
.attr("stroke", "red")
.attr("stroke-alignment", "inner");

const projCentroids = unrotatedPolygons.features.map((d) =>
projection(d3.geoCentroid(d))
);

// 1. Get the selected face polygon and find its centroid
const face = unrotatedPolygons.features[selectedId];
const centroid = d3.geoCentroid(face); // [longitude, latitude]

// 2. Define a point slightly north of the centroid (e.g., 10° north)
const northRef = [centroid[0], centroid[1] + 10];

// 3. Project both points
const [cx, cy] = clipped(centroid);
const [nx, ny] = clipped(northRef);

const cent = viz
.append("g")
.append("circle")
.attr("cx", cx)
.attr("cy", cy)
.attr("r", 5)
.attr("fill", "blue")
.attr("stroke", "none");

viz
.append("line")
.attr("x1", cx)
.attr("y1", cy)
.attr("x2", nx)
.attr("y2", ny)
.attr("stroke", "black")
.attr("stroke-width", 2);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@7.9", "d3-geo@3.1.1", "d3-geo-polygon@2.0.1")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more