From 5c110716cfda6e616a795edd12f2012b132dca9f Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 2 Apr 2017 17:51:12 +0200 Subject: Add a chart on payments by month by categories --- src/client/Chart/Model.elm | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/client/Chart/Model.elm (limited to 'src/client/Chart/Model.elm') diff --git a/src/client/Chart/Model.elm b/src/client/Chart/Model.elm new file mode 100644 index 0000000..b5c176f --- /dev/null +++ b/src/client/Chart/Model.elm @@ -0,0 +1,73 @@ +module Chart.Model exposing + ( Chart + , Serie + , maxScale + , Vec2 + , View + , mkView + , bounds + ) + +import List.Extra as List + +type alias Chart = + { keys : List String + , series : List Serie + , size : Vec2 + , title : String + , scaleColor : String + , formatOrdinate : Float -> String + , ordinateLines : Int + } + +type alias Serie = + { values : List Float + , color : String + , label : String + } + +maxScale : Chart -> Float +maxScale { keys, series } = + List.range 0 (List.length keys - 1) + |> List.map (\i -> + series + |> List.map (truncate << Maybe.withDefault 0 << List.getAt i << .values) + |> List.maximum + |> Maybe.withDefault 0 + ) + |> List.maximum + |> Maybe.withDefault 0 + |> upperBound + +upperBound : Int -> Float +upperBound n = toFloat (upperBoundInt 0 n) + +upperBoundInt : Int -> Int -> Int +upperBoundInt count n = + if n < 10 + then + (n + 1) * (10 ^ count) + else + upperBoundInt (count + 1) (n // 10) + +type alias Vec2 = + { x : Float + , y : Float + } + +type alias View = + { fx : Float -> Float + , fy : Float -> Float + } + +mkView : Vec2 -> Vec2 -> View +mkView p1 p2 = + { fx = \x -> p1.x + x * (p2.x - p1.x) + , fy = \y -> p1.y + y * (p2.y - p1.y) + } + +bounds : View -> (Vec2, Vec2) +bounds { fx, fy } = + ( { x = fx 0, y = fy 0 } + , { x = fx 1, y = fy 1 } + ) -- cgit v1.2.3