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

3
import Prelude (bind, map, pure, ($), (>))
4
import Data.Argonaut (class DecodeJson, decodeJson, (.:))
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 Effect.Aff (Aff)
11 12
import Reactix as R

13
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
14 15
import Gargantext.Components.Charts.Options.Series (seriesBarD1, seriesPieD1)
import Gargantext.Components.Charts.Options.Color (blue)
16 17
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Data (dataSerie)
18
import Gargantext.Hooks.Loader (useLoader)
19
import Gargantext.Components.Nodes.Corpus.Chart.Utils as U
20
import Gargantext.Routes (SessionRoute(..))
21
import Gargantext.Sessions (Session, get)
22
import Gargantext.Types (ChartType(..), TabType)
23 24 25 26 27 28

type Path =
  { corpusId :: Int
  , tabType  :: TabType
  }

29
type Props = ( session :: Session, path :: Path )
30

31 32
newtype ChartMetrics = ChartMetrics
  { "data" :: HistoMetrics
33 34
  }

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

newtype HistoMetrics = HistoMetrics
  { dates :: Array String
  , count :: Array Number
44 45
  }

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

53
type Loaded = HistoMetrics
54

55 56
chartOptionsBar :: HistoMetrics -> Options
chartOptionsBar (HistoMetrics { dates: dates', count: count'}) = Options
57 58
  { mainTitle : "Bar"
  , subTitle  : "Count of GraphTerm"
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 64
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
  }
65 66 67 68 69 70

chartOptionsPie :: HistoMetrics -> Options
chartOptionsPie (HistoMetrics { dates: dates', count: count'}) = Options
  { mainTitle : "Pie"
  , subTitle  : "Distribution by GraphTerm"
  , xAxis     : xAxis' []
71
  , yAxis     : yAxis' { position: "", show: false, min:0}
72 73 74 75 76 77 78
  , 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}" }
  }


79 80
getMetrics :: Session -> Path -> Aff HistoMetrics
getMetrics session {corpusId, tabType:tabType} = do
81
  ChartMetrics ms <- get session chart
82
  pure ms."data"
83
  where chart = Chart {chartType: ChartPie, tabType: tabType} (Just corpusId)
84

85 86
pie :: Record Props -> R.Element
pie props = R.createElement pieCpt props []
87

88 89
pieCpt :: R.Component Props
pieCpt = R.hooksComponent "LoadedMetricsPie" cpt
90
  where
91
    cpt {path,session} _ = do
92
      setReload <- R.useState' 0
93
      pure $ metricsLoadPieView session setReload path
94

95 96
metricsLoadPieView :: Session -> R.State Int -> Path -> R.Element
metricsLoadPieView s setReload p = R.createElement el {session: s,path: p} []
97
  where
98
    el = R.hooksComponent "MetricsLoadedPieView" cpt
99 100
    cpt {session,path} _ = do
      useLoader path (getMetrics session) $ \loaded ->
101 102 103
        loadedMetricsPieView setReload loaded

loadedMetricsPieView :: R.State Int -> HistoMetrics -> R.Element
104
loadedMetricsPieView setReload loaded = U.reloadButtonWrap setReload $ chart $ chartOptionsPie loaded
105 106


107 108
bar :: Record Props -> R.Element
bar props = R.createElement barCpt props []
109

110 111
barCpt :: R.Component Props
barCpt = R.hooksComponent "LoadedMetricsBar" cpt
112
  where
113
    cpt {path, session} _ = do
114
      setReload <- R.useState' 0
115
      pure $ metricsLoadBarView session setReload path
116 117


118 119
metricsLoadBarView :: Session -> R.State Int -> Path -> R.Element
metricsLoadBarView s setReload p = R.createElement el {path: p, session: s} []
120
  where
121
    el = R.hooksComponent "MetricsLoadedBarView" cpt
122 123
    cpt {path, session} _ = do
      useLoader path (getMetrics session) $ \loaded ->
124
        loadedMetricsBarView setReload loaded
125

126
loadedMetricsBarView :: R.State Int -> Loaded -> R.Element
127
loadedMetricsBarView setReload loaded = U.reloadButtonWrap setReload $ chart $ chartOptionsBar loaded