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
chart4_data = {
const data = d3.rollup(hr_covid,
cases => d3.sum(cases, c => c.cases),
d => d.district,
d => d3.timeDay(d.date));
return Array.from(data, ([district, day]) => ({
district: district,
info: Array.from(day, ([date, cases]) => ({date, cases}))
}));
}
Insert cell
hr_covid.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(hr_covid, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(hr_totals, d => d.cases)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
color = d3.scaleOrdinal()
.domain(d3.extent(chart4_data, d => d.district))
.range(d3.schemeTableau10);
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
chart4 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const g = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
svg.append("g")
.attr('transform', `translate(0, ${height - margin.bottom})`)
.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")
svg.append("g")
.attr('transform', `translate(${margin.left}, 0)`)
.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("Number of Cases")
svg.append('text')
.attr('x', width / 2)
.attr('y', 30)
.attr('font-family', 'sans-serif')
.attr('font-size', '14px')
.attr('font-weight',"bold")
.attr('text-anchor', 'middle')
.attr('dominant-baseline', 'hanging')
.text('Total Cases over Time for Western Tidewater, Chesapeake, Virginia Beach, Norfolk, Portsmouth, Hampton, Peninsula Health Districts');
const line_set = svg.append('g')
.selectAll('g')
.data(chart4_data)
.join('g')
.attr('stroke', d => color(d.district))
.append('path')
.datum(d => d.info)
.attr('fill', 'none')
.attr('stroke-width', 2)
.attr('d', line);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
import {aq, op} from '@uwdata/arquero'
Insert cell
import {hr_totals} from '@himarshaj/cs-725-825-spr21-hw-2-d3-intro'
Insert cell
Insert cell
Insert cell
chart6_data = aq.from(hr_totals)
.groupby('district')
.rollup({ total: d => op.sum(d.cases) })
.orderby('district')
.select({ district: 'key', total: 'value' })
.objects()
.sort(function (b, a) {return b.value - a.value;});
Insert cell
Insert cell
Insert cell
Insert cell
height2 = 600
Insert cell
margin2 = ({top: 70, right: 70, bottom: 70, left: 110})
Insert cell
Insert cell
x2 = d3.scaleLinear()
.domain([0, d3.max(chart6_data, d => d.value)]).nice()
.range([margin2.left, width - margin2.right])
Insert cell
y2 = d3.scaleBand()
.domain(chart6_data.map(d => d.key))
.range([height2 - margin2.bottom, margin2.top])
.padding(0.05)
Insert cell
Insert cell
xAxis2 = g => g
.attr('transform', `translate(0, ${height2 - margin2.bottom})`)
.call(d3.axisBottom(x2).ticks(width / 80))
.style("font", "11px times")
Insert cell
yAxis2 = g => g
.attr('transform', `translate(${margin2.left -2}, 0)`)
.call(d3.axisLeft(y2))
.style("font", "11px times")
Insert cell
Insert cell
chart6 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height2]);
const g = svg.append('g')
.attr('transform', `translate(${margin2.left}, ${margin2.top})`);
svg.append("g")
.call(xAxis2)
.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("Number of Cases")
svg.append("g")
.call(yAxis2)
.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")
.classed('rotation', true)
.attr('transform', 'translate(-55,700) rotate(-90)')
.text("Health District")
svg.selectAll('rect')
.data(chart6_data)
.join('rect')
.attr('x', margin2.left)
.attr('y', d => y2(d.key))
.attr('width', d => x2(d.value)- x2(margin2.left))
.attr('height', d => y2.bandwidth()) // <-- Band scales split a pixel range into equal-sized bands
.style('fill', d => color(d.key))
// title
svg.append('text')
.attr('x', width / 2)
.attr('y', 30)
.attr('font-family', 'sans-serif')
.attr('font-size', '15px')
.attr('font-weight',"bold")
.attr('text-anchor', 'middle')
.attr('dominant-baseline', 'hanging')
.text('Total Cases for Western Tidewater, Chesapeake, Virginia Beach, Norfolk, Portsmouth, Hampton, Peninsula Health Districts');
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