Pie.purs 5.64 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.Generic.Rep (class Generic)
6
import Data.Either (Either)
7
import Data.Eq.Generic (genericEq)
8
import Data.Maybe (Maybe(..))
9
import Data.Newtype (class Newtype)
10 11
import Data.String (take, joinWith, Pattern(..), split, length)
import Data.Tuple (Tuple(..))
12
import Data.Tuple.Nested ((/\))
13
import Effect.Aff (Aff)
14 15
import Reactix as R
import Reactix.DOM.HTML as H
16
import Simple.JSON as JSON
17
import Toestand as T
18

19 20
import Gargantext.Prelude (class Eq, bind, map, pure, ($), (==), (>))

21
import Gargantext.Components.Charts.Options.Color (blue)
22
import Gargantext.Components.Charts.Options.Data (dataSerie)
23 24 25
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)
26
import Gargantext.Config.REST (RESTError)
27
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
28
import Gargantext.Components.Nodes.Corpus.Chart.Types
29
  (MetricsProps, Path, Props, ReloadPath)
30
import Gargantext.Hooks.Loader (HashedResponse(..))
31
import Gargantext.Routes (SessionRoute(..))
32
import Gargantext.Sessions (Session, get)
33
import Gargantext.Types (ChartType(..))
34
import Gargantext.Utils.CacheAPI as GUC
35
import Gargantext.Utils.Reactix as R2
36
import Gargantext.Utils.Toestand as T2
37

38 39
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Pie"
40

41 42
newtype ChartMetrics = ChartMetrics {
    "data" :: HistoMetrics
43
  }
44 45 46
derive instance Generic ChartMetrics _
derive instance Newtype ChartMetrics _
derive newtype instance JSON.ReadForeign ChartMetrics
47 48 49 50

newtype HistoMetrics = HistoMetrics
  { dates :: Array String
  , count :: Array Number
51
  }
52
derive instance Generic HistoMetrics _
53 54 55 56
derive instance Newtype HistoMetrics _
instance Eq HistoMetrics where eq = genericEq
derive newtype instance JSON.ReadForeign HistoMetrics
derive newtype instance JSON.WriteForeign HistoMetrics
57

58
type Loaded = HistoMetrics
59

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

73 74
chartOptionsPie :: Record MetricsProps -> HistoMetrics -> Options
chartOptionsPie { onClick, onInit } (HistoMetrics { dates: dates', count: count'}) = Options
75
  { mainTitle : "Pie"
76
  , subTitle  : "Distribution by MapTerm"
77
  , xAxis     : xAxis' []
78
  , yAxis     : yAxis' { position: "", show: false, min:0}
79 80 81 82
  , 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}" }
83 84
  , onClick
  , onInit
85 86
  }

87
getMetricsHash :: Session -> ReloadPath -> Aff (Either RESTError String)
88
getMetricsHash session (_ /\ { corpusId, listId, tabType }) = do
89
  get session $ ChartHash { chartType: ChartPie, listId: mListId, tabType } (Just corpusId)
90 91
  where
    mListId = if listId == 0 then Nothing else (Just listId)
92

93
chartUrl :: Record Path -> SessionRoute
94 95 96
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartPie, limit, listId: mListId, tabType} (Just corpusId)
  where
    mListId = if listId == 0 then Nothing else (Just listId)
97 98 99 100 101

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

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

104
pie :: R2.Leaf Props
105
pie props = R.createElement pieCpt props []
106
pieCpt :: R.Component Props
107
pieCpt = here.component "pie" cpt
108
  where
109
    cpt { boxes, path, session, onClick, onInit } _ = do
110
      reload <- T.useBox T2.newReload
111

112 113
      pure $ metricsWithCacheLoadView
        { boxes
114
        , getMetricsHash
115 116 117 118 119 120
        , handleResponse
        , loaded: loadedPie
        , mkRequest: mkRequest session
        , path
        , reload
        , session
121 122
        , onClick
        , onInit
123 124 125
        }

loadedPie :: Record MetricsProps -> HistoMetrics -> R.Element
126
loadedPie p loaded =
127
  H.div {} [
128
  {-  U.reloadButton reload
129
  , U.chartUpdateButton { chartType: ChartPie, path, reload, session }
130
  , -} chart $ chartOptionsPie p loaded
131
  ]
132 133


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

      pure $ metricsWithCacheLoadView {
143
           boxes
144
         , getMetricsHash
145 146 147 148 149 150
         , handleResponse
         , loaded: loadedBar
         , mkRequest: mkRequest session
         , path
         , reload
         , session
151 152
         , onClick
         , onInit
153
         }
154 155

loadedBar :: Record MetricsProps -> Loaded -> R.Element
156
loadedBar p loaded =
157
  H.div {} [
158
  {-  U.reloadButton reload
159
  , U.chartUpdateButton { chartType: ChartBar, path, reload, session }
160
  , -} chart $ chartOptionsBar p loaded
161
  ]