aboutsummaryrefslogtreecommitdiff
path: root/src/client/LoggedIn/Stat/View.elm
blob: e389c67a8e2c1b52cbd76223c46ec49dd999e567 (plain)
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
module LoggedIn.Stat.View exposing
  ( view
  )

import Date exposing (Month)
import Dict
import Html exposing (..)
import Html.Attributes exposing (..)
import List.Extra as List
import Time exposing (Time)

import Chart.Api as Chart
import LoggedData exposing (LoggedData)
import LoggedIn.Stat.Model as Stat
import LoggedIn.View.Format as Format
import Model.Category exposing (CategoryId, Categories)
import Model.Conf exposing (Conf)
import Model.Payment as Payment exposing (Payments)
import Model.PaymentCategory as PaymentCategory exposing (PaymentCategories)
import Model.Translations exposing (Translations, getMessage, getParamMessage)
import Msg exposing (Msg)
import Utils.List as List
import View.Date as Date
import View.Plural exposing (plural)

view : LoggedData -> Stat.Model -> Html Msg
view loggedData { paymentsByMonthByCategory } =
  div
    [ class "stat withMargin" ]
    [ renderChart loggedData paymentsByMonthByCategory ]

renderChart : LoggedData -> List ((Month, Int), List (CategoryId, Int)) -> Html msg
renderChart { currentTime, paymentCategories, categories, conf, translations } paymentsByMonthByCategory =
  let monthPaymentMean = getMonthPaymentMean currentTime paymentsByMonthByCategory
      title = getParamMessage [ Format.price conf monthPaymentMean ] translations "ByMonthsAndMean"
      keys =
        paymentsByMonthByCategory
          |> List.map (\((month, year), _) -> Date.shortMonthAndYear month year translations)
      series =
        categories
          |> Dict.toList
          |> List.map (\(categoryId, category) ->
               { values =
                   List.map
                     (\(_, paymentsByCategory) ->
                       paymentsByCategory
                         |> List.find (\(c, _) -> c == categoryId)
                         |> Maybe.map (toFloat << Tuple.second)
                         |> Maybe.withDefault 0
                     )
                     paymentsByMonthByCategory
               , color = category.color
               , label = category.name
               }
             )
      totalSerie =
        { values =
            List.transpose (List.map .values series)
              |> List.map List.sum
        , color = "black"
        , label = getMessage translations "Total"
        }
  in  Chart.from keys (series ++ [totalSerie])
        |> Chart.withSize { x = 2000, y = 900 }
        |> Chart.withTitle title
        |> Chart.withOrdinate 10 (Format.price conf << truncate)
        |> Chart.toHtml

getMonthPaymentMean : Time -> List ((Month, Int), List (CategoryId, Int)) -> Int
getMonthPaymentMean  currentTime paymentsByMonthByCategory =
  paymentsByMonthByCategory
    |> List.filter (\((month, year), _) ->
         let currentDate = Date.fromTime currentTime
         in  not (Date.month currentDate == month && Date.year currentDate == year)
       )
    |> List.map (List.sum << List.map Tuple.second << Tuple.second)
    |> List.mean