Pie.purs 5.58 KB
Newer Older
1
module Gargantext.Components.Nodes.Corpus.Chart.Pie where
2

3
import Data.Array (zip, filter)
4
import Data.Array as A
5
import Data.Eq.Generic (genericEq)
6
import Data.Generic.Rep (class Generic)
7
import Data.Maybe (Maybe(..))
8
import Data.Newtype (class Newtype)
9 10
import Data.String (take, joinWith, Pattern(..), split, length)
import Data.Tuple (Tuple(..))
11
import Data.Tuple.Nested ((/\))
12
import Gargantext.Components.Charts.Options.Color (blue)
13
import Gargantext.Components.Charts.Options.Data (dataSerie)
14 15 16
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Series (seriesBarD1, seriesPieD1)
17
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
18 19
import Gargantext.Components.Nodes.Corpus.Chart.Types (MetricsProps, Path, Props, ReloadPath)
import Gargantext.Config.REST (AffRESTError)
20
import Gargantext.Hooks.Loader (HashedResponse(..))
21
import Gargantext.Prelude (class Eq, bind, map, pure, ($), (==), (>))
22
import Gargantext.Routes (SessionRoute(..))
23
import Gargantext.Sessions (Session, get)
24
import Gargantext.Types (ChartType(..))
25
import Gargantext.Utils.CacheAPI as GUC
26
import Gargantext.Utils.Reactix as R2
27
import Gargantext.Utils.Toestand as T2
28 29 30 31
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
import Toestand as T
32

33 34
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Pie"
35

36 37
newtype ChartMetrics = ChartMetrics {
    "data" :: HistoMetrics
38
  }
39 40 41
derive instance Generic ChartMetrics _
derive instance Newtype ChartMetrics _
derive newtype instance JSON.ReadForeign ChartMetrics
42 43 44 45

newtype HistoMetrics = HistoMetrics
  { dates :: Array String
  , count :: Array Number
46
  }
47
derive instance Generic HistoMetrics _
48 49 50 51
derive instance Newtype HistoMetrics _
instance Eq HistoMetrics where eq = genericEq
derive newtype instance JSON.ReadForeign HistoMetrics
derive newtype instance JSON.WriteForeign HistoMetrics
52

53
type Loaded = HistoMetrics
54

55 56
chartOptionsBar :: Record MetricsProps -> HistoMetrics -> Options
chartOptionsBar { onClick, onInit } (HistoMetrics { dates: dates', count: count'}) = Options
57
  { mainTitle : "Bar"
58
  , subTitle  : "Count of MapTerm"
59
  , xAxis     : xAxis' $ map (\t -> joinWith " " $ map (take 3) $ A.take 3 $ filter (\s -> length s > 3) $ split (Pattern " ") t) dates'
60
  , yAxis     : yAxis' { position: "left", show: true, min:0}
61
  , series    : [seriesBarD1 {name: "Number of publication / year"} $ map (\n -> dataSerie {name: "", itemStyle: itemStyle {color:blue}, value: n }) count']
62 63
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
64 65
  , onClick
  , onInit
66
  }
67

68 69
chartOptionsPie :: Record MetricsProps -> HistoMetrics -> Options
chartOptionsPie { onClick, onInit } (HistoMetrics { dates: dates', count: count'}) = Options
70
  { mainTitle : "Pie"
71
  , subTitle  : "Distribution by MapTerm"
72
  , xAxis     : xAxis' []
73
  , yAxis     : yAxis' { position: "", show: false, min:0}
74 75 76 77
  , series    : [seriesPieD1 {name: "Data"} $ map (\(Tuple n v) -> dataSerie {name: n, value:v}) $ zip dates' count']
  -- , series    : [seriesBarD1 {name: "Number of publication / year"} $ map (\n -> dataSerie {name: "", value: n }) count']
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
78 79
  , onClick
  , onInit
80 81
  }

82
getMetricsHash :: Session -> ReloadPath -> AffRESTError String
83
getMetricsHash session (_ /\ { corpusId, listId, tabType }) = do
84
  get session $ ChartHash { chartType: ChartPie, listId: mListId, tabType } (Just corpusId)
85 86
  where
    mListId = if listId == 0 then Nothing else (Just listId)
87

88
chartUrl :: Record Path -> SessionRoute
89 90 91
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartPie, limit, listId: mListId, tabType} (Just corpusId)
  where
    mListId = if listId == 0 then Nothing else (Just listId)
92 93 94 95 96

handleResponse :: HashedResponse ChartMetrics -> HistoMetrics
handleResponse (HashedResponse { value: ChartMetrics ms }) = ms."data"

mkRequest :: Session -> ReloadPath -> GUC.Request
97
mkRequest session (_ /\ path) = GUC.makeGetRequest session $ chartUrl path
98

99
pie :: R2.Leaf Props
100
pie props = R.createElement pieCpt props []
101
pieCpt :: R.Component Props
102
pieCpt = here.component "pie" cpt
103
  where
104
    cpt { boxes, path, session, onClick, onInit } _ = do
105
      reload <- T.useBox T2.newReload
106

107 108
      pure $ metricsWithCacheLoadView
        { boxes
109
        , getMetricsHash
110 111 112 113 114 115
        , handleResponse
        , loaded: loadedPie
        , mkRequest: mkRequest session
        , path
        , reload
        , session
116 117
        , onClick
        , onInit
118 119 120
        }

loadedPie :: Record MetricsProps -> HistoMetrics -> R.Element
121
loadedPie p loaded =
122
  H.div {} [
123
  {-  U.reloadButton reload
124
  , U.chartUpdateButton { chartType: ChartPie, path, reload, session }
125
  , -} chart $ chartOptionsPie p loaded
126
  ]
127 128


129
bar :: Record Props -> R.Element
130
bar props = R.createElement barCpt props []
131
barCpt :: R.Component Props
132
barCpt = here.component "bar" cpt
133
  where
134
    cpt { boxes, path, session, onClick, onInit} _ = do
135
      reload <- T.useBox T2.newReload
136 137

      pure $ metricsWithCacheLoadView {
138
           boxes
139
         , getMetricsHash
140 141 142 143 144 145
         , handleResponse
         , loaded: loadedBar
         , mkRequest: mkRequest session
         , path
         , reload
         , session
146 147
         , onClick
         , onInit
148
         }
149 150

loadedBar :: Record MetricsProps -> Loaded -> R.Element
151
loadedBar p loaded =
152
  H.div {} [
153
  {-  U.reloadButton reload
154
  , U.chartUpdateButton { chartType: ChartBar, path, reload, session }
155
  , -} chart $ chartOptionsBar p loaded
156
  ]