Published
Edited
Aug 29, 2022
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
map = html`<svg viewBox="0 0 ${width} ${height}" style="display: block;">
<defs>
${export_paths.map(
(s, i) =>
`<marker id="triangle${i}" viewBox="0 0 2.5 2.5"
refX="1" refY="1.25"
markerUnits="strokeWidth"
markerWidth="2.5" markerHeight="2.5"
orient="auto">
<path d="M 0 0 L 2.5 1.25 L 0 2.5 z" fill-opacity="0.7" fill="${s.properties.startcolor}"/>
</marker>`
)}
</defs>
<path d="${path(graticule)}" fill="none" stroke="#eee"></path>
<path d="${path(outline)}" fill="none" stroke="#ddd"></path>
<path d="${path(world10m_wound)}" fill="#ccc" stroke="#aaa"></path>
${export_paths.map(
(s, i) =>
`<path d="${path(s)}" fill="none" stroke="${
s.properties.startcolor
}" stroke-width="${Math.max(
1.5,
s.properties.percentage * 10
)}" marker-end="url(#triangle${i})" stroke-opacity="0.7" ></path>`
)}
</svg>`
Insert cell
selected_with_percent.map((s) => Math.max(1.5, s.percentage * 10))
Insert cell
Insert cell
Insert cell
bulk_cobalt = FileAttachment("bulk_cobalt.csv").csv()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
projection = selectProj.value().fitSize([width, height], world)
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")
.concat(usa)
Insert cell
tw = center.filter((f) => f.properties.ISO == "TWN")
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
center.find((c) => c.properties.ISO == "USA")
Insert cell
selected = cobalt_export.filter((c) => c.key == selectYear)[0]
Insert cell
total = selected.values.reduce(function (acc, obj) {
return acc + parseInt(obj.NetWeight);
}, 0)
Insert cell
selected_with_percent = selected.values.map((s) => {
s.percentage = s.NetWeight / total;
return s;
})
Insert cell
// make lines between trading partners
export_paths = selected_with_percent
.map((m) => {
let start = center.filter((c) => c.properties.ISO == m.PISO);
let end = center.filter((c) => c.properties.ISO == m.RISO);
if ((start.length > 0) & (end.length > 0)) {
return turf.lineString(
[start[0].geometry.coordinates, end[0].geometry.coordinates],
{
start: m.Partner_Name,
end: m.Reporter_Name,
value: m.TradeQuantity,
startcolor: country_colors.find((c) => c.iso == m.PISO).color,
endcolor: country_colors.find((c) => c.iso == m.RISO).color,
percentage: m.percentage
}
);
}
})
.filter((f) => f != undefined)
.sort((a, b) => (a.properties.percentage > b.properties.percentage ? 1 : -1))
Insert cell
world = FileAttachment("ne_10m_admin_0.geojson.json").json()
Insert cell
nation_center = world.features.map((n) =>
turf.centroid(n, { properties: { name: n.properties.SOV_A3 } })
)
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