Published
Edited
Mar 31, 2021
Insert cell
Insert cell
Insert cell
Insert cell
import {Select} from "@observablehq/inputs"
Insert cell
select_Variable
Insert cell
Insert cell
select_States
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
const g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

let path = svg.selectAll("path.dataline")
.data(select_States)
.enter()
.append("path")
.attr("class", "dataline")
.attr("fill", "none")
.attr("stroke", d => colors(d))
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", state => line(casesByState.get(state)));

const legend = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.attr("transform", "translate(0,20)")
.selectAll("g")
.data(select_States)
.enter().append("g")
.attr("transform", function(d, i) {
return "translate(0," + i * 20 + ")";
});

legend.append("rect")
.attr("x", 10)
.attr("width", 18)
.attr("height", 18)
.attr("fill", d => colors(d));

legend.append("text")
.attr("x", 35)
.attr("y", 12)
.attr("dy", "0.32em")
.attr("text-anchor", "start")
.text(function(d) {
return d;
});

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
casesByState.get('Michigan')[0].date
Insert cell
d3.utcParse("%Y-%m-%d")
Insert cell
casesByState = d3_array.group(data, row => row['state'])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
line = d3.line()
.defined(d => !isNaN(d[select_Variable]))
.x(d => x(d.date))
.y(d => y(d[select_Variable]))
Insert cell
line([{date: new Date('2021-01-01'), cases: 300}])
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