Published
Edited
Jun 22, 2022
1 fork
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));
var x = d3.scaleBand()
.domain(data.map(d => d.sendsPerWeek))
.range([margin.left, width - margin.right]);
var xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0));
var y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.contactCount)]).nice()
.range([height - margin.bottom, margin.top]);

var yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickFormat(d3.format(",")));

//TODO: Add a select to choose colors?
//var colorScheme = d3.interpolateBlues;
var colorScheme = d3.interpolateRdBu;

//TODO: Add a select to choose which rate to show.
var selectedRate = 'unresponsiveRate';
//var selectedRate = 'scannerRate';
//var selectedRate = 'botRate';
//var selectedRate = 'bounceRate';
//Draw bars
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("fill", d => colorScheme(1- d[selectedRate])) //<<< "1 - " reverses the color scale.
.attr("x", d => x(d.sendsPerWeek))
.attr("y", d => y(d.contactCount))
.attr("height", d => y(0) - y(d.contactCount))
.attr("width", x.bandwidth());

svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);

return svg.node();
}
Insert cell
Insert cell
data = [{
sendsPerWeek: 0,
contactCount: 10000,
unresponsiveRate: 1.0,
scannerRate: 0.01,
botRate: .02,
bounceRate: 0.01
},
{
sendsPerWeek: 1,
contactCount: 9765,
unresponsiveRate: .65,
scannerRate: 0.02,
botRate: .02,
bounceRate: 0.08
},
{
sendsPerWeek: 2,
contactCount: 8402,
unresponsiveRate: .45,
scannerRate: 0.03,
botRate: .01,
bounceRate: 0.05
},
{
sendsPerWeek: 3,
contactCount: 7753,
unresponsiveRate: .25,
scannerRate: 0.04,
botRate: .03,
bounceRate: 0.04
}]
Insert cell
Insert cell
Insert cell
Insert cell
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