Pie.purs 4.64 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 18
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Data (dataSerie)
import Gargantext.Ends (url)
19
import Gargantext.Hooks.Loader (useLoader)
20
import Gargantext.Components.Nodes.Corpus.Chart.Utils as U
21
import Gargantext.Routes (SessionRoute(..))
22
import Gargantext.Sessions (Session, get)
23
import Gargantext.Types (ChartType(..), TabType)
24 25 26 27 28 29

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

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

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

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

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

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

54
type Loaded = HistoMetrics
55

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

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


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

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

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

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

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


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

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


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

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