Published
Edited
Jan 15, 2020
Insert cell
Insert cell
timelines = [
{ start: 0, end: 20 },
{ start: 9, end: 16 },
{ start: 4, end: 30 },
{ start: 8, end: 21 },
{ start: 15, end: 30 },
{ start: 15, end: 30 },
{ start: 17, end: 30 },
{ start: 27, end: 40 },
{ start: 3, end: 21 },
{ start: 8, end: 43 },
{ start: 20, end: 23 },
{ start: 18, end: 24 },
]
Insert cell
chart = {
const height = 100;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 600, height])
.attr("height", 200);
const g = svg.append('g');
const yScale = (x) => (height - x);
sortedTimelines.forEach((timeline, timelineIndex)=> {
for (let t = timeline.start; t <= timeline.end; t++) {
const t0 = t;
const t1 = t+1;
const height0 = heightAtTime(timelineIndex, t0);
const height1 = heightAtTime(timelineIndex, t1);
const y0 = yAtTime(timelineIndex, t0);
const y1 = yAtTime(timelineIndex, t1);
const x0 = DT_WIDTH*t0;
const x1 = DT_WIDTH*t1;
const d = `M ${x0} ${yScale(y0)} L ${x0} ${yScale(y0+height0)} L ${x1} ${yScale(y1+height1)} L ${x1} ${yScale(y1)} z`;
g.append('path')
.attr("d", d)
.attr('fill', d3.interpolateCool(timelineIndex/(sortedTimelines.length-1)));
}
});

return svg.node();
}
Insert cell
Insert cell
function weightAtTime(timelineIndex, time) {
const timeline = sortedTimelines[timelineIndex];
const { start, end } = timeline;
const MAX = 4;
if (time < start || time > end) return 0;
const easing = (end-start < MAX*2) ? Math.floor((end-start)/2) : MAX;
const startEase = d3.easePolyOut((time - start)/easing, 3);
const endEase = d3.easePolyOut((end - time)/easing, 3);
const max = d3.easePolyOut(1-(MAX-easing)/MAX, 3);
if (time - start <= easing && end - time <= easing) return Math.min(startEase, endEase) * max;
if (time - start <= easing) return startEase * max;
if (end - time <= easing) return endEase * max;
return max;
}
Insert cell
Insert cell
Insert cell
Insert cell
function heightAtTime(timelineIndex, time) {
const weight = weightAtTime(timelineIndex, time);
const weightAbove = weightAtTimeAboveTimeline(timelineIndex, time);
const coef = 1 - (weightAbove / 20);
const height = Math.round(weight * LINE_HEIGHT * coef);
return height;
}
Insert cell
function yAtTime(timelineIndex, time) {
const indexes = timelineIndexesAtTimeBelowTimeline(timelineIndex, time);
const beforeHeight = indexes.reduce((totalHeight, timelineIndex) => {
return totalHeight + heightAtTime(timelineIndex, time);
}, V_GAP * indexes.length);
return beforeHeight;
}
Insert cell
function timelineIndexesAtTime(time) {
return sortedTimelines.reduce((collector, timeline, index) => {
if (timeline.start <= time && timeline.end >= time) {
collector.push(index);
}
return collector;
}, []);
}
Insert cell
function timelineIndexesAtTimeBelowTimeline(timelineIndex, time) {
const indexes = timelineIndexesAtTime(time);
return indexes.filter(index => index < timelineIndex);
}
Insert cell
function timelineIndexesAtTimeAboveTimeline(timelineIndex, time) {
const indexes = timelineIndexesAtTime(time);
return indexes.filter(index => index > timelineIndex);
}
Insert cell
function weightAtTimeAboveTimeline(timelineIndex, time) {
const indexes = timelineIndexesAtTimeAboveTimeline(timelineIndex, time);
return indexes.reduce(((collector, index) => collector + weightAtTime(index, time)), 0);
}
Insert cell
d3 = require("d3@5");
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