Published
Edited
Feb 16, 2021
Insert cell
Insert cell
Insert cell
{
const formatTime = d3.utcFormat("%d/%m/%Y");
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("g")
.attr("fill", color)
.selectAll("rect")
.data(obitsPerDay100)
.enter()
.append("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(d.obits))
.attr("height", d => y(0) - y(d.obits))
.attr("width", x.bandwidth())
.on('mouseover', function(ev, d) {
tooltip
.html(
`<div>Data: ${formatTime(d.date)} </div><div>Óbitos: ${d3.format(
".2f"
)(d.obits)}</div>`
)
.style('visibility', 'visible');
d3.select(this)
.transition()
.attr('fill', hoverColor);
})
.on('mousemove', function(ev, d) {
tooltip
.style('top', 100 + 'px')
.style('left', Math.min(width - 120, ev.pageX + 10) + 'px');
})
.on('mouseout', function(ev, d) {
tooltip.html(``).style('visibility', 'hidden');
d3.select(this)
.transition()
.attr('fill', color);
});
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
height = 500
Insert cell
margin = ({ top: 30, right: 0, bottom: 30, left: 40 })
Insert cell
Insert cell
y = d3
.scaleLinear()
.domain([0, d3.max(obitsPerDay100, d => d.obits)])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
color = "steelblue"
Insert cell
hoverColor = '#eec42d'
Insert cell
staticColor = '#437c90'
Insert cell
d3 = require("d3@6")
Insert cell
tooltip = d3
.select('body')
.append('div')
.attr('class', 'd3-tooltip')
.style('position', 'absolute')
.style('z-index', '10')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff')
.text('a simple tooltip')
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