Tree.purs 2.57 KB
Newer Older
1
module Gargantext.Components.Nodes.Corpus.Chart.Tree where
2

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

10 11 12 13
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Series (TreeNode, Trees(..), mkTree)
import Gargantext.Components.Charts.Options.Font (mkTooltip, templateFormatter)
import Gargantext.Ends (url)
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 21 22 23 24 25

type Path =
  { corpusId :: Int
  , listId   :: Int
  , tabType  :: TabType
  , limit    :: Maybe Int
  }
26
type Props = ( path :: Path, session :: Session )
27 28

newtype Metrics = Metrics
29
  { "data" :: Array TreeNode
30 31 32 33 34
  }

instance decodeMetrics :: DecodeJson Metrics where
  decodeJson json = do
    obj <- decodeJson json
35
    d   <- obj .: "data"
36 37
    pure $ Metrics { "data": d }

38
type Loaded  = Array TreeNode
39

40 41 42 43 44
scatterOptions :: Array TreeNode -> Options
scatterOptions nodes = Options
  { mainTitle : "Tree"
  , subTitle  : "Tree Sub Title"
  , xAxis     : xAxis' []
45
  , yAxis     : yAxis' { position : "", show: false, min:0}
46
  , series    : [ mkTree TreeMap nodes]
47 48
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
49 50
-- TODO improve the formatter:
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=treemap-obama
51

52
  }
53

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

tree :: Record Props -> R.Element
tree props = R.createElement treeCpt props []
63

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

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

loadedMetricsView :: R.State Int -> Loaded -> R.Element
80 81 82 83
loadedMetricsView setReload loaded =
  H.div {}
  [ U.reloadButton setReload
  , chart (scatterOptions loaded) ]