Public
Edited
Jun 9, 2023
Insert cell
Insert cell
Insert cell
import {rangeSlider} from "@mootari/range-slider"
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
import {select, radio, slider, checkbox} from "@jashkenas/inputs"
Insert cell
cntry_data_raw = d3.json("https://raw.githubusercontent.com/covid19-data/covid19-data/master/output/cntry_stat_owid.json")
Insert cell
cntry_data = cntry_data_raw.filter(d => d.region !== null)
Insert cell
us_state_data_raw = d3.json("https://raw.githubusercontent.com/covid19-data/covid19-data/master/output/us_state_nyt.json")
Insert cell
us_state_data = us_state_data_raw.map(d => ({
//https://forum.freecodecamp.org/t/parenthesis-wrapped-around-the-object/426725/6
'country_code': d.code,
'country_name': d.name,
'region': "US States",
'confirmed': d.confirmed,
'deaths': d.deaths
}))
Insert cell
data = cntry_data.concat(us_state_data)
Insert cell
// viewof timeSliderT = Inputs.range([0, 255], {step: 1, label:"Favorite number"})
Insert cell
parseTime = d3.timeParse("%Y-%m-%d");
Insert cell
all_dates = data[0]['confirmed'].map(x => parseTime(x[0]))
Insert cell
startDate = d3.timeDay(new Date(2020, 0, 22))
Insert cell
endDate = all_dates[all_dates.length - 1]
Insert cell
formatDate = d3.timeFormat("%Y-%m-%d")
Insert cell
viewof timeSliderT = rangeSlider({
min: 0,
max: d3.timeDay.count(startDate, all_dates[all_dates.length - 1]),
step: 1,
format: date => formatDate(d3.timeDay.offset(startDate, date)),
title: 'Adjust the date range as you need.'
})
Insert cell
viz_data = data.map(d => ({
'country_code': d.country_code,
'country_name': d.country_name,
'population': d.population,
'region': d.region,
'confirmed': d.confirmed.filter(d => new Date(d[0]) < currDateT)
}))
Insert cell
currDateT
Insert cell
viewof currDateT = Scrubber(d3.timeDays(d3.timeDay.offset(startDate, timeSliderT[0]), d3.timeDay.offset(startDate, timeSliderT[1] + 1)), {
autoplay: true,
loop: false,
alternate: false,
initial: 0,
format: date => date.toLocaleString("ko-KO", {year: "numeric", month: "numeric", day: "numeric"}),
delay: 500 - playSpeedT * 100,
})
Insert cell
Insert cell
params = ({
width: 800,
height: 500,
opac_weak: 0.5,
margin: ({top: 25, right: 35, bottom: 35, left: 60}),
xmin: 10,
xmax: 1e8,
ymin: 10,
ymax: 1e7,
});
Insert cell
color = d3.scaleOrdinal(cntry_data.filter(d => d.country_code != 'OWID_WRL').concat(us_state_data).map(d => d.region), ["#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd","#8c564b","#e377c2","#17becf"])
Insert cell
viewof yTypeT = radio({
title: 'Show',
options: [
{label: "Confirmed cases", value: 'confirmed' },
{label: "Deaths", value: 'deaths'}
],
value: 'confirmed'
});
Insert cell
x = d3.scaleLog([params.xmin, params.xmax], [params.margin.left, params.width - params.margin.right])
Insert cell
xAxis = g => g
.attr("transform", `translate(0, ${params.height - params.margin.bottom})`)
// how many ticks, or numbers, to show. the ',' i guess is to automatically add ',' in, say, 1000
.call(d3.axisBottom(x).ticks(10, ","))
.call(g => g.append("text")
.attr("font-size", 15)
.attr("x", params.width)
.attr("y", params.margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(`Total ${yTypeT} →`)
)
Insert cell
y = d3.scaleLog([params.ymin, params.ymax], [params.height - params.margin.bottom, params.margin.top])
Insert cell
showAxis = {
const svg = d3.create("svg").attr("width", params.width).attr("height", params.height)

svg.append("g").call(xAxis)

return svg.node()
}
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