forked from avh4/elm-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordGraph.elm
105 lines (94 loc) · 2.4 KB
/
WordGraph.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module Component.WordGraph exposing (..)
import Svg exposing (..)
import Svg.Attributes exposing (..)
type alias Entry =
{ words : Int
, day : String
}
type alias Day =
{ amount : Float
, day : String
, xOffset : Int
}
graphHeight = 18
barWidth = 10
barMargin = 1
maxDays = 14
bar : Day -> Float -> Svg
bar day yOffset =
rect
[ x <| toString <| day.xOffset * (barWidth + barMargin)
, y <|
toString <|
if day.amount <= 0
then yOffset
else yOffset - day.amount
, height <| toString <| abs day.amount
, width <| toString barWidth
]
[ Svg.title [] [ text day.day ] ]
graph : Float -> List Day -> Svg
graph yOffset days =
let graphWidth = List.length days * (barWidth + barMargin)
axis =
line
[ x1 "0"
, y1 <| toString yOffset
, x2 <| toString (graphWidth - barMargin)
, y2 <| toString yOffset
]
[]
in svg
[ id "doc-word-count-graph"
, width <| toString graphWidth
, height <| toString graphHeight
]
[ g
[ y <| toString yOffset
]
{- SVG uses a painter algorithm, so we need axis at the end of
the list to keep bars from overlapping the axis, which gets
pretty ugly.
-}
(List.append (List.map (\day -> bar day yOffset) days)
[ axis ]
)
]
scale : Int -> Int -> Int -> Float
scale top bot value =
let range = top - bot
ratio =
graphHeight
/ (if range == 0
then 0.1
else toFloat range
)
in ratio * toFloat value
viewWordGraph : List Entry -> Svg
viewWordGraph list =
let lastTwoWeeks = List.take maxDays list
max =
List.map (\x -> x.words) lastTwoWeeks
|> List.maximum
|> Maybe.withDefault graphHeight
min =
List.map (\x -> x.words) lastTwoWeeks
|> List.minimum
|> Maybe.withDefault 0
|> (\x ->
if x > 0
then 0
else x
)
yOffset = scale max min max
days =
List.map2
(\offset entry ->
{ amount = scale max min entry.words
, day = entry.day
, xOffset = offset
}
)
[0..maxDays - 1]
lastTwoWeeks
in graph yOffset days