Published
Edited
Jul 6, 2020
Fork of Deaths Chart
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("stroke-miterlimit", 1);
svg.append("g")
.call(xAxis)
.selectAll("text")
.attr("color", "gray")
.attr("font-size", 12);

svg.append("g")
.call(yAxis)
.selectAll("text")
.attr("color", "gray")
.attr("font-size", 18);

svg.append("path")
.attr("fill", "none")
.attr("stroke", "darkred")
.attr("stroke-width", 3)
.attr("d", line(data));
if (!mutable lookup) mutable lookup = new Date(data[0][0]);
const bar = svg
.append("line")
.attr("stroke", "#ccc")
.attr("stroke-width", 1.5)
.attr("y2", height)
.attr("x1", x(mutable lookup))
.attr("x2", x(mutable lookup));
const legendData = svg
.append("text")
.style("font", "30px sans-serif")
.attr("fill", "#ccc")
.attr("x", 10)
.attr("y", 100);
const legendR = svg
.append("text")
.style("font", "30px sans-serif")
.attr("fill", "#ccc")
.attr("x", 10)
.attr("y", 130);
const marker = svg
.append("circle")
.attr("r", 3)
.attr("cx", -100)
.attr("fill", "black")
.attr("fill-opacity", 0)//circle hidden
const formatTime = d3.utcFormat("%d/%m/%Y");
function update(date) {
const i = bisect.right(data, date);
if (i < data.length && i > 0) {
legendData.text(`${formatTime(new Date(data[i][0]))}`);
legendR.text(`Mortes: ${data[i][1]}`);
marker.attr("cx", x(new Date(data[i][0]))).attr("cy", y(data[i][1]));
bar.attr("stroke", "#ccc");
legendData.attr("x", (i > data.length/2)? x(new Date(data[i][0])) - 5 : x(new Date(data[i][0])))
legendData.attr("text-anchor", (i > data.length/2)? "end" : "start")
legendR.attr("x", (i > data.length/2)? x(new Date(data[i][0])) - 5 : x(new Date(data[i][0])))
legendR.attr("text-anchor", (i > data.length/2)? "end" : "start")
}
mutable lookup = new Date(date);
bar.attr("x1", x(mutable lookup)).attr("x2", x(mutable lookup));
//https://observablehq.com/@d3/index-chart --> line with range
}
svg.on("mousemove click touchmove", function() {
const m = d3.mouse(this);
update(x.invert(m[0]));
});
update(mutable lookup);
return svg.node();
}
Insert cell
mutable lookup = null
Insert cell
bisect = d3.bisector(d => new Date(d[0]))
Insert cell
// negativeColor = "darkred"
Insert cell
threshold = 1
Insert cell
file = "https://hackcovid.s3-us-west-2.amazonaws.com/data/data_new_cases.json"
Insert cell
data_raw = Object.assign(await d3.json(file, d3.autoType), {y: "↑ Mortes por dia"})
Insert cell
state = "AC"
Insert cell
data = data_raw.filter(d => d.state_code === state)[0].deaths.reverse()
Insert cell
line = d3.line()
.x(d => x(new Date(d[0])))
.y(d => y(d[1]))
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => new Date(d[0])))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear() //scaleLinear domain([-1, ]) //scaleLog() domain([1, ])
.domain([0, d3.max(data, d => d[1])])
.range([height - margin.bottom, margin.top])
.clamp(true)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 120).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(d => `${d}`))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.attr("id", "mobility-title")
.text(""))
.call(g => g.selectAll(".tick line")
.select(function() { return this.parentNode.appendChild(this.cloneNode()); })
.attr("stroke-opacity", 0.3)
.attr("stroke-width", 2)
.attr("stroke", "#ccc")
.attr("x2", width - margin.left - margin.right))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.attr("stroke-width", 0.5)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
height = 600
Insert cell
width = 600
Insert cell
margin = {
if (max_value > 100000) {
return ({top: 20, right: 30, bottom: 30, left: 75})
}
else if (max_value > 10000) {
return ({top: 20, right: 30, bottom: 30, left: 65})
} else {
return ({top: 20, right: 30, bottom: 30, left: 55})
}
}
Insert cell
max_value = d3.max(data, d => d[1])
Insert cell
d3.timeFormatDefaultLocale(locale);
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more