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

3 4
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
5
import Data.Maybe (Maybe(..))
6 7
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
8
import Effect.Aff (Aff)
9 10 11
import Reactix as R
import Reactix.DOM.HTML as H

12 13
import Gargantext.Prelude

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

thisModule = "Gargantext.Components.Nodes.Corpus.Chart.Tree"
28

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

instance decodeMetrics :: DecodeJson Metrics where
  decodeJson json = do
    obj <- decodeJson json
36
    d   <- obj .: "data"
37
    pure $ Metrics { "data": d }
38 39 40 41 42
instance encodeMetrics :: EncodeJson Metrics where
  encodeJson (Metrics { "data": d }) =
       "data" := encodeJson d
    ~> jsonEmptyObject

43
type Loaded  = Array TreeNode
44

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

57
  }
58

59 60
getMetricsHash :: Session -> Tuple Reload (Record Path) -> Aff String
getMetricsHash session (_ /\ { corpusId, limit, listId, tabType }) = do
61
  get session $ ChartHash { chartType: ChartTree, listId: mListId, tabType } (Just corpusId)
62 63
  where
    mListId = if listId == 0 then Nothing else (Just listId)
64

65
chartUrl :: Record Path -> SessionRoute
66 67 68
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartTree, limit, listId: mListId, tabType} (Just corpusId)
  where
    mListId = if listId == 0 then Nothing else (Just listId)
69 70 71 72 73 74 75

handleResponse :: HashedResponse Metrics -> Loaded
handleResponse (HashedResponse { value: Metrics ms }) = ms."data"

mkRequest :: Session -> ReloadPath -> GUC.Request
mkRequest session (_ /\ path@{ corpusId, limit, listId, tabType }) = GUC.makeGetRequest session $ chartUrl path

76
tree :: Record Props -> R.Element
77
tree props = R.createElement treeCpt props []
78

79
treeCpt :: R.Component Props
80
treeCpt = R2.hooksComponent thisModule "tree" cpt
81
  where
82
    cpt {path, session} _ = do
83
      reload <- R.useState' 0
84
      pure $ metricsWithCacheLoadView {
85
          getMetricsHash
86 87 88 89 90 91 92
        , handleResponse
        , loaded
        , mkRequest: mkRequest session
        , path
        , reload
        , session
        }
93

94 95
loaded :: Record MetricsProps -> Loaded -> R.Element
loaded { path, reload, session } loaded =
96 97
  H.div {} [
    U.reloadButton reload
98
  , U.chartUpdateButton { chartType: ChartTree, path, reload, session }
99 100
  , chart (scatterOptions loaded)
  ]