Utils.purs 1.28 KB
Newer Older
1 2 3
module Gargantext.Components.GraphExplorer.Utils where

import Data.Maybe (Maybe(..))
4 5 6 7 8

import Gargantext.Prelude

import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Hooks.Sigmax.Types as ST
9
import Gargantext.Utils.Array as GUA
10 11 12 13 14 15 16 17


stEdgeToGET :: Record ST.Edge -> GET.Edge
stEdgeToGET { _original } = _original

stNodeToGET :: Record ST.Node -> GET.Node
stNodeToGET { id, label, x, y, _original: GET.Node { attributes, size, type_ } } = GET.Node {
    attributes
18
  , children: []
19 20 21 22 23 24 25
  , id_: id
  , label
  , size
  , type_
  , x
  , y
  }
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

normalizeNodes :: Array GET.Node -> Array GET.Node
normalizeNodes ns = map normalizeNode ns
  where
    xs = map (\(GET.Node { x }) -> x) ns
    ys = map (\(GET.Node { y }) -> y) ns
    mMinx = GUA.min xs
    mMaxx = GUA.max xs
    mMiny = GUA.min ys
    mMaxy = GUA.max ys
    mXrange = do
      minx <- mMinx
      maxx <- mMaxx
      pure $ maxx - minx
    mYrange = do
      miny <- mMiny
      maxy <- mMaxy
      pure $ maxy - miny
    xdivisor = case mXrange of
      Nothing -> 1.0
      Just xdiv -> 1.0 / xdiv
    ydivisor = case mYrange of
      Nothing -> 1.0
      Just ydiv -> 1.0 / ydiv
    normalizeNode (GET.Node n@{ x, y }) = GET.Node $ n { x = x * xdivisor
                                                       , y = y * ydivisor }