Histo.purs 2.98 KB
Newer Older
1
module Gargantext.Components.Nodes.Corpus.Chart.Histo where
2

3
import Prelude (bind, map, pure, ($))
4
import Data.Argonaut (class DecodeJson, decodeJson, (.:))
5
import Data.Maybe (Maybe(..))
6
import Effect.Aff (Aff)
7 8
import Reactix as R

9 10 11 12 13
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Series (seriesBarD1)
import Gargantext.Components.Charts.Options.Color (grey)
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Data (dataSerie)
14
import Gargantext.Hooks.Loader (useLoader)
15
import Gargantext.Components.Nodes.Corpus.Chart.Utils as U
16
import Gargantext.Routes (SessionRoute(..))
17
import Gargantext.Sessions (Session, get)
18
import Gargantext.Types (ChartType(..), TabType)
19

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

22
type Props = ( path :: Path, session :: Session )
23 24

newtype ChartMetrics = ChartMetrics { "data" :: HistoMetrics }
25 26 27 28

instance decodeChartMetrics :: DecodeJson ChartMetrics where
  decodeJson json = do
    obj <- decodeJson json
29
    d   <- obj .: "data"
30 31
    pure $ ChartMetrics { "data": d }

32
newtype HistoMetrics = HistoMetrics { dates :: Array String, count :: Array Number }
33 34 35 36

instance decodeHistoMetrics :: DecodeJson HistoMetrics where
  decodeJson json = do
    obj   <- decodeJson json
37 38
    d <- obj .: "dates"
    c <- obj .: "count"
39 40 41 42 43 44 45 46 47
    pure $ HistoMetrics { dates : d , count: c}

type Loaded = HistoMetrics

chartOptions :: HistoMetrics -> Options
chartOptions (HistoMetrics { dates: dates', count: count'}) = Options
  { mainTitle : "Histogram"
  , subTitle  : "Distribution of publications over time"
  , xAxis     : xAxis' dates'
48
  , yAxis     : yAxis' { position: "left", show: true, min:0}
49 50
  , addZoom   : true
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
51 52
  , series    : [seriesBarD1 {name: "Number of publication / year"} $
                 map (\n -> dataSerie {value: n, itemStyle : itemStyle {color:grey}}) count'] }
53

54 55
getMetrics :: Session -> Path -> Aff HistoMetrics
getMetrics session {corpusId, tabType} = do
56
  ChartMetrics ms <- get session chart
57
  pure ms."data"
58 59 60 61
  where chart = Chart {chartType: Histo, tabType: tabType} (Just corpusId)

histo :: Record Props -> R.Element
histo props = R.createElement histoCpt props []
62

63 64
histoCpt :: R.Component Props
histoCpt = R.hooksComponent "LoadedMetricsHisto" cpt
65
  where
66
    cpt {session,path} _ = do
67
      setReload <- R.useState' 0
68
      pure $ metricsLoadView session setReload path
69

70 71
metricsLoadView :: Session -> R.State Int -> Path -> R.Element
metricsLoadView s setReload p = R.createElement el {session: s, path: p} []
72 73
  where
    el = R.hooksComponent "MetricsLoadedHistoView" cpt
74 75
    cpt {path,session} _ = do
      useLoader path (getMetrics session) $ \loaded ->
76 77 78
        loadedMetricsView setReload loaded

loadedMetricsView :: R.State Int -> HistoMetrics -> R.Element
79
loadedMetricsView setReload loaded = U.reloadButtonWrap setReload $ chart $ chartOptions loaded