Public
Edited
Feb 20, 2024
Paused
1 fork
Importers
13 stars
Insert cell
Insert cell
Insert cell
// An example date range, spanning the past 30 days.
dates = d3.utcDays(d3.utcDay.offset(d3.utcDay(), -30), d3.utcDay())
Insert cell
viewof range = offsetInterval(dates, {
label: "Date range",
formatValue: date => date.toISOString().slice(0, 10),
value: [ dates[2], dates[5] ]
})
Insert cell
range
Insert cell
viewof range2 = offsetInterval(["extra small", "small", "medium", "large", "extra large"], {
label: "Sizes",
})
Insert cell
range2
Insert cell
Insert cell
offsetInterval(dates, {
value: [ dates[10], dates[20] ],
format: ([a, b]) => htl.html`<span ${{
style: "display: flex; justify-content: space-between"
}}>
<span>${a.toISOString().slice(0, 10)}</span>
<span>${b.toISOString().slice(0, 10)}</span>
</span>`
})
Insert cell
Insert cell
function offsetInterval(values, {
value = [values[0], values[values.length - 1]],
formatValue = v => v,
format = ([a, b]) => `${formatValue(a)} … ${formatValue(b)}`,
...options
} = {}) {
if(new Set(values).size < values.length) throw Error("All values have to be unique.");
const valueof = i => values[i];
const indexof = v => values.indexOf(v);
const _format = i => formatValue(valueof(i));
const input = interval([0, values.length - 1], {
...options,
step: 1,
value: value.map(indexof),
format: v => format(v.map(valueof)),
});
return mapInputValue(input, indices => indices.map(valueof), dates => dates.map(indexof));
}
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