Published
Edited
Feb 26, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.rollups(covid, v => [d3.sum(v, d => d.cases)], d => d.date)
Insert cell
chart3_data = data.map(obj => {let rObj= {}; rObj["date"] = obj[0]; rObj["cases"] = obj[1][0]; return rObj;})
Insert cell
chart3_data.sort((a, b) => a.date - b.date)
Insert cell
Insert cell
Insert cell
Insert cell
height = 700
Insert cell
margin = ({top: 70, right: 70, bottom: 70, left: 70})
Insert cell
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(chart3_data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(chart3_data, d => d.cases)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.style("font", "11px times")
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.style("font", "11px times")
Insert cell
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.cases))
Insert cell
Insert cell
chart3 = {
const svg = d3.create('svg')
.attr("viewBox", [0, 0, width, height]);
// setup the x-axis and label
svg.append('g')
.call(xAxis)
.append("text")
.attr("x", width/2)
.attr("y", 40)
.attr('font-weight',"bold")
.attr('font-family', 'sans-serif')
.attr('font-size', '16px')
.attr("fill", "currentColor")
.text("Date")
// setup the y-axis and label
svg.append('g')
.call(yAxis)
.append("text")
.attr("x", width/2)
.attr("y", 0)
.attr('font-weight',"bold")
.attr('font-family', 'sans-serif')
.attr('font-size', '16px')
.attr("fill", "currentColor")
.classed('rotation', true)
.attr('transform', 'translate(-55,700) rotate(-90)')
.text("Total Number of Cases")
svg.append('text')
.attr('x', width / 2)
.attr('y', 30)
.attr('font-family', 'sans-serif')
.attr('font-size', '24px')
.attr('font-weight',"bold")
.attr('text-anchor', 'middle')
.attr('dominant-baseline', 'hanging')
.text('Total Number of Cases in VA over Time');
// draw the path, we use the covid as the dataset.
svg.append('path')
.datum(chart3_data)
.style('fill', 'none')
.style('stroke', 'teal')
.style('stroke-width', '3')
.attr('d', line)

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