Published
Edited
Sep 8, 2020
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.rollup(
d3.csvParse(await FileAttachment("lh_mun_vols.csv").text(), d => ({ ...d, volume: +d.volume })),
v => ({
value: d3.sum(v.map(d => d.volume)),
children: d3.rollup(v, v2 => v2[0].volume, d => d.mun)
}),
d => d.lh
)
Insert cell
height = 600
Insert cell
projection = d3
.geoOrthographic()
.rotate([65, 55, 6])
.center([4, 42])
.scale(1000)
Insert cell
colour = d3.scaleSequential(
d3.extent(data, ([,v]) => v.value),
d => d3.interpolateInferno(1 - d)
)
Insert cell
countries = d3
.json("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json")
.then(d => topojson.feature(d, d.objects.countries))
Insert cell
brazil = FileAttachment("brazil@1.json")
.json()
.then(d => topojson.feature(d, d.objects.level3))
Insert cell
graticule = d3.geoGraticule10()
Insert cell
// adapted from https://observablehq.com/@d3/color-legend
function drawLegend({
color,
title,
tickSize = 6,
width = 320,
height = 44 + tickSize,
marginTop = 18,
marginRight = 20,
marginBottom = 16 + tickSize,
marginLeft = 0,
ticks = width / 64,
tickFormat,
tickValues
} = {}) {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.style("overflow", "hidden")
.style("display", "block");

// scale
const x = Object.assign(
color
.copy()
.interpolator(d3.interpolateRound(marginLeft, width - marginRight)),
{
range() {
return [marginLeft, width - marginRight];
}
}
);

// ramp
svg
.append("image")
.attr("x", marginLeft)
.attr("y", marginTop)
.attr("width", width - marginLeft - marginRight)
.attr("height", height - marginTop - marginBottom)
.attr("preserveAspectRatio", "none")
.attr("xlink:href", ramp(color.interpolator()).toDataURL());

// axis
const axis = d3
.axisBottom(x)
.ticks(ticks, typeof tickFormat === "string" ? tickFormat : undefined)
.tickFormat(typeof tickFormat === "function" ? tickFormat : undefined)
.tickSize(tickSize)
.tickValues(tickValues);

// ticks
const g = svg
.append("g")
.attr("transform", `translate(0, ${height - marginBottom})`)
.style("font-family", "sans-serif")
.call(axis)
.call(g =>
g.selectAll(".tick line").attr("y1", marginTop + marginBottom - height)
)
.call(g =>
g
.select(".domain")
.attr("stroke-width", 0)
.remove()
)
.call(g =>
g
.append("text")
.attr("y", marginTop + marginBottom - height - 6)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(title)
);

// update
return Object.assign(svg.node(), {
update(domain) {
x.domain(domain);
g.transition()
.duration(500)
.call(axis)
.call(g =>
g
.selectAll(".tick line")
.attr("y1", marginTop + marginBottom - height)
)
.call(g =>
g
.select(".domain")
.attr("stroke-width", 0)
.remove()
);
}
});
}
Insert cell
d3 = require("d3@6")
Insert cell
topojson = require("topojson-client@3")
Insert cell
import { ramp } from "@d3/color-legend"
Insert cell
// lh_mun_vols = DatabaseClient("trase-master").then(client =>
// client.query(
// `
// SELECT SUBSTRING(logistics_hub_trase_id, 4, 9) as lh,
// SUBSTRING(region_production_1_trase_id, 4, 9) as mun,
// SUM(volume) as volume
// FROM views.datasets
// WHERE country_of_production = 'BRAZIL'
// AND commodity = 'SOY'
// AND year = 2017
// GROUP BY lh, mun
// `
// )
// )
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