Public
Edited
Oct 11, 2022
1 fork
7 stars
Insert cell
Insert cell
import {SeafowlDatabase} from "@seafowl/client"
Insert cell
database = new SeafowlDatabase({ host: "https://demo.seafowl.io" })
Insert cell
Insert cell
Insert cell
allImportsP = Plot.plot({
height: 400,
width: 900,
marginLeft: 100,
grid: true,
x: {
axis: "top",
label: "Commodity"
},
y: {
label: "Country"
},
color: {
scheme: "PiYG"
},
marks: [
Plot.cell(allImports, {
x: "commodity",
y: "country_of_production",
fill: "total_volume"
}),
Plot.text(allImports, {
x: "commodity",
y: "country_of_production",
text: (d) => d.total_volume?.toFixed(0),
title: "total_volume"
})
]
})
Insert cell
viewof productionCountry = Inputs.select(productionCountries, {label: "Country of production"})
Insert cell
viewof importCountry = Inputs.select(importCountries, {
label: `Country of import from ${productionCountry}`
})
Insert cell
viewof commodity = Inputs.select(commodities, {label: `Commodity exports from ${productionCountry} to ${importCountry}`})
Insert cell
Insert cell
Insert cell
topExportersP = Plot.plot({
marginLeft: 300,
x: {
axis: "top",
grid: true
},
y: {
domain: topExporters.map((d) => d.exporter)
},
marks: [
Plot.barX(topExporters, {
x: "total_volume",
y: "exporter"
})
]
})
Insert cell
Insert cell
volumeByYearP = Plot.plot({
x: { tickFormat: d3.format(",.0f") },
marks: [Plot.line(volumeByYear, { x: "year", y: "total_volume" })]
})
Insert cell
Insert cell
topExporters = database.sql`
SELECT exporter, SUM(volume) AS total_volume
FROM supply_chains
WHERE country_of_production = ${productionCountry}
AND country_of_import = ${importCountry}
AND commodity = ${commodity}
GROUP BY 1 ORDER BY 2 DESC LIMIT 20`
Insert cell
volumeByYear = database.sql`
SELECT year::integer AS year,
SUM(volume) AS total_volume
FROM supply_chains
WHERE country_of_production = ${productionCountry}
AND country_of_import = ${importCountry}
AND commodity = ${commodity}
GROUP BY 1 ORDER BY 1 ASC`
Insert cell
database
SELECT "country_of_production", commodity, SUM(volume) AS total_volume FROM supply_chains GROUP BY 1, 2 ORDER BY 3 DESC;
Insert cell
Insert cell
Insert cell
productionCountries = database.sql`
SELECT DISTINCT(country_of_production) AS c
FROM supply_chains ORDER BY 1 ASC
`.then(
(r) => r.map((r) => r.c)
)
Insert cell
Insert cell
importCountries = database.sql`
SELECT DISTINCT(country_of_import)
AS c FROM supply_chains
WHERE country_of_production = ${productionCountry}
ORDER BY 1 ASC
`.then(r => r.map(r => r.c));
Insert cell
Insert cell
commodities = database.sql`
SELECT DISTINCT(commodity) AS c
FROM supply_chains
WHERE country_of_production = ${productionCountry}
AND country_of_import = ${importCountry}
ORDER BY 1 ASC
`.then(r => r.map(r => r.c))
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more