Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save connerxyz/fc6bc23b2678f26bed64cd38011609ac to your computer and use it in GitHub Desktop.
Save connerxyz/fc6bc23b2678f26bed64cd38011609ac to your computer and use it in GitHub Desktop.
d3.js: v5: scaling number of ticks with window's inner width
var xScale = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.key; })) // Input
.range([0, width]); // output
// Quantize 400px - 1600px window range
var tickScale = d3.scaleQuantize()
.domain([400, 1600])
.range([3, 7, 10]);
// Set number of ticks based on window height
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale).ticks(tickScale(window.innerWidth)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment