Published
Edited
Sep 30, 2019
3 forks
Importers
8 stars
Insert cell
md`# Matthias D3 | Simple bar-chart with tooltip-hover`
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, chart_width, chart_height]);

svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr( 'x', function( d, i ){
return x_scale(i);
})
.attr( 'y', function(d ){
return chart_height - y_scale( d );
})
.attr( 'width', x_scale.bandwidth() )
.attr( 'height', function( d ){
return y_scale( d );
})
.attr( 'fill', '#7ED26D' )
.on('mouseover', function(){
d3.select(this)
.transition()
.attr('fill', '#0C9CDF');
})
.on('mouseout', function(){
d3.select(this)
.transition()
.attr('fill', '#7ED26D');
})
.attr('fill', '#7ED26D')
.append('title')
.text(function(d){
return d;
})
return svg.node();
}
Insert cell
d3 = require("d3@5")

Insert cell
chart_width = 800;
Insert cell
chart_height = 400;
Insert cell
data = [6,20,10,8,11,30,45,35,26]
Insert cell
x_scale = d3.scaleBand()
.domain(d3.range(data.length))
.rangeRound([0, chart_width])
.paddingInner(0.05);
Insert cell
y_scale = d3.scaleLinear()
.domain([
0,
d3.max(data)
])
.range([0, chart_height]);

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