mthiems / intervals / Intervals

Produce "nice" intervals for e.g. axis labels.

What are "nice" numbers/integers/datetimes?

When I say "nice", I just mean that I try to calculate intervals which begin with 10, 5, 3, 2, 1 (adjusted to magnitude, of course!). For dates, I try to hit whole days, weeks, months or hours, minutes, and seconds.

Nice numbers

ints : Amount -> Range -> List Basics.Int

Produce a list of "nice" integers.

floats : Amount -> Range -> List Basics.Float

Produce a list of "nice" floats.


type Amount

around : Basics.Int -> Amount

Will get you around the amount of numbers you pass it, although it will prioritize getting "nice" numbers.

exactly : Basics.Int -> Amount

Will get you closer to the amount of numbers you pass it, although not actually exactly, since you still want decently "nice" numbers.

P.S. If you have a better name for this function, please contact me.


type alias Range =
{ min : Basics.Float
, max : Basics.Float 
}

The upper and lower bound of your numbers/timestamps.

Custom numbers

custom : Basics.Float -> Basics.Float -> Range -> List Basics.Float

Makes evenly spaced floats.

Arguments: 1. A number which must be in your resulting numbers (commonly 0). 2. The interval between your numbers. 3. The range which your numbers must be between.

Intervals.custom 45 10 (Range 25 100)
-- ^ Makes [ 25, 35, 45, 55, 65, 75, 85, 95 ]

Intervals.custom 30 20 (Range 25 100)
-- ^ Makes [ 30, 50, 70, 90 ]

Nice times

times : Time.Zone -> Basics.Int -> Range -> List Time

Produce a list of "nice" dates.

Arguments:

  1. The timezone which the chart is being shown in.
  2. The maximum amount of times you'd like to have produced.
  3. The beginning timestamp (as a float for chart convenience)
  4. The ending timestamp (as a float for chart convenience)


type alias Time =
{ timestamp : Time.Posix
, zone : Time.Zone
, isFirst : Basics.Bool
, unit : Unit
, multiple : Basics.Int
, change : Maybe Unit 
}

A timestamp with extra info helpful for formatting. Explanation:


type Unit
    = Millisecond
    | Second
    | Minute
    | Hour
    | Day
    | Month
    | Year

You can format your tick label differently based on it's unit.