Published
Edited
Jun 4, 2021
Importers
Insert cell
Insert cell
Insert cell
html`<style>
.tick {
font-size: 12px;
color: #444;
font-family:'Benton Gothic'
}

path.year2020 {
stroke: #ec8431;
stroke-width: 1.5;
}

h4 {
line-height: 1.5em;
padding: 20px 0px 20px 0px;
}
</style>`
Insert cell
data = {
const timeParse = d3.timeParse("%Y-%m-%d")

const raw = d3.csvParse(await FileAttachment("fireworks_by_date.csv").text(), function (d) {
d.date = timeParse(d.date)
d.call = +d.calls
return d
})
const grouped = Array.from(d3.group(raw, d => d.date.getFullYear()), d => ({
name: d[0],
values: d3.cumsum(d[1], d => d.calls),
dates: d[1].map(d => d.date),
}))
// use latest complete year for dates
const dates = grouped[grouped.length - 2].dates
return {
y: "Fireworks complaints to LAPD",
series: grouped.filter(d => d.name > 1958),
dates: dates,
}
}
Insert cell
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 30})
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data.dates))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data.series, d => d3.max(d.values))]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.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
line = d3.line()
.curve(d3.curveStep)
.x((d, i) => x(data.dates[i]))
.y(d => y(d))
Insert cell
d3 = require("d3@5", "d3-array@2")
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