Published
Edited
Jun 25, 2020
Insert cell
Insert cell
Insert cell
import {vl} from '@vega/vega-lite-api'
Insert cell
d3 = require('d3')
Insert cell
import {table} from '@tmcw/tables/2'
Insert cell
Insert cell
function removeState(d) {
delete d.state;
return d
}
Insert cell
toTitleCase = str => str[0].toUpperCase() + str.slice(1)
Insert cell
function getBarChart(yField) {
return vl.markBar()
.data(moStateData)
.transform(
vl.filter('month(datum.date) >= 2'),
vl.window(
vl.first_value(yField).as(`previous_${yField}`)
).frame(-1, 0),
vl.calculate(`datum.${yField} - datum.previous_${yField}`).as(`new_${yField}`)
)
.encode(
vl.y().field(`new_${yField}`).type('quantitative').title(toTitleCase(yField)),
vl.x().field('date').type('temporal').title('Date')
)
}
Insert cell
function getLineChart(yField) {
return vl.markLine({color: "red"})
.data(moStateData)
.transform(
vl.filter('month(datum.date) >= 2'),
vl.window(
vl.first_value(yField).as(`previous_${yField}`)
).frame(-1, 0),
vl.calculate(`datum.${yField} - datum.previous_${yField}`).as(`new_${yField}`),
vl.window(vl.mean(`new_${yField}`).as('rolling_mean')).frame(-6, 0)
)
.encode(
vl.y().field('rolling_mean').type('quantitative').title(toTitleCase(yField)),
vl.x().field('date').type('temporal').title('Date')
)
}
Insert cell
function getLayeredChart(yField) {
const barChart = getBarChart(yField);
const lineChart = getLineChart(yField);
return vl.layer(barChart, lineChart)
.title(`New ${toTitleCase(yField)} in Missouri (actual and 7-day average)`)
.height(400)
.width(600)
}
Insert cell
Insert cell
stateData = d3.csv('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv')
Insert cell
moStateData = stateData
.filter(d => d.state == 'Missouri')
.map(d => removeState(d))
Insert cell
table(moStateData)
Insert cell
getLayeredChart('cases').render()
Insert cell
getLayeredChart('deaths').render()
Insert cell
Insert cell
countyData = d3.csv('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv')
Insert cell
moCountyData = countyData
.filter(d => d.state == 'Missouri')
.map(d => removeState(d))
Insert cell
table(moCountyData)
Insert cell
countyNames = d3.map(moCountyData, d => d.county).keys().sort()
Insert cell
Insert cell
topoJSON = vl.topojson(
'https://raw.githubusercontent.com/stlpublicradio/dailygraphics-archive/master/2018/2018-11-06-us-sen-mo-map/mocountieskc.json'
).feature('mocountieskc')
Insert cell
countyData.filter(d => d.date == '2020-06-23')
Insert cell
vl.markGeoshape({stroke: '#aaa', strokeWidth: 0.25})
.data(topoJSON)
.transform(
vl.calculate("replace(datum.properties.NAMELSAD, ' County', '')").as("county_name_t"),
vl.lookup('county_name_t')
.from(
vl.data(countyData.filter(d => d.date == '2020-06-23'))
.key('county')
.fields(['cases', 'deaths'])
)
)
.encode(
vl.tooltip([
{type: 'nominal', field: 'county_name_t', title: 'County'},
{type: 'quantitative', field: 'cases', title: 'Cases'},
{type: 'quantitative', field: 'deaths', title: 'Deaths'},
]),
vl.color()
.fieldQ('cases')
.scale({scheme: "blues"})
.legend({title: "Cases"})
)
.project(vl.projection('albersUsa'))
.width(800)
.height(500)
.config({view: {stroke: null}})
.render()
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