Published unlisted
Edited
Apr 5, 2021
Insert cell
Insert cell
viewof focus = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, focusHeight])
.style("display", "block");

const brush = d3.brushX()
.extent([[margin.left, 0.5], [width - margin.right, focusHeight - margin.bottom + 0.5]])
.on("brush", brushed)
.on("end", brushended);

const defaultSelection = [x(d3.timeHour.offset(x.domain()[0], 0)), x.range()[1]];

svg.append("g")
.call(xAxis, x, focusHeight);

svg.append("path")
.datum(data)
.attr("fill", "steelblue")
.attr("d", area(x, y.copy().range([focusHeight - margin.bottom, 4])));

const gb = svg.append("g")
.call(brush)
.call(brush.move, defaultSelection);

function brushed({selection}) {
if (selection) {
svg.property("value", selection.map(x.invert, x).map(d3.timeHour.round));
svg.dispatch("input");
}
}

function brushended({selection}) {
if (!selection) {
gb.call(brush.move, defaultSelection);
}
}

return svg.node();
}
Insert cell
Insert cell
bisect = {
const bisect = d3.bisector(d => parseDate(d.date)).left;
return mx => {
const date = x.invert(mx);
const index = bisect(data, date, 1);
const a = data[index - 1];
const b = data[index];
return b && (date - a.date > b.date - date) ? b : a;
};
}
Insert cell
formatDate2 = d3.timeFormat("%H:%M:%S")
Insert cell
formatDate = d3.timeFormat("%Y-%m-%d %H:%M:%S")
Insert cell
parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S")
Insert cell
callout = (g, value) => {
if (!value) return g.style("display", "none");

g
.style("display", null)
.style("pointer-events", "none")
.style("font", "10px sans-serif");

const path = g.selectAll("path")
.data([null])
.join("path")
.attr("fill", "white")
.attr("stroke", "black");

const text = g.selectAll("text")
.data([null])
.join("text")
.call(text => text
.selectAll("tspan")
.data((value + "").split(/\n/))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => `${i * 1.1}em`)
.style("font-weight", (_, i) => i ? null : "bold")
.text(d => d));

const {x, y, width: w, height: h} = text.node().getBBox();

text.attr("transform", `translate(${-w / 2},${15 - y})`);
path.attr("d", `M${-w / 2 - 10},5H-5l5,-5l5,5H${w / 2 + 10}v${h + 20}h-${w + 20}z`);
}
Insert cell
update = {
const [minX, maxX] = focus;
const maxY = d3.max(data, d => minX <= parseDate(d.date) && parseDate(d.date) <= maxX ? d.value : NaN);
chart.update(x.copy().domain(focus), y.copy().domain([0, maxY]));
}
Insert cell
data = {
const earliestStartDate = d3.min(candidates.map(d => parseDate(d.start)))
const lastDate = d3.max(candidates.map(d => parseDate(d.end)))
let dates = d3.scaleTime()
.domain([d3.timeHour.offset(earliestStartDate, -0), lastDate])
.ticks(d3.timeHour.every(1))
.map(d => formatDate(d))
const datesAndCounts = dates.map(d => {
let c = 0
for (let i = 0; i < candidates.length; i++) {
const cStart = candidates[i].start
const cEnd = candidates[i].end
if ((parseDate(cStart) <= parseDate(d)) && (parseDate(cEnd) > parseDate(d) || cEnd === "")) {
c++
}
}
return {date: d, value: c}
})
return datesAndCounts
}
Insert cell
candidates = [
{name: "nome1", start: "2019-09-01 09:00:12", end: "2019-09-02 15:20:13"},
{name: "nome2", start: "2019-09-01 05:10:33", end: "2019-09-01 12:40:15"},
{name: "nome3", start: "2019-09-01 01:30:11", end: "2019-09-01 18:15:23"},
{name: "nome2", start: "2019-09-01 08:30:33", end: "2019-09-01 10:20:15"},
{name: "nome3", start: "2019-09-01 00:30:11", end: "2019-09-01 05:15:23"},
{name: "nome2", start: "2019-09-01 05:10:33", end: "2019-09-01 12:40:15"},
{name: "nome3", start: "2019-08-31 22:30:11", end: "2019-09-01 01:10:23"},
{name: "nome2", start: "2019-08-31 23:30:33", end: "2019-09-01 10:50:15"},
{name: "nome3", start: "2019-08-31 20:10:11", end: "2019-09-01 03:3:23"}
]
Insert cell
area = (x, y) => d3.area()
.defined(d => !isNaN(d.value))
.x(d => x(parseDate(d.date)))
.y0(y(0))
.y1(d => y(d.value))
.curve(d3.curveStepAfter)
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => parseDate(d.date)))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = (g, x, height) => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = (g, y, title) => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".title").data([title]).join("text")
.attr("class", "title")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(title))
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 40})
Insert cell
height = 440
Insert cell
focusHeight = 100
Insert cell
d3 = require("d3@6")
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