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
Insert cell
Insert cell
Insert cell
height = 600
Insert cell
margin = ({top: 70, right: 70, bottom: 70, left: 70})
Insert cell
Insert cell
x = d3.scaleLinear()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.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", "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
chart1 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
// domain for x and y.
x.domain([0,d3.max(covid_totals, d => d.cases)]).nice()
y.domain([0, d3.max(covid_totals, d => d.deaths)]).nice()
// 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', '14px')
.attr("fill", "currentColor")
.text("Number of Cases")
// 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', '14px')
.attr("fill", "currentColor")
.classed('rotation', true)
.attr('transform', 'translate(-40,700) rotate(-90)')
.text("Number of Deaths")
// title of the chart
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 Vs. Deaths Reported per Each Locality in VA');
// drawing the circles
svg.append("g")
.attr("stroke-width", 1)
.attr("fill", "none")
.selectAll("circle")
.data(covid_totals)
.join("circle")
.attr("cx", d => x(d.cases))
.attr("cy", d => y(d.deaths))
.attr('stroke', "teal")
.attr("r", 3)

return svg.node()
}
Insert cell
Insert cell
Insert cell
import {covid} from '@himarshaj/cs-725-825-spr21-hw-2-d3-intro'
Insert cell
Insert cell
Insert cell
data = d3.rollups(covid, v => [d3.sum(v, d => d.cases), d3.sum(v, d => d.hospitalizations)], d => d.date);
Insert cell
chart2_data = data.map(obj => {let rObj= {}; rObj["date"] = obj[0]; rObj["cases"] = obj[1][0];
rObj["hospitalizations"] = obj[1][1]; return rObj;})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart2 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("max-width", width);
// set the x and y domains based on our data
x.domain(d3.extent(chart2_data, d => d.cases)).nice()
y.domain(d3.extent(chart2_data, d => d.hospitalizations)).nice()
// 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', '14px')
.attr("fill", "currentColor")
.text("Number of Cases")
// 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', '14px')
.attr("fill", "currentColor")
.classed('rotation', true)
.attr('transform', 'translate(-45,700) rotate(-90)')
.text("Number of Hospitalizations")
// draw the circles
svg.append("g")
.attr("stroke-width", 1)
.attr("fill", "none")
.selectAll("circle")
.data(chart2_data)
.join("circle")
.attr("cx", d => x(d.cases))
.attr("cy", d => y(d.hospitalizations))
.attr('stroke', "teal")
.attr("r", 3);
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 Vs. Hospitalizations in VA over Time');

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