Pie.purs 4.44 KB
Newer Older
1
module Gargantext.Pages.Corpus.Chart.Pie where
2

3 4 5
import Data.String (take, joinWith, Pattern(..), split, length)
import Data.Array (foldl, zip, filter)
import Data.Array as A
6 7
import Data.Tuple (Tuple(..))
import Data.Map as Map
8
import Data.Int (toNumber)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import Data.Map (Map)
import Data.Argonaut (class DecodeJson, decodeJson, (.?))
import Data.Maybe (Maybe(..), maybe)
import Effect.Aff (Aff)
import Gargantext.Config -- (End(..), Path(..), TabType, toUrl)
import Gargantext.Config.REST (get)
import React (ReactClass, ReactElement, createElement)
import Thermite (Spec, Render, defaultPerformAction, simpleSpec, createClass)
import Gargantext.Prelude
import Gargantext.Types (TermList(..))
import Gargantext.Components.Loader as Loader
import Gargantext.Components.Charts.Options.ECharts
import Gargantext.Components.Charts.Options.Type
import Gargantext.Components.Charts.Options.Series
import Gargantext.Components.Charts.Options.Color
import Gargantext.Components.Charts.Options.Font
import Gargantext.Components.Charts.Options.Data

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

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

36
instance decodeChartMetrics :: DecodeJson ChartMetrics where
37
  decodeJson json = do
38 39 40 41 42 43 44
    obj <- decodeJson json
    d   <- obj .? "data"
    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 50 51 52
    obj   <- decodeJson json
    d <- obj .? "dates"
    c <- obj .? "count"
    pure $ HistoMetrics { dates : d , count: c}
53

54
type Loaded = HistoMetrics
55 56 57 58 59

loadedMetricsSpec :: Spec {} (Loader.InnerProps Path Loaded ()) Void
loadedMetricsSpec = simpleSpec defaultPerformAction render
  where
    render :: Render {} (Loader.InnerProps Path Loaded ()) Void
60 61 62 63 64 65
    render dispatch {loaded : metricsData} {} _ = [chart (chartOptions metricsData)]

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

loadedMetricsSpecPie :: Spec {} (Loader.InnerProps Path Loaded ()) Void
loadedMetricsSpecPie = simpleSpec defaultPerformAction render
75
  where
76 77 78 79 80 81 82 83
    render :: Render {} (Loader.InnerProps Path Loaded ()) Void
    render dispatch {loaded : metricsData} {} _ = [chart (chartOptionsPie metricsData)]

chartOptionsPie :: HistoMetrics -> Options
chartOptionsPie (HistoMetrics { dates: dates', count: count'}) = Options
  { mainTitle : "Pie"
  , subTitle  : "Distribution by GraphTerm"
  , xAxis     : xAxis' []
84
  , yAxis     : yAxis' { position: "", show: false, min:0}
85 86 87 88 89 90 91 92
  , 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}" }
  }


metricsLoader :: Loader.Props' Path HistoMetrics -> ReactElement
93
metricsLoader props = createElement metricsLoaderClass props []
94 95 96 97 98 99 100 101
  where
    metricsLoaderClass :: ReactClass (Loader.Props Path HistoMetrics)
    metricsLoaderClass = Loader.createLoaderClass "MetricsLoader" getMetrics

    getMetrics :: Path -> Aff HistoMetrics
    getMetrics {corpusId, tabType:tabType} = do
      ChartMetrics ms <- get $ toUrl Back (Chart {chartType: ChartPie, tabType: tabType}) $ Just corpusId
      pure ms."data"
102 103 104

pieSpec :: Spec {} Path Void
pieSpec = simpleSpec defaultPerformAction render
105 106 107 108 109 110 111 112 113 114
  where
    render :: Render {} Path Void
    render dispatch path {} _ =
      [ metricsLoader
        { path
        , component: createClass "LoadedMetrics" loadedMetricsSpecPie (const {})
        } ]

barSpec :: Spec {} Path Void
barSpec = simpleSpec defaultPerformAction render
115 116 117 118 119 120 121
  where
    render :: Render {} Path Void
    render dispatch path {} _ =
      [ metricsLoader
        { path
        , component: createClass "LoadedMetrics" loadedMetricsSpec (const {})
        } ]
122