Published
Edited
Feb 26, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
norfolk = covid.filter(d => d.district="Norfolk")
Insert cell
data = d3.rollups(norfolk, v => [d3.sum(v, d => d.cases)], d => d.date)
Insert cell
chart5_data = data.map(obj => {let rObj= {}; rObj["date"] = obj[0]; rObj["cases"] = obj[1][0]; return rObj;})
Insert cell
chart5_data.sort((a, b) => a.date - b.date)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(chart5_data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(chart5_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 / 70))
.style("font", "12px times")
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.style("font", "12px times")
Insert cell
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.cases))
Insert cell
Insert cell
chart5 = {
const svg = d3.create('svg')
.attr("viewBox", [0, 0, width, height]);
// Drawing the title
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 Cases Over Time: Norfolk District');
// Creatig 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") ;
// Creatig the y-axis and label
svg.append('g')
.call(yAxis)
.append("text")
.attr("x", 500)
.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")
// Drawing the line chart using chart5_data
svg.append('path')
.datum(chart5_data)
.style('fill', 'none')
.style('stroke', 'teal')
.style('stroke-width', '3')
.style('stroke-miterlimit', '1')
.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