Public
Edited
Jul 13, 2024
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

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