aboutsummaryrefslogtreecommitdiff
path: root/src/client/Chart/Model.elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/Chart/Model.elm')
-rw-r--r--src/client/Chart/Model.elm73
1 files changed, 73 insertions, 0 deletions
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 }
+ )