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

3 4
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
5
import Data.Array (zip, filter)
6
import Data.Array as A
7 8
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
9
import Data.Maybe (Maybe(..))
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 Toestand as T
17

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

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

36 37
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Pie"
38

39 40
newtype ChartMetrics = ChartMetrics {
    "data" :: HistoMetrics
41 42
  }

43
instance decodeChartMetrics :: DecodeJson ChartMetrics where
44
  decodeJson json = do
45
    obj <- decodeJson json
46
    d   <- obj .: "data"
47 48 49 50 51
    pure $ ChartMetrics { "data": d }

newtype HistoMetrics = HistoMetrics
  { dates :: Array String
  , count :: Array Number
52
  }
53 54 55
derive instance genericHistoMetrics :: Generic HistoMetrics _
instance eqHistoMetrics :: Eq HistoMetrics where
  eq = genericEq
56
instance decodeHistoMetrics :: DecodeJson HistoMetrics where
57
  decodeJson json = do
58
    obj   <- decodeJson json
59 60
    d <- obj .: "dates"
    c <- obj .: "count"
61
    pure $ HistoMetrics { dates : d , count: c}
62

63 64 65 66 67 68
instance encodeHistoMetrics :: EncodeJson HistoMetrics where
  encodeJson (HistoMetrics { dates, count }) =
       "count" := encodeJson count
    ~> "dates"    := encodeJson dates
    ~> jsonEmptyObject

69
type Loaded = HistoMetrics
70

71 72
chartOptionsBar :: HistoMetrics -> Options
chartOptionsBar (HistoMetrics { dates: dates', count: count'}) = Options
73
  { mainTitle : "Bar"
74
  , subTitle  : "Count of MapTerm"
75
  , xAxis     : xAxis' $ map (\t -> joinWith " " $ map (take 3) $ A.take 3 $ filter (\s -> length s > 3) $ split (Pattern " ") t) dates'
76
  , yAxis     : yAxis' { position: "left", show: true, min:0}
77
  , series    : [seriesBarD1 {name: "Number of publication / year"} $ map (\n -> dataSerie {name: "", itemStyle: itemStyle {color:blue}, value: n }) count']
78 79 80
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
  }
81 82 83 84

chartOptionsPie :: HistoMetrics -> Options
chartOptionsPie (HistoMetrics { dates: dates', count: count'}) = Options
  { mainTitle : "Pie"
85
  , subTitle  : "Distribution by MapTerm"
86
  , xAxis     : xAxis' []
87
  , yAxis     : yAxis' { position: "", show: false, min:0}
88 89 90 91 92 93
  , 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}" }
  }

94
getMetricsHash :: Session -> ReloadPath -> Aff String
95
getMetricsHash session (_ /\ { corpusId, limit, listId, tabType }) = do
96
  get session $ ChartHash { chartType: ChartPie, listId: mListId, tabType } (Just corpusId)
97 98
  where
    mListId = if listId == 0 then Nothing else (Just listId)
99

100
chartUrl :: Record Path -> SessionRoute
101 102 103
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartPie, limit, listId: mListId, tabType} (Just corpusId)
  where
    mListId = if listId == 0 then Nothing else (Just listId)
104 105 106 107 108 109 110

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

mkRequest :: Session -> ReloadPath -> GUC.Request
mkRequest session (_ /\ path@{ corpusId, limit, listId, tabType }) = GUC.makeGetRequest session $ chartUrl path

111
pie :: R2.Leaf Props
112
pie props = R.createElement pieCpt props []
113

114
pieCpt :: R.Component Props
115
pieCpt = here.component "pie" cpt
116
  where
117
    cpt { path, session } _ = do
118
      reload <- T.useBox T2.newReload
119

120
      pure $ metricsWithCacheLoadView {
121
          getMetricsHash
122 123 124 125 126 127 128 129 130 131
        , handleResponse
        , loaded: loadedPie
        , mkRequest: mkRequest session
        , path
        , reload
        , session
        }

loadedPie :: Record MetricsProps -> HistoMetrics -> R.Element
loadedPie { path, reload, session } loaded =
132
  H.div {} [
133
  {-  U.reloadButton reload
134
  , U.chartUpdateButton { chartType: ChartPie, path, reload, session }
135
  , -} chart $ chartOptionsPie loaded
136
  ]
137 138


139
bar :: Record Props -> R.Element
140
bar props = R.createElement barCpt props []
141

142
barCpt :: R.Component Props
143
barCpt = here.component "bar" cpt
144
  where
145
    cpt {path, session} _ = do
146
      reload <- T.useBox T2.newReload
147 148 149 150 151 152 153 154 155 156

      pure $ metricsWithCacheLoadView {
           getMetricsHash
         , handleResponse
         , loaded: loadedBar
         , mkRequest: mkRequest session
         , path
         , reload
         , session
         }
157 158 159

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