Public
Edited
Oct 3, 2024
8 forks
Importers
57 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`# Hello map `
Insert cell
height = 480
Insert cell
projection = d3.geoPatterson()
Insert cell
path = d3.geoPath(projection)
Insert cell
Insert cell
Insert cell
borders = topojson.mesh(
world,
world.objects.world_countries_data,
(a, b) => a !== b
)
Insert cell
Insert cell
md `If you want to extract borders only for one country, you can do like that`
Insert cell
id = "USA"
Insert cell
borders_country = topojson.mesh(
world,
world.objects.world_countries_data,
(a,b) => (a.properties.ISO3 == id & b.properties.ISO3 != id) || (b.properties.ISO3 == id & a.properties.ISO3 != id)
)
Insert cell
{
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("width", "100%")
.style("height", "auto");

svg
.append("path")
.datum(borders)
.attr("fill", "none")
.attr("stroke", "grey")
.attr("stroke-width", 1)
.attr("d", path);

svg
.append("path")
.datum(borders_country)
.attr("fill", "none")
.attr("stroke", "#ed87c8")
.attr("stroke-width", 3)
.attr("d", path);

return svg.node();
}
Insert cell
Insert cell
merged = topojson.merge(world, world.objects.world_countries_data.geometries)
Insert cell
{
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("width", "100%")
.style("height", "auto");

svg
.append("path")
.datum(merged)
.attr("fill", "#fae6f2")
.attr("stroke", "#ed87c8")
.attr("stroke-width", 1)
.attr("d", path);

return svg.node();
}
Insert cell
Insert cell
neighbors = topojson.neighbors(world.objects.world_countries_data.geometries)
Insert cell
ids = countries.features.map(d => d.properties.ISO3)
Insert cell
getcontig = id => {
var result = [];
var contig = neighbors[ids.indexOf(id)];
result = contig.map(i => ids[i]);
return result;
}
Insert cell
mycountry = "CHN"
Insert cell
me = countries.features.filter(d => d.properties.ISO3 == mycountry)
Insert cell
neighborhood = countries.features.filter(d =>
getcontig(mycountry).includes(d.properties.ISO3)
)
Insert cell
{
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("width", "100%")
.style("height", "auto");

// Sphere

svg
.append("path")
.datum(countries)
.attr("fill", "#eeeeee")
.attr("stroke", "white")
.attr("stroke-width", 0.4)
.attr("d", path);

svg
.append("path")
.data(me)
.attr("fill", "#ed87c8")
.attr("stroke", "white")
.attr("stroke-width", 0.4)
.attr("d", path);

svg
.append("g")
.attr("id", "map")
.selectAll("path")
.data(neighborhood)
.join("path")
.attr("fill", "#f0d175")
.attr("d", path);

return svg.node();
}
Insert cell
Insert cell
world_simplified = {
let world_simplified = topojson.presimplify(world);
let min_weight = topojson.quantile(world_simplified, minArea);
world_simplified = topojson.simplify(world_simplified, min_weight);
return world_simplified;
}
Insert cell
countries2 = topojson.feature(
world_simplified,
world_simplified.objects.world_countries_data
).features
Insert cell
world_simplified.objects.world_countries_data.geometries
Insert cell
viewof minArea = Range([0.01, 0.1], { step: 0.001 })
Insert cell
{
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("width", "100%")
.style("height", "auto");

svg
.append("g")
.attr("id", "map")
.selectAll("path")
.data(countries2)
.join("path")
.attr("fill", "#ed87c8")
.attr("stroke", "white")
.attr("stroke-width", 0.4)
.attr("d", path);

return svg.node();
}
Insert cell
md`# Same thing with geoCurvePath`
Insert cell
Insert cell
path2 = smooth(d3.curveBasisClosed, projection)
Insert cell
land_simplified = {
let w = topojson.presimplify(world);
let min_weight = topojson.quantile(w, minArea2);
w = topojson.simplify(w, min_weight);

//Merge the simplified countries into a new land shape
return topojson.merge(w, w.objects.world_countries_data.geometries);
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
land_simplified2 = {
let w = topojson.presimplify(world);
let min_weight = topojson.quantile(w, minArea2);
w = topojson.simplify(w, min_weight);
//Merge the simplified countries
return topojson.merge(w, w.objects.world_countries_data.geometries);
}
Insert cell
viewof minArea2 = Range([0.005, 0.1], { step: 0.001 })
Insert cell
map = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("width", "100%")
.style("height", "auto");

svg
.append("path")
.datum(land_simplified)
.attr("d", path2)
.style("fill", "#e3a3cc");

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
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