Published
Edited
Dec 17, 2020
Insert cell
md`# Animated map of UK imports`
Insert cell
width = 1000
Insert cell
height = 600
Insert cell
chart_1 = {
const svg = d3.create("svg")
.attr("height", height)
.attr("width", width)
const projection = d3.geoMercator()
.fitSize([width, height], world)
const path = d3.geoPath()
.projection(projection)
svg.selectAll("path")
.data(world.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", "#bdbdbd")
.style("stroke", "#fff")
return svg.node();
}
Insert cell
european_countries = ["Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden"]
Insert cell
chart_2 = {
const svg = d3.create("svg")
.attr("height", height)
.attr("width", width)
const projection = d3.geoMercator()
.fitSize([width, height], europe_extent)
const path = d3.geoPath()
.projection(projection)
svg.selectAll("path")
.data(world.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", "#bdbdbd")
.style("stroke", "#fff")
const uk = world.features.find(country => country.properties.NAME === "United Kingdom")
svg.selectAll("line")
.data(world.features.filter(country => european_countries.find(c => c === country.properties.NAME)))
.enter()
.append("line")
.attr("x1", d => path.centroid(d)[0])
.attr("x2", d => path.centroid(uk)[0])
.attr("y1", d => path.centroid(d)[1])
.attr("y2", d => path.centroid(uk)[1])
.style("stroke", "red")
return svg.node();
}
Insert cell
country_3 = {
const svg = d3.create("svg")
.attr("height", height)
.attr("width", width)
const projection = d3.geoMercator()
.fitSize([width, height], europe_extent)
const path = d3.geoPath()
.projection(projection)
svg.selectAll("path")
.data(world.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", "#bdbdbd")
.style("stroke", "#fff")
const uk = world.features.find(country => country.properties.NAME === "United Kingdom")
const cheese_data = trade_data.filter(d => d.Item === "Cheese, whole cow milk")
const max_value = d3.max(cheese_data, d => d["Value"])
const line_width = d3.scaleSqrt().domain([0, max_value]).range([0, 5])
svg.selectAll("line")
.data(world.features.filter(country => european_countries.find(c => c === country.properties.NAME)))
.enter()
.append("line")
.attr("x1", d => path.centroid(d)[0])
.attr("x2", d => path.centroid(uk)[0])
.attr("y1", d => path.centroid(d)[1])
.attr("y2", d => path.centroid(uk)[1])
.style("stroke", "#20bbfc") //blue
.style("stroke-opacity", "0.7")
.style("stroke-width", d => {
const data = cheese_data.find(c => c["Partner Countries"] === d.properties.NAME)
return line_width(data["Value"])
})
svg.selectAll("text")
.data(world.features.filter(country => european_countries.find(c => c === country.properties.NAME)))
.enter()
.append("text")
.attr("x", d => path.centroid(d)[0])
.attr("y", d => path.centroid(d)[1])
.text(d => d.properties.NAME)
return svg.node();
}
Insert cell
country_4 = {
const svg = d3.create("svg")
.attr("height", height)
.attr("width", width)
const projection = d3.geoMercator()
.fitSize([width, height], europe_extent)
const path = d3.geoPath()
.projection(projection)
svg.selectAll("path")
.data(world.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", "#eaeaea")
.style("stroke", "#fff")
const uk = world.features.find(country => country.properties.NAME === "United Kingdom")
const cheese_data = trade_data.filter(d => d.Item === "Cheese, whole cow milk")
const data_extent = d3.extent(cheese_data, d => d["Value"])
const line_width = d3.scaleSqrt().domain(data_extent).range([0, 5])
svg.selectAll("line.base")
.data(world.features.filter(country => european_countries.find(c => c === country.properties.NAME)))
.enter()
.append("line")
.attr("x1", d => path.centroid(d)[0])
.attr("x2", d => path.centroid(uk)[0])
.attr("y1", d => path.centroid(d)[1])
.attr("y2", d => path.centroid(uk)[1])
.style("stroke", "#20bbfc")
.style("stroke-opacity", "0.2")
.style("stroke-width", d => {
const data = cheese_data.find(c => c["Partner Countries"] === d.properties.NAME)
return line_width(data["Value"])
})
svg.selectAll("line.overlap")
.data(world.features.filter(country => european_countries.find(c => c === country.properties.NAME)))
.enter()
.append("line")
.attr("x1", d => path.centroid(d)[0])
.attr("x2", d => path.centroid(uk)[0])
.attr("y1", d => path.centroid(d)[1])
.attr("y2", d => path.centroid(uk)[1])
.style("stroke-width", d => {
const data = cheese_data.find(c => c["Partner Countries"] === d.properties.NAME)
return line_width(data["Value"])
})
.classed("animated-line", true)
svg.selectAll("text")
.data(world.features.filter(country => european_countries.find(c => c === country.properties.NAME)))
.enter()
.append("text")
.attr("x", d => path.centroid(d)[0])
.attr("y", d => path.centroid(d)[1])
.text(d => d.properties.NAME)
return svg.node();
}
Insert cell
html`<style>

.animated-line {
stroke-dasharray: 1,50;
stroke-linecap: round;
stroke-dashoffset: 1000000;
animation: dash 20000s linear infinite;
stroke-opacity: 0.9;
stroke: #20bbfc;
}

@keyframes dash {
100% {
stroke-dashoffset: 0;
}
}

text {
text-anchor: middle;
font-size: 9px;
font-family: Arial;
opacity: 0.8;
}

</style>`
Insert cell
d3 = require("d3@5")
Insert cell
trade_data = d3.csv("https://interactive.guim.co.uk/atoms/2019/07/brexit-food/v/1565715578702/assets/trade_eu.csv")
Insert cell
world = FileAttachment("world.geojson").json()
Insert cell
europe_extent = JSON.parse(`{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[-14.765625,34.016241889667015],[-14.765625,66.79190947341796],[44.12109374999999,66.79190947341796],[44.12109374999999,34.016241889667015],[-14.765625,34.016241889667015]]]
}
}
]
}`)
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