Public
Edited
May 10, 2023
Insert cell
Insert cell
t1=new Date("2023/10/05 13:00")
Insert cell
t2 = new Date("2023/10/05 12:00")
Insert cell
timedata = [
{ date: new Date("2023/10/05 13:00") },
{ date: new Date("2023/10/05 14:00") },
{ date: new Date("2023/10/05 15:00") },
{ date: new Date("2023/10/05 16:00") },
{ date: new Date("2023/10/05 17:00") },
{ date: new Date("2023/10/05 18:00") }
]
Insert cell
{
const diff = [];
for (var i = 0; i < timedata.length - 1; i++) {
console.log(timedata[i]);
diff.push({
difference: Math.floor(
(timedata[i + 1].date - timedata[i].date) / 1000 / 60
),
time: timedata[i].date
});
}
return diff;
}

Insert cell
Math.floor(((t1-t2)/1000)/60);
Insert cell
chart = {
const zoom = d3
.zoom()
.scaleExtent([1, 70])
.extent([
[margin.left, 0],
[width - margin.right, height]
])
.translateExtent([
[margin.left, -Infinity],
[width - margin.right, Infinity]
])
.on("zoom", zoomed);

const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

const clip = DOM.uid("clip");

svg
.append("clipPath")
.attr("id", clip.id)
.append("rect")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("width", width - margin.left - margin.right)
.attr("height", height - margin.top - margin.bottom);

const path = svg
.append("path")
.attr("clip-path", clip)
.attr("fill", "steelblue")
.attr("d", area(data, x));

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

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

svg
.call(zoom)
.transition()
.duration(750)
.call(zoom.scaleTo, 68, [x(Date.UTC(2001, 9, 11)), 0]);

function zoomed(event) {
const xz = event.transform.rescaleX(x);
path.attr("d", area(data, xz));
gx.call(xAxis, xz);
}

return svg.node();
}
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 30})
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = (g, x) => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = (g, y) => 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
area = (data, x) => d3.area()
.curve(d3.curveStepAfter)
.x(d => x(d.date))
.y0(y(0))
.y1(d => y(d.value))
(data)
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("flights.csv").text(), d3.autoType), {y: "Flights"})
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