Published
Edited
Apr 17, 2020
1 fork
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

let filterKey = "Uitgifte"
let groupKey = "Model"
let filterKeys = Array.from(new Set(data.map((item) => {return item[filterKey]}) ) )
let x0 = d3.scaleBand()
.domain(data.map(d => d[groupKey]))
.rangeRound([margin.left, width - margin.right])
.paddingInner(0.1)
let x1 = d3.scaleBand()
.domain(filterKeys)
.rangeRound([0, x0.bandwidth()])
.padding(0.05)
let xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x0).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
const bar = svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x0(d[groupKey]) + x1(d[filterKey]))
.attr("y", d => y(d.Value))
.attr("width", x1.bandwidth())
.attr("height", d => y(0) - y(d.Value))
.attr("fill", d => color(d.Model));

const gx = svg.append("g")
.call(xAxis);

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

svg.append("g")
.call(legend);

return Object.assign(svg.node(), {
update(order) {
x0.domain(data.map(d => d[order.groupKey]))
const keys = Array.from(new Set(data.map((item) => {return item[order.filterKey]}) ) )
x1 = d3.scaleBand()
.domain(keys)
.rangeRound([0, x0.bandwidth()])
.padding(0.05)
let xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x0).tickSizeOuter(0))
.call(g => g.select(".domain").remove())
const t = svg.transition()
.duration(750);

bar.data(data)
.order()
.transition(t)
.delay((d, i) => i * 25)
.attr("x", d => x0(d[order.groupKey]) + x1(d[order.filterKey]));

gx.transition(t)
.call(xAxis)
.selectAll(".tick")
.delay((d, i) => i * 20);
}
});
}
Insert cell
chart.update(order)
Insert cell
legend = svg => {
const g = svg
.attr("transform", `translate(${width},0)`)
.attr("text-anchor", "end")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(color.domain().slice().reverse())
.join("g")
.attr("transform", (d, i) => `translate(0,${i * 20})`);

g.append("rect")
.attr("x", -19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", color);

g.append("text")
.attr("x", -24)
.attr("y", 9.5)
.attr("dy", "0.35em")
.text(d => d);
}
Insert cell
y.domain()
Insert cell
y = d3.scaleLinear()
.domain([0, 1.4 * d3.max(data, d => d.Value )]).nice()
.rangeRound([height - margin.bottom, margin.top])
Insert cell
color = d3.scaleOrdinal()
.range(["#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"])
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
Insert cell
margin = ({top: 10, right: 10, bottom: 20, left: 40})
Insert cell
height = 500
Insert cell
d3 = require("d3@5")
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