Public
Edited
Oct 18, 2022
Importers
8 stars
Insert cell
Insert cell
Insert cell
map = html`<svg viewBox="0 0 ${width} ${height}" style="display: block;">
<g id = "basemap">
<path d="${path(graticule)}" fill="none" stroke="#ddd"></path>
<path d="${path(outline)}" fill="none" stroke="#ddd"></path>
<path d="${path(world10m_wound)}" fill="#ccc" stroke="#aaa"></path>
</g>
<g id = "countries">
${countries.map(
(c) =>
`<path d="${path(c)}" fill="darkgoldenrod" id = "${
c.properties.ADM0_A3
}"></path>`
)}
</g>
<g id ="imports">
${importsWithColors.map(
(s) =>
`<g id = "HS${s.key}">
${s.values
.slice(0, 5)
.map(
(v) =>
`<path d="${path(
turf.lineSplit(v, twBuffer).features[0]
)}" fill="none" stroke=${v.properties.color} stroke-width="${Math.max(
1.5,
v.properties.percentage / 10
)}" id = "${v.properties.start.replace(" ", "")}to${v.properties.end}(${
v.properties.percentage
})"></path>`
)}
</g>`
)}
</g>
</svg>`
Insert cell
colors = ["#000004", "#51127c", "#b73779", "#fc8961", "#fcfdbf"]
Insert cell
codes = Array.from(new Set(imports.map((i) => i["Commodity Code"])))
Insert cell
countryset = new Set(
importsWithColors
.map((m) => m.values.slice(0, 6))
.flat()
.map((m) => m.properties.iso)
)
Insert cell
countries = world10m_wound.features
.filter((f) => countryset.has(f.properties.ADM0_A3))
.filter((f) => f.properties.ADM0_A3 != "FRA")
.concat(france)
Insert cell
Insert cell
imports_A3 = imports.map((i) => {
i.ISO3 = isocodes
.filter((f) => f["alpha-2"] == i.CountryISO2)
.map((m) => m["alpha-3"]);
return i;
})
Insert cell
transform = d3.geoTransform({
point: function (x, y) {
var projection = d3.geoMercator();
this.stream.point(...projection([x, y]));
}
})
Insert cell
Insert cell
importsWithColors = imports_nested
.map((m, i) => {
m.values.map((v) => {
v.properties.color = colors[i];
return v;
});
return m;
})
Insert cell
testLine = turf.bezierSpline(
turf.lineString([
center.filter((f) => f.properties.ISO == "TWN")[0].geometry.coordinates,
center.filter((f) => f.properties.ISO == "USA")[0].geometry.coordinates
])
)
Insert cell
Insert cell
FC_countries = turf.featureCollection(countries)
Insert cell
projection = selectProj
.value()
.fitSize([width, height], world)
.rotate([-120.61108655801085, -23.685162485477473])
Insert cell
path = d3.geoPath(projection)
Insert cell
height = 600
Insert cell
world10m_wound = FileAttachment("world10m_wound.json").json()
Insert cell
outline = ({type: "Sphere"})
Insert cell
graticule = d3.geoGraticule10()
Insert cell
//get center points of all countries
center = world.features
.map((w) =>
turf.centroid(w.geometry, {
properties: { ISO: w.properties.ADM0_A3 }
})
)
.filter((w) => w.properties.ISO != "USA")
.filter((w) => w.properties.ISO != "FRA")
.concat(usa)
.concat(franceCenter)
Insert cell
import_paths
Insert cell
tw = center.filter((f) => f.properties.ISO == "TWN")
Insert cell
twBuffer = turf.buffer(
world.features.filter((w) => w.properties.ADMIN == "Taiwan")[0],
60,
{ units: "miles" }
)
Insert cell
//this is fussy but the "center" of the USA when factoring in Alaska and Hawaii ends up not in the center of the contiguous US and it looks kind of clunky is all
usa = [
Object({
type: "Feature",
properties: { ISO: "USA" },
geometry: { type: "Point", coordinates: [-98.583333, 39.833333] }
})
]
Insert cell
france = turf.rewind(
turf.flatten(world.features.filter((w) => w.properties.ADM0_A3 == "FRA")[0])
.features[1],
{ reverse: true }
)
Insert cell
franceCenter = [
Object({
type: "Feature",
properties: { ISO: "FRA" },
geometry: {
type: "Point",
coordinates: turf.centroid(france).geometry.coordinates
}
})
]
Insert cell
center.find((c) => c.properties.ISO == "USA")
Insert cell
// make lines between trading partners
import_paths = imports_A3
// .filter((f) => f["Commodity Code"] == selectHS)
.map((m) => {
if (m.ISO3 == "USA") {
let end = center.filter((c) => c.properties.ISO == m.ISO3[0]);
let start = center.filter((c) => c.properties.ISO == "TWN");
}
let start = center.filter((c) => c.properties.ISO == m.ISO3[0]);
let end = center.filter((c) => c.properties.ISO == "TWN");
if ((start.length > 0) & (end.length > 0)) {
return turf.bezierSpline(
turf.lineString(
[start[0].geometry.coordinates, end[0].geometry.coordinates],
{
start: m.CountryName,
end: "Taiwan",
hs: m["Commodity Code"],
iso: m.ISO3[0],
// value: m.TradeQuantity,
// // startcolor: country_colors.find((c) => c.iso == m.PISO).color,
// // endcolor: country_colors.find((c) => "TWN").color,
percentage: m.Percent
}
),
{
properties: {
start: m.CountryName,
end: "Taiwan",
hs: m["Commodity Code"],
iso: m.ISO3[0],
// value: m.TradeQuantity,
// // startcolor: country_colors.find((c) => c.iso == m.PISO).color,
// // endcolor: country_colors.find((c) => "TWN").color,
percentage: m.Percent
}
}
);
}
})
.filter((f) => f != undefined)
//.sort((a, b) => (a.properties.percentage > b.properties.percentage ? 1 : -1))
Insert cell
world.features.filter((w) => w.properties.ADMIN == "Taiwan")
Insert cell
world = FileAttachment("ne_10m_admin_0.geojson.json").json()
Insert cell
imports = FileAttachment("imports@1.csv").csv()
Insert cell
isocodes = FileAttachment("ISOcodes.csv").csv()
Insert cell
nation_center = world.features.map((n) =>
turf.centroid(n, { properties: { name: n.properties.ISO_A2 } })
)
Insert cell
d3 = require("d3-collection@latest", "d3@5.9")
Insert cell
Insert cell
projections.map((p) => p.name)
Insert cell
import { projections } from "@d3/projection-comparison"
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