Pie.purs 5.54 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
import Data.Maybe (Maybe(..))
8 9
import Data.String (take, joinWith, Pattern(..), split, length)
import Data.Tuple (Tuple(..))
10
import Data.Tuple.Nested ((/\))
11
import Effect.Aff (Aff)
12 13 14
import Reactix as R
import Reactix.DOM.HTML as H

15
import Gargantext.Components.Charts.Options.Color (blue)
16
import Gargantext.Components.Charts.Options.Data (dataSerie)
17 18 19
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)
20
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsLoadView, metricsWithCacheLoadView)
21
import Gargantext.Components.Nodes.Corpus.Chart.Types
22
import Gargantext.Components.Nodes.Corpus.Chart.Utils as U
23
import Gargantext.Hooks.Loader (HashedResponse(..))
24
import Gargantext.Prelude
25
import Gargantext.Routes (SessionRoute(..))
26
import Gargantext.Sessions (Session, get)
27
import Gargantext.Types (ChartType(..), TabType)
28
import Gargantext.Utils.CacheAPI as GUC
29 30 31
import Gargantext.Utils.Reactix as R2

thisModule = "Gargantext.Components.Nodes.Corpus.Chart.Pie"
32

33 34
newtype ChartMetrics = ChartMetrics {
    "data" :: HistoMetrics
35 36
  }

37
instance decodeChartMetrics :: DecodeJson ChartMetrics where
38
  decodeJson json = do
39
    obj <- decodeJson json
40
    d   <- obj .: "data"
41 42 43 44 45
    pure $ ChartMetrics { "data": d }

newtype HistoMetrics = HistoMetrics
  { dates :: Array String
  , count :: Array Number
46 47
  }

48
instance decodeHistoMetrics :: DecodeJson HistoMetrics where
49
  decodeJson json = do
50
    obj   <- decodeJson json
51 52
    d <- obj .: "dates"
    c <- obj .: "count"
53
    pure $ HistoMetrics { dates : d , count: c}
54

55 56 57 58 59 60
instance encodeHistoMetrics :: EncodeJson HistoMetrics where
  encodeJson (HistoMetrics { dates, count }) =
       "count" := encodeJson count
    ~> "dates"    := encodeJson dates
    ~> jsonEmptyObject

61
type Loaded = HistoMetrics
62

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

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

86 87
getMetricsHash :: Session -> Tuple Reload (Record Path) -> Aff String
getMetricsHash session (_ /\ { corpusId, limit, listId, tabType }) = do
88
  get session $ ChartHash { chartType: ChartPie, listId: mListId, tabType } (Just corpusId)
89 90
  where
    mListId = if listId == 0 then Nothing else (Just listId)
91

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

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

103
pie :: Record Props -> R.Element
104
pie props = R.createElement pieCpt props []
105

106
pieCpt :: R.Component Props
107
pieCpt = R2.hooksComponent thisModule "pie" cpt
108
  where
109
    cpt { path, session } _ = do
110
      reload <- R.useState' 0
111

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

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


131
bar :: Record Props -> R.Element
132
bar props = R.createElement barCpt props []
133

134
barCpt :: R.Component Props
135
barCpt = R2.hooksComponent thisModule "bar" cpt
136
  where
137
    cpt {path, session} _ = do
138
      reload <- R.useState' 0
139 140 141 142 143 144 145 146 147 148

      pure $ metricsWithCacheLoadView {
           getMetricsHash
         , handleResponse
         , loaded: loadedBar
         , mkRequest: mkRequest session
         , path
         , reload
         , session
         }
149 150 151

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