Published
Edited
Feb 10, 2021
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
malta_id = countryCode("MLT")
Insert cell
Insert cell
malta110 = countries110.objects.countries.geometries.find(e => e.id === malta_id)
Insert cell
Insert cell
malta50 = countries50.objects.countries.geometries.find(e => e.id === malta_id)
Insert cell
Insert cell
arcs = malta50.arcs.flat(2)
Insert cell
Insert cell
arcs_geometry = arcs.map(pos => countries50.arcs[pos])
Insert cell
Insert cell
Insert cell
Insert cell
function transformPoint([x,y]){
const oldTransform = countries50.transform
const newTransform = countries110.transform
const newX = (x * oldTransform.scale[0] + oldTransform.translate[0] - newTransform.translate[0]) / (newTransform.scale[0])
const newY = (y * oldTransform.scale[1] + oldTransform.translate[1] - newTransform.translate[1]) / (newTransform.scale[1])
return [newX, newY]
}
Insert cell
Insert cell
function transformScale([x,y]){
const oldTransform = countries50.transform
const newTransform = countries110.transform
const newX = (x * oldTransform.scale[0]) / (newTransform.scale[0])
const newY = (y * oldTransform.scale[1]) / (newTransform.scale[1])
return [newX, newY]
}
Insert cell
transformed_arcs = arcs_geometry.map(([first, ...rest]) => [transformPoint(first), ...rest.map(transformScale)])
Insert cell
Insert cell
Insert cell
Insert cell
current_length = countries110.arcs.length
Insert cell
Insert cell
merged_topojson.arcs.push(...transformed_arcs)
Insert cell
Insert cell
merged_topojson.arcs.slice(-transformed_arcs.length)
Insert cell
Insert cell
mapped_arcs = arcs.reduce((o, key, i) => ({ ...o, [key]: current_length + i}), {})
Insert cell
Insert cell
malta50
Insert cell
malta_copy = {
const malta_copy = JSON.parse(JSON.stringify(malta50));
malta_copy.arcs = malta_copy.arcs.map(e => e.map(e => e.map(e => mapped_arcs[e])))
return malta_copy
}
Insert cell
Insert cell
Insert cell
merged_topojson.objects.countries.geometries.push(malta_copy)
Insert cell
Insert cell
merged_topojson
Insert cell
Insert cell
final_topojson = {
const final_topojson = merged_topojson
final_topojson.arcs.push(...transformed_arcs)
final_topojson.objects.countries.geometries.push(malta_copy)
return final_topojson
}
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const countriesPath = svg.append("path")
.datum(countries)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 0.5)
.attr("stroke-linejoin", "round")
.attr("d", path);
const zoomed = ({ transform }) => {
countriesPath.attr('transform', transform)
countriesPath.attr('stroke-width', 0.5 / transform.k)
}

const zoom = d3.zoom().scaleExtent([1, 32]).on('zoom', zoomed)

svg.call(zoom)

return svg.node();
}
Insert cell
height = 500
Insert cell
projection = d3.geoNaturalEarth1()
Insert cell
path = d3.geoPath(projection)
Insert cell
countries = topojson.feature(final_topojson, final_topojson.objects.countries)
Insert cell
Insert cell
d3 = require("d3")
Insert cell
topojson = require("topojson-client")
Insert cell
codes = FileAttachment("country-codes.json").json()
Insert cell
Insert cell
countryCode("FRA")
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