Published
Edited
Jul 20, 2020
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.state_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.state_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.state_code}`)
.attr("font-size", 10)
.attr("font-family", "Helvetica, sans-serif")
.attr("class", d => `${d.state_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.state_code}`)
.attr("fill", d => color(d.state_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.state_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.state_code)
)
.slice(0)
.map(function(d) {
const values = d[ySelection].slice(0).map((x, idx) => {
if (idx === 0) {
return x[1]
} else {
return (x[1] - d[ySelection][idx - 1][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
file = "https://hackcovid.s3-us-west-2.amazonaws.com/data/data_new_cases.json"
Insert cell
data = Object.assign(await d3.json(file, d3.autoType), {y: "↑ Mortes por dia"})
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