-- miscellaneous functions extracted from Main so
-- we can more easily test them

module Lib exposing (looseLabels)


-- https://github.com/cenfun/nice-ticks/blob/master/docs/Nice-Numbers-for-Graph-Labels.pdf

niceNumber x round =
    let expt b ex = b^ toFloat ex
        exp = floor (logBase 10 x)
        f = x / expt 10.0 exp
        nfRound = if f < 1.5
                  then 1
                  else if f < 3
                       then 2
                       else if f < 7
                            then 5
                            else 10
        nf = if f <= 1
             then 1
             else if f <= 2
                  then 2
                  else if f <= 5
                       then 5
                       else 10
    in
        if round
        then
            nfRound * expt 10 exp
        else
            nf * expt 10 exp

looseLabels ticks min max =
    let
        range = niceNumber (max-min) False
        d = niceNumber (range/(ticks - 1)) True
        graphmin = toFloat (floor (min/d)) * d
        graphmax = toFloat (ceiling (max/d)) * d
    in (graphmin, graphmax, d)
