plot icon indicating copy to clipboard operation
plot copied to clipboard

Periodic scales?

Open Fil opened this issue 2 years ago • 0 comments

When dealing with time series, we often want to overlay lines to facilitate year on year comparisons. Or, when we facet by year, we'd like the time axis (within facets) to spread from start to end of year.

A way to do it is to change x though a scale transform, e.g.:

  • d => d3.utcDay.count(d3.utcYear(d.Date), d.Date) (number of days since start of the year), or
  • d => +d.Date - d3.utcYear(d) (number of milliseconds since start of the year), or even
  • d => new Date(+new Date(Date.UTC(1972, 0, 1) + +d - d3.utcYear(d)) (a date between 1st Jan. 1972 and 31 Dec 1972 — a leap year)

This is a bit complex to get right (UTC, DST and leap years can make it hard to adjust), and it can cause issues elsewhere — for example in the tips we want the true data, not a shifted value, and on the date axis we want to suppress the year.

I wonder if we could have a "periodic: year" syntax in the scale definition which took care of all this for us. As it is orthogonal with intervals, we would accept the same DSL — "periodic: 6 months"; "periodic: 1" would plot the fractional value.

Here's a notebook that shows how what I'd like to simplify: https://observablehq.com/@observablehq/periodic-scale-1888

Fil avatar Oct 02 '23 08:10 Fil