Published
Edited
Sep 28, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dms = {
let dimensions = {
width: width,
height: width * 0.6,
marginTop: width / 20
};

dimensions.marginLeft = dimensions.marginTop;
dimensions.marginBottom = dimensions.marginTop;
dimensions.marginRight = dimensions.marginLeft;

dimensions.chartWidth = d3.min([
800,
dimensions.width - dimensions.marginLeft - dimensions.marginRight
]);
dimensions.chartHeight =
dimensions.height - dimensions.marginTop - dimensions.marginBottom;

return dimensions;
}
Insert cell
Insert cell
data = {
const attached = await FileAttachment(
"CoronavirusWorldData-WashingtonPost.csv"
).text();

const dataRaw = d3.csvParse(attached);

const dataProcessed = dataRaw.map(d => {
let { countryIso, deaths, date } = d;
return { country: countryIso, deaths: +deaths, date: date };
});

return dataProcessed;
}
Insert cell
dataDeathsByDay = {
return d3.group(data, d => d.date);
}
Insert cell
dataDeathsSingleDay = Array.from(dataDeathsByDay.values())[day]
Insert cell
dataDeathsLastDay = Array.from(dataDeathsByDay.values()).pop()
Insert cell
dataDeathsTotal = dataDeathsSingleDay.reduce(
(result, current) => result + current.deaths,
0
)
Insert cell
Insert cell
scaleDeaths = d3
.scaleLinear()
// .domain([0, d3.max(dataDeathsSingleDay.map(d => Math.sqrt(d.deaths)))])
.domain([0, d3.max(dataDeathsLastDay.map(d => Math.sqrt(d.deaths)))])
.range([0, dms.height / 6])
Insert cell
Insert cell
countries = {
const attached = await FileAttachment("WorldMapSimplified-2.json").json();

const shapes = topojson.feature(
attached,
attached.objects["ne_10m_admin_0_countries_lakes"]
);

// shapes.features = shapes.features.filter(
// d => d.properties["CONTINENT"] == "Asia"
// );

return shapes;
}
Insert cell
projection = d3
.geoNaturalEarth1()
.fitExtent(
[
[dms.marginLeft, dms.marginTop],
[dms.width - dms.marginRight, dms.height - dms.marginBottom]
],
countries
)
Insert cell
path = d3.geoPath(projection)
Insert cell
Insert cell
square = function(size) {
const corner = size / 2;

return `M ${corner} ${-corner} L ${-corner} ${-corner} L ${-corner} ${corner} L ${corner} ${corner} Z`;
}
Insert cell
Insert cell
Insert cell
Insert cell
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