-
Notifications
You must be signed in to change notification settings - Fork 0
/
SalariesChart.elm
168 lines (107 loc) · 3.18 KB
/
SalariesChart.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module SalariesChart exposing (..)
import Html exposing (..)
import Html.Attributes as Attributes exposing (..)
import Http
import Json.Decode as Decode exposing (..)
import Json.Decode.Pipeline as Pipeline exposing (decode, required)
import Svg exposing ( svg, circle, line, text_, text , g )
import Svg.Attributes as SvgAttributes exposing (..)
-- RUN
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Salary =
{ pay : Int }
type alias Model =
{
salaries : List Salary
}
initModel : Model
initModel =
{ salaries =
[ { pay = 70000}
, { pay = 90000 }
, { pay = 55000 }
, { pay =86000 }
]
}
init : ( Model, Cmd Msg)
init =
( initModel, getSalaries )
-- UPDATE
type Msg
= Salaries
| NewSalaries ( Result Http.Error (List Salary))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Salaries ->
( model, Cmd.none )
NewSalaries ( Ok newSalaries ) ->
( { model | salaries = newSalaries }, Cmd.none )
NewSalaries ( Err error) ->
let
_ = Debug.log "Error is:" error
in
( model, Cmd.none )
-- VIEW
view : Model -> Html Msg
view model =
let
articleStyle: Attribute msg
articleStyle =
Attributes.style
[
( "display", "flex")
, ( "width", "auto" )
, ( "flexFlow", "row wrap")
, ( "margin", "2%")
]
in
article [ articleStyle ]
[
h2 [][ Html.text "View Anonymous Salary reports"]
, chart model
]
chart : Model -> Svg.Svg msg
chart model =
svg
[ SvgAttributes.width "100%", SvgAttributes.height "10%", viewBox " 0 0 100 10 " ]
[ g[] (List.map salaryDot model.salaries )
, line [ x1 "10", x2 "90", y1 "8", y2 "8", stroke "#0074d9", strokeWidth "0.1" ][]
, text_ [ x "6", y "9", fontSize "2" ][ Svg.text "$30k" ]
, text_ [ x "90", y "9" , fontSize "2" ][ Svg.text "$150k" ]
]
viewSalary : Salary -> Html Msg
viewSalary salary =
div []
[ Html.text (toString salary.pay) ]
salaryDot : Salary -> Svg.Svg msg
salaryDot salary =
circle [ cx (toString ( salary.pay * 80 // 150000 ) ), cy "5", r "0.5" ] []
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- HTTP
getSalaries =
let
url = "http://api.dollartranscript.xyz/salaries" -- replace localhost with api.dollarTranscript.xyz/salaries
request =
Http.get url decodeSalaries
in
Http.send NewSalaries request
-- JSON
decodeSalaries : Decode.Decoder ( List Salary )
decodeSalaries =
Decode.list decodeSalary
-- Decode.list <| Decode.map (\x -> { pay = x } ) ( Decode.field "pay" Decode.int )
decodeSalary : Decode.Decoder Salary
decodeSalary =
decode Salary
|> Pipeline.required "pay" Decode.int