Published
Edited
Jul 19, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function onMouseOver(selected) {
const gNodes = d3.selectAll("svg > g").nodes();
d3.select(gNodes[2])
.selectAll("*")
.attr("stroke-opacity", params.opacityOut);
d3.select(gNodes[3])
.selectAll("*")
.attr("fill-opacity", params.opacityOut);
d3.select(gNodes[4])
.selectAll("*")
.attr("fill-opacity", params.opacityOut);

// http://bl.ocks.org/mstanaland/6106487
// formatComma = d3.format(",")
// body.append("p").text(function() { return 'd3.format(",") : ' + formatComma(number); });

const code = d3.select(this).attr("class");
d3.selectAll(`.${code}`)
// .text(d => `${d.code}: ${Math.round(d.yValues[d.yValues.length - 1])}`)
.text(d => `${d.code}: ` + d3.formatLocale(pt_BR).format(",d")(d.yTotal))
.attr("fill-opacity", params.opacityHover)
.attr("stroke-opacity", params.opacityHover)
.attr("stroke-width", 3)
.attr("r", 3);
}
Insert cell
function onMouseOut(selected) {
const code = d3.select(this).attr("class");

d3.selectAll(`.${code}`)
.text(d => `${d.code}`)
.attr("stroke-width", 1)
.attr("r", 3);

d3.selectAll(`path`).attr("stroke-opacity", params.opacitySteady);
d3.selectAll(`circle`).attr("fill-opacity", params.opacitySteady);
d3.selectAll(`text`).attr("fill-opacity", params.opacitySteady);
}
Insert cell
Insert cell
function drawTooltips(svg) {
svg
.append("g")
.selectAll("text")
.data(chartData)
.join("text")
.text(d => `${d.code}`)
.attr("font-size", 10)
.attr("font-family", "Helvetica, sans-serif")
.attr("class", d => `${d.code}`)
.attr("fill", d => color(d.region))
.attr("fill-opacity", params.opacitySteady)
.attr("x", d => d3.min([x(d.yValues.length - 1) + 4, x(params.xmax) - 10]))
.attr("y", d => y(d.yValues[d.yValues.length - 1]))
.on("mouseover", onMouseOver)
.on("mouseout", onMouseOut);
}
Insert cell
function drawCircles(svg) {
svg
.append("g")
.selectAll("circle")
.data(chartData)
.join("circle")
.attr("class", d => `${d.code}`)
.attr("fill", d => color(d.region))
.attr("fill-opacity", params.opacitySteady)
.attr("cx", d => x(d.yValues.length - 1))
.attr("cy", d => y(d.yValues[d.yValues.length - 1]))
.attr("r", 3)
.on("mouseover", onMouseOver)
.on("mouseout", onMouseOut);
}
Insert cell
function drawLines(svg) {
svg
.append("g")
.selectAll("path")
.data(chartData)
.join("path")
.attr("class", d => `${d.code}`)
.attr("stroke", d => color(d.region))
.attr("fill", "none")
.attr("stroke-width", 1)
.attr("stroke-opacity", params.opacitySteady)
.attr("d", d => getLinePath(d.yValues))
.on("mouseover", onMouseOver)
.on("mouseout", onMouseOut);
}
Insert cell
function getLinePath(d) {
const path = d3.line();
return path(d.map(function(e, i) { return [x(i), y(e)] }));
}
Insert cell
Insert cell
color = d3
.scaleOrdinal(data.map(d => d.region), d3.schemeCategory10)
.unknown("black")
Insert cell
params = ({
width: 600,
height: 400,
ticksScale: 80,
margin: { top: 30, right: 80, bottom: 45, left: 40 },
yMin: 0,
yMax: roundToNearest(getMax(chartData.map(i => getMax(i.yValues))), 200),
xMin: 0,
xMax: getMax(chartData.map(i => i.yValues.length)),
opacitySteady: 0.6,
opacityHover: 1.0,
opacityOut: 0.1
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${params.margin.left},0)`)
.call(
d3
.axisLeft(y)
.ticks(params.height / params.ticksScale, ",")
.tickFormat(d3.formatLocale(pt_BR).format(",d"))
)
.call(g =>
g
.append("text")
.attr("font-size", 12)
.attr("x", -params.margin.left)
.attr("y", params.margin.top - 15)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(`${yLabels.filter(l => l.value == `${ySelection}`)[0].label}`)
)
Insert cell
//y = d3.scaleLog([params.ymin, params.ymax], [params.height - params.margin.bottom, params.margin.top])
y = d3
.scaleLinear()
.domain([params.yMin, params.yMax])
.range([params.height - params.margin.bottom, params.margin.top])
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${params.height - params.margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / params.ticksScale))
.call(g =>
g
.append("text")
.attr("x", params.width)
.attr("y", params.margin.bottom - 5)
.attr("font-size", 12)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(`Dias (desde o primeiro caso confirmado)`)
)
Insert cell
x = d3
.scaleLinear()
.domain([params.xMin, params.xMax])
.range([params.margin.left, params.width - params.margin.right])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chartData = data
.filter(
d => regionSelection.includes(d.region) && stateSelection.includes(d.code)
)
.slice(0)
.map(function(d) {
const values = d[ySelection].slice(0).map(d => d[1]);

d.yTotal = values.reduce((acc, i) => (acc += i), 0);

if (yRawOrRolling === "yRaw") {
d.yValues = values;
} else if (yRawOrRolling === "y7dRollAvg") {
d.yValues = rollingAvg(values, 7);
}

return d;
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = dataCsv.slice(0).reduce((acc, obj) => {
if (obj.date > updates[obj.state]) return acc; // ignore csv lines with dates posterior to the latest update

let idx = acc.map(i => i.code).indexOf(obj.state);
if (idx == -1) return acc; // ignore eventual csv lines without one of the 27 Brazilian states

let dateIdx = acc[idx]["confirmed"].map(i => i[0]).indexOf(obj.date);

if (dateIdx >= 0) {
acc[idx]["confirmed"][dateIdx][1] += parseInt(obj.new_confirmed);
acc[idx]["deaths"][dateIdx][1] += parseInt(obj.new_deaths);
} else {
acc[idx]["confirmed"].push([obj.date, parseInt(obj.new_confirmed)]);
acc[idx]["deaths"].push([obj.date, parseInt(obj.new_deaths)]);
}

return acc;
}, dataTemplate)
Insert cell
dataCsv = d3.csvParse(pako.inflate(gzData.data, { to: 'string' }))
Insert cell
gzData = axios({
method: 'get',
url: 'https://data.brasil.io/dataset/covid19/caso_full.csv.gz',
responseType: 'arraybuffer'
})
// data source: https://data.brasil.io/dataset/covid19/_meta/list.html

// references / tips
// https://github.com/googleapis/google-api-nodejs-client/issues/1777
// https://stackoverflow.com/questions/49040247/download-binary-file-with-axios#comment98082293_50220546
// https://github.com/axios/axios/issues/448
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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