plot icon indicating copy to clipboard operation
plot copied to clipboard

Interval-aware default tick formats

Open Fil opened this issue 3 years ago • 1 comments

When the data only contains integers, it would be great if the default tickFormat would not do "1.0, 2.0, 3.0"… but rather "1, 2, 3".

In #849 I hoped that indicating an integer interval would be used, in this case to infer an intent of only having integers. (Similarly, if we used interval: d3.utcYear we'd rather format the ticks as "%Y", and d3.utcMonth would result in a default of "%Y-%m" or something.)

Example: the test plot integer-interval.js (svg) should have inferred an integer tickFormat for the x axis. (See also the discussion here.)

Also related to #768

Fil avatar Jun 10 '22 17:06 Fil

I’ve retitled this issue to describe the more general issue.

mbostock avatar Jun 10 '22 22:06 mbostock

A related typical use case is

Plot.tickX([new Date(2001, 0, 1), new Date(2001, 1, 1)]).plot({
  x: {interval: "day", tickRotate:40},
  height: 70, marginBottom: 50})
Capture d’écran 2023-01-28 à 13 38 15

Since the interval is "day" we could have a stricter tickFormat than the default "multiscale".

cc: @mootari

Fil avatar Jan 28 '23 12:01 Fil

Related feedback from Misha Tsvelik: https://vimeo.com/829300247/91d6c7eeef

This issue applies to ticks, and not just tick formats: for example if the data is just integers, then we wouldn’t want to show ticks at 0.1, 0.2, 0.3, etc., or if the data is just months, we don’t want to show weekly ticks.

I guess we would need some heuristic that looks at the channel values and guesses what the interval is; if we guess successfully, then we can choose an appropriate tick interval and accompanying tick format.

mbostock avatar May 23 '23 18:05 mbostock

This is pretty much fixed by #1728 for time axes, but we’d need to do something similar to numbers/integers. ~~And also this isn’t fixed for binning, in the sense of you don’t want to bin hourly if you only have daily data.~~ That latter issue is #1013.

mbostock avatar Jun 27 '23 16:06 mbostock

I got this working in #1790, but messy:

    : tickFormat === undefined && data && isNumeric(data) && scale.interval?.[intervalDuration]
    ? format(`.${`${scale.interval?.[intervalDuration] % 1}`.length - 2}f`)

mbostock avatar Aug 23 '23 20:08 mbostock