Published
Edited
Jan 6, 2021
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const rect = svg.selectAll("g")
.data(y01z)
.join("g")
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("fill", (d, i) => z(d, i))
.attr("stroke", "grey")
.attr("x", (d, i) => x(i))
.attr("y", height - margin.bottom)
.attr("width", x.bandwidth())
.attr("height", 0);

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

function transitionStacked() {
y.domain([0, y1Max]);

rect.transition()
.duration(500)
.delay((d, i) => i * 20)
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.transition()
.attr("x", (d, i) => x(i))
.attr("width", x.bandwidth());
}

function update() {
transitionStacked();
}

return Object.assign(svg.node(), {update});
}
Insert cell
update = chart.update()
Insert cell
xz = d3.range(m) // the x-values shared by all series
Insert cell
yz = sortBumps() // the y-values of each of the n series
Insert cell
function sortBumps() {
let sortedArr = Array.from(Array(n), () => new Array(m))
let arr = d3.range(n).map(() => {
return bumps(m)
})
console.log(arr)
for (let i = 0; i < m; i++) {
arr.sort((a,b) => {
return b[i]-a[i]
});
for (let ii = 0; ii < n; ii++) {
sortedArr[ii][i] = arr[ii][i]
}
}
return sortedArr
}
Insert cell
y01z = d3.stack()
.keys(d3.range(n))
(d3.transpose(yz)) // stacked yz
.map((data, i) => {
//console.log(data)
return data.map(([y0, y1]) => [y0, y1, i]
)
})
Insert cell
yMax = d3.max(yz, y => d3.max(y))
Insert cell
y1Max = d3.max(y01z, y => d3.max(y, d => d[1]))
Insert cell
x = d3.scaleBand()
.domain(xz)
.rangeRound([margin.left, width - margin.right])
.padding(0.00)
Insert cell
y = d3.scaleLinear()
.domain([0, y1Max])
.range([height - margin.bottom, margin.top])
Insert cell
colorsArr = [d3.interpolateReds, d3.interpolateBlues, d3.interpolateGreens, d3.interpolateOranges]
Insert cell
z = (d, i) => {
return colorsArr[i](0.9 - d[2]/m)
}
Insert cell
xAxis = svg => svg.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0).tickFormat(() => ""))
Insert cell
trueN = n
Insert cell
n = 4 // number of series
Insert cell
m = 4 // number of values per series
Insert cell
height = 500
Insert cell
margin = ({top: 0, right: 0, bottom: 10, left: 0})
Insert cell
// Returns an array of m psuedorandom, smoothly-varying non-negative numbers.
// Inspired by Lee Byron’s test data generator.
// http://leebyron.com/streamgraph/
function bumps(m) {
const values = [];

// Initialize with uniform random values in [0.1, 0.2).
for (let i = 0; i < m; ++i) {
values[i] = 0.1 + 0.1 * Math.random();
}

// Add five random bumps.
for (let j = 0; j < 5; ++j) {
const x = 1 / (0.1 + Math.random());
const y = 2 * Math.random() - 0.5;
const z = 10 / (0.1 + Math.random());
for (let i = 0; i < m; i++) {
const w = (i / m - y) * z;
values[i] += x * Math.exp(-w * w);
}
}

// Ensure all values are positive.
for (let i = 0; i < m; ++i) {
values[i] = Math.max(0, values[i]);
}

return values;
}
Insert cell
d3 = require("d3@6")
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