Published
Edited
Sep 3, 2020
Fork of Styled axes
Insert cell
Insert cell
Insert cell
Insert cell
xAxis = svg => svg
.append("svg")
.attr("viewBox", "0 0 100 100")
.attr("vector-effect", "non-scaling-stroke")
.attr("preserveAspectRatio", "none")
.attr("x", "0%")
.attr("y", "95")
.call(d3.axisBottom(x)
.ticks(d3.timeMonth.every(3))
.tickFormat(d => d <= d3.timeYear(d) ? d.getFullYear() : null))
Insert cell
Insert cell
yAxis = svg => svg
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisRight(y)
.tickSize(width - margin.left - margin.right)
.tickFormat(formatTick))
.call(g => g.select(".domain")
.remove())
.call(g => g.selectAll(".tick:not(:first-of-type) line")
.attr("stroke-opacity", 0.5)
.attr("stroke-dasharray", "2,2"))
.call(g => g.selectAll(".tick text")
.attr("x", 4)
.attr("dy", -4))
Insert cell
Insert cell
function formatTick(d) {
const s = (d / 1e6).toFixed(1);
return this.parentNode.nextSibling ? `\xa0${s}` : `$${s} million`;
}
Insert cell
Insert cell
x = d3.scaleTime()
.domain([new Date(2010, 7, 1), new Date(2012, 7, 1)])
.range([0, 100])
Insert cell
y = d3.scaleLinear()
.domain([0, 2e6])
.range([0, 100])
Insert cell
margin = ({top: 20, right: 0, bottom: 30, left: 0})
Insert cell
height = 500
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