Public
Edited
Aug 1, 2023
21 forks
Importers
24 stars
Insert cell
Insert cell
Insert cell
chart = {
// Specify the chart’s dimensions.
const width = 928;
const height = 500;
const marginTop = 20;
const marginRight = 12;
const marginBottom = 30;
const marginLeft = 30;

// Create the temporal scale.
const x = d3.scaleTime()
.domain(d3.extent(dates))
.range([marginLeft, width - marginRight]);

// Count the events by day.
const bins = d3.bin()
.domain(x.domain())
.thresholds(x.ticks(d3.timeDay))
(dates);

// Apply the moving-average transform.
const values = movingAverage(bins.map(d => d.length), N);

// Create the vertical scale.
const y = d3.scaleLinear()
.domain([0, d3.max(values)]).nice()
.rangeRound([height - marginBottom, marginTop]);

// Create the container SVG.
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");

// Append the axes.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0));
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width)
.attr("stroke-opacity", 0.1));

// Append the area. Ignore invalid values due to an incomplete window
// at the start of the time period.
const area = d3.area()
.defined(d => !isNaN(d))
.x((d, i) => x(bins[i].x0))
.y0(y(0))
.y1(y);

svg.append("path")
.attr("fill", "steelblue")
.attr("d", area(values));

return svg.node();
}
Insert cell
function movingAverage(values, N) {
let i = 0;
let sum = 0;
const means = new Float64Array(values.length).fill(NaN);
for (let n = Math.min(N - 1, values.length); i < n; ++i) {
sum += values[i];
}
for (let n = values.length; i < n; ++i) {
sum += values[i];
means[i] = sum / N;
sum -= values[i - N + 1];
}
return means;
}
Insert cell
dates = {
const parseDate = d3.timeParse("%m/%d/%Y %I:%M:%S %p");
return FileAttachment("chicago-homicide-dates.csv").csv().then(data => data.map(({date}) => parseDate(date)))
}
Insert cell
Insert cell
Plot.areaY(dates, Plot.windowY(N, Plot.binX({ y: "count" }, { x: Plot.identity, interval: "day", fill: "steelblue" })))
.plot({ y: { grid: true, label: null } })
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