Published
Edited
Nov 30, 2020
1 fork
Insert cell
Insert cell
import { databyday } from "@fil/ncov2019-data"
Insert cell
import { tdeath } from "@fil/ncov2019-data"
Insert cell
tdeath
Insert cell
deathsByDate = {
return tdeath.sort(function(a, b) {
var keyA = new Date(a.date);
var keyB = new Date(b.date);

// Compare the 2 dates
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;

return 0;
});
}
Insert cell
deaths = {
let allDeaths = [];

deathsByDate.forEach(function(countryDay) {
for (let i = 0; i < countryDay.cases; i++) {
allDeaths.push({
country: countryDay.country,
date: countryDay.ymd,
dateClass: 'day' + countryDay.ymd.split('-').join(''),
countryClass: countryDay.country.split(' ').join('')
});
}
});

return allDeaths;
}
Insert cell
cleanData = {
let cleanData = [];

databyday.forEach(function(day) {
//flat list of country names
let countryCollector = [];
//list of country data objects
let countryDayData = [];

let allDeaths = 0;

for (const [key, value] of Object.entries(day)) {
if (typeof value == 'number') {
allDeaths += value;
}
let splitCountryName = key.split(":")[0];

if (countryCollector.indexOf(splitCountryName) == -1) {
//country is new country
countryCollector.push(splitCountryName);
countryDayData.push({ name: splitCountryName, deaths: value });
} else {
//country is country we have placed already
let countryIndex = countryCollector.indexOf(splitCountryName);
countryDayData[countryIndex].deaths += value;
}
}
let eachDay = countryDayData.shift().deaths;

let dataobj = {
date: eachDay,
totalDeaths: allDeaths,
countries: countryDayData
};
cleanData.push(dataobj);
});

return cleanData;
}
Insert cell
totalDeaths = deaths.length
Insert cell
width = 800
Insert cell
columns = 150
Insert cell
rows = Math.ceil(totalDeaths / skip / columns)
Insert cell
perCircle = width / columns
Insert cell
height = perCircle * rows
Insert cell
radius = perCircle / 2
Insert cell
Insert cell
visualizedDataset = {
let reducedData = [];

for (let i = 0; i < totalDeaths; i = i + skip) {
reducedData.push(deaths[i]);
}
return reducedData;
}
Insert cell
Insert cell
myCell = {
let svg = d3
.create('svg')
.attr("width", width)
.attr('height', height)
.attr('id', 'mainViz');

let bg = svg
.append('rect')
.attr("width", width)
.attr('height', height)
.attr('fill', '#222');

let deathCircs = svg
.selectAll('.deathCircles')
.data(visualizedDataset)
.enter()
.append('circle')
.attr('cx', (d, i) => (i % columns) * perCircle + radius)
.attr('cy', (d, i) => Math.floor(i / columns) * perCircle + radius)
.attr('r', radius)
.attr('class', d => d.dateClass + " " + d.countryClass + " deathCircles")
.attr('fill', '#aaa')
.attr('stroke', 'white')
.on('mouseover', function(d) {
svg.selectAll('.' + d.countryClass).attr('stroke-width', 1);

svg.selectAll('.' + d.dateClass).attr('fill', 'white');

d3.select('#legend').text(d.country + " " + d.date);
})
.on('mouseout', function(d) {
let mousedClass = d3
.selectAll('.deathCircles')
.attr('stroke-width', 0)
.attr('fill', '#aaa');

d3.select('#legend').text('');
});

return svg.node();
}
Insert cell
d3 = require("d3@5")
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