Commit 9f1d85a7 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[graph] implement node scaling when label is scaled

parent edc959c8
...@@ -273,6 +273,7 @@ controlsCpt = R.memo' $ here.component "controls" cpt where ...@@ -273,6 +273,7 @@ controlsCpt = R.memo' $ here.component "controls" cpt where
-- search button -- search button
-- search topics -- search topics
labelSizeButton { forceAtlasState labelSizeButton { forceAtlasState
, graph
, sigmaRef , sigmaRef
, state: labelSize } , state: labelSize }
] ]
......
...@@ -6,6 +6,8 @@ module Gargantext.Components.GraphExplorer.Toolbar.SlideButton ...@@ -6,6 +6,8 @@ module Gargantext.Components.GraphExplorer.Toolbar.SlideButton
, mouseSelectorSizeSlider , mouseSelectorSizeSlider
) where ) where
import Data.Array as A
import Data.Map as Map
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
import Data.Number as DN import Data.Number as DN
import Prelude import Prelude
...@@ -15,7 +17,10 @@ import Reactix.DOM.HTML as H ...@@ -15,7 +17,10 @@ import Reactix.DOM.HTML as H
import Toestand as T import Toestand as T
import Gargantext.Components.Bootstrap.Types (ComponentStatus(Disabled)) import Gargantext.Components.Bootstrap.Types (ComponentStatus(Disabled))
import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Components.GraphExplorer.Utils as GEU
import Gargantext.Hooks.Sigmax as Sigmax import Gargantext.Hooks.Sigmax as Sigmax
import Gargantext.Hooks.Sigmax.Graphology as Graphology
import Gargantext.Hooks.Sigmax.Sigma as Sigma import Gargantext.Hooks.Sigmax.Sigma as Sigma
import Gargantext.Hooks.Sigmax.Types as SigmaxTypes import Gargantext.Hooks.Sigmax.Types as SigmaxTypes
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
...@@ -68,6 +73,7 @@ sizeButtonCpt = here.component "sizeButton" cpt where ...@@ -68,6 +73,7 @@ sizeButtonCpt = here.component "sizeButton" cpt where
type LabelSizeButtonProps = type LabelSizeButtonProps =
( forceAtlasState :: T.Box SigmaxTypes.ForceAtlasState ( forceAtlasState :: T.Box SigmaxTypes.ForceAtlasState
, graph :: T.Box SigmaxTypes.SGraph
, sigmaRef :: R.Ref Sigmax.Sigma , sigmaRef :: R.Ref Sigmax.Sigma
, state :: T.Box Number) , state :: T.Box Number)
...@@ -76,13 +82,19 @@ labelSizeButton = R2.leaf labelSizeButtonCpt ...@@ -76,13 +82,19 @@ labelSizeButton = R2.leaf labelSizeButtonCpt
labelSizeButtonCpt :: R.Component LabelSizeButtonProps labelSizeButtonCpt :: R.Component LabelSizeButtonProps
labelSizeButtonCpt = here.component "labelSizeButton" cpt labelSizeButtonCpt = here.component "labelSizeButton" cpt
where where
cpt { forceAtlasState, sigmaRef, state} _ = do cpt { forceAtlasState, graph, sigmaRef, state} _ = do
graph' <- T.useLive T.unequal graph
let minLabelSize = 1.0
let maxLabelSize = 30.0
let defaultLabelSize = 14.0
pure $ sizeButton { pure $ sizeButton {
state state
, caption: "Label size" , caption: "Label size"
, forceAtlasState , forceAtlasState
, min: 1.0 , min: minLabelSize
, max: 30.0 , max: maxLabelSize
, onChange: \e -> do , onChange: \e -> do
let sigma = R.readRef sigmaRef let sigma = R.readRef sigmaRef
let newValue' = DN.fromString $ R.unsafeEventValue e let newValue' = DN.fromString $ R.unsafeEventValue e
...@@ -90,6 +102,15 @@ labelSizeButtonCpt = here.component "labelSizeButton" cpt ...@@ -90,6 +102,15 @@ labelSizeButtonCpt = here.component "labelSizeButton" cpt
Nothing -> pure unit Nothing -> pure unit
Just newValue -> Just newValue ->
Sigmax.dependOnSigma sigma "[labelSizeButton] sigma: Nothing" $ \s -> do Sigmax.dependOnSigma sigma "[labelSizeButton] sigma: Nothing" $ \s -> do
let ratio = (newValue - minLabelSize) / (defaultLabelSize - minLabelSize)
let nodes = SigmaxTypes.graphNodes graph'
let nodesResized = (\n@{ size } -> n { size = size * ratio }) <$> nodes
let nodesMap = SigmaxTypes.idMap nodesResized
Graphology.forEachNode (Sigma.graph s) $ \{ id } -> do
case Map.lookup id nodesMap of
Nothing -> pure unit
Just { size } -> Graphology.mergeNodeAttributes (Sigma.graph s) id { size }
Sigma.setSettings s { Sigma.setSettings s {
defaultLabelSize: newValue defaultLabelSize: newValue
, drawLabels: true , drawLabels: true
......
...@@ -18,6 +18,13 @@ export function _mergeNodeAttributes(g, name, attrs) { ...@@ -18,6 +18,13 @@ export function _mergeNodeAttributes(g, name, attrs) {
return g.mergeNodeAttributes(name, attrs); return g.mergeNodeAttributes(name, attrs);
} }
export function _forEachNode(g, fn) {
return g.forEachNode(function(_name, attrs) {
// NOTE: fn is an effectful function, it wraps `do` in a separate function
return fn(attrs)();
})
}
export function _addEdge(g, source, target, e) { export function _addEdge(g, source, target, e) {
//return g.addEdge(source, target, e); //return g.addEdge(source, target, e);
......
...@@ -27,6 +27,7 @@ foreign import _addNode :: EffectFn3 Graph String (Record Types.Node) String ...@@ -27,6 +27,7 @@ foreign import _addNode :: EffectFn3 Graph String (Record Types.Node) String
foreign import _updateNode :: EffectFn3 Graph String (Record Types.Node -> Record Types.Node) Unit foreign import _updateNode :: EffectFn3 Graph String (Record Types.Node -> Record Types.Node) Unit
foreign import _addEdge :: EffectFn4 Graph String String (Record Types.Edge) String foreign import _addEdge :: EffectFn4 Graph String String (Record Types.Edge) String
foreign import _mergeNodeAttributes :: forall a. EffectFn3 Graph String a Unit foreign import _mergeNodeAttributes :: forall a. EffectFn3 Graph String a Unit
foreign import _forEachNode :: EffectFn2 Graph (Record Types.Node -> Effect Unit) Unit
--foreign import _updateEdge :: EffectFn4 Graph String String (Record Types.Edge) String --foreign import _updateEdge :: EffectFn4 Graph String String (Record Types.Edge) String
foreign import _mapNodes :: forall a. Fn2 Graph (Record Types.Node -> a) (Array a) foreign import _mapNodes :: forall a. Fn2 Graph (Record Types.Node -> a) (Array a)
foreign import _filterNodes :: Fn2 Graph (Record Types.Node -> Boolean) (Array Types.NodeId) foreign import _filterNodes :: Fn2 Graph (Record Types.Node -> Boolean) (Array Types.NodeId)
...@@ -65,9 +66,7 @@ updateNode g node@{ id, borderColor, color, equilateral, hidden, highlighted, ty ...@@ -65,9 +66,7 @@ updateNode g node@{ id, borderColor, color, equilateral, hidden, highlighted, ty
mergeNodeAttributes :: forall a. Graph -> Types.NodeId -> a -> Effect Unit mergeNodeAttributes :: forall a. Graph -> Types.NodeId -> a -> Effect Unit
mergeNodeAttributes = runEffectFn3 _mergeNodeAttributes mergeNodeAttributes = runEffectFn3 _mergeNodeAttributes
forEachNode :: Graph -> (Record Types.Node -> Effect Unit) -> Effect Unit forEachNode :: Graph -> (Record Types.Node -> Effect Unit) -> Effect Unit
-- TODO Check this: how does FFI translate function of two arguments forEachNode = runEffectFn2 _forEachNode
-- into PS \x y ?
forEachNode g fn = pure $ g ... "forEachNode" $ [\_ n -> fn n]
mapNodes :: forall a. Graph -> (Record Types.Node -> a) -> Array a mapNodes :: forall a. Graph -> (Record Types.Node -> a) -> Array a
mapNodes = runFn2 _mapNodes mapNodes = runFn2 _mapNodes
filterNodes :: Graph -> (Record Types.Node -> Boolean) -> Array Types.NodeId filterNodes :: Graph -> (Record Types.Node -> Boolean) -> Array Types.NodeId
......
...@@ -9,6 +9,7 @@ import Data.Map as Map ...@@ -9,6 +9,7 @@ import Data.Map as Map
import Data.Maybe (Maybe(..), fromJust) import Data.Maybe (Maybe(..), fromJust)
import Data.Sequence as Seq import Data.Sequence as Seq
import Data.Set as Set import Data.Set as Set
import Data.Traversable (class Traversable)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Partial.Unsafe (unsafePartial) import Partial.Unsafe (unsafePartial)
import Prelude (class Eq, class Show, map, ($), (&&), (==), (||), (<$>), (<), mod, not) import Prelude (class Eq, class Show, map, ($), (&&), (==), (||), (<$>), (<), mod, not)
...@@ -107,19 +108,20 @@ graphEdges (Graph {edges}) = edges ...@@ -107,19 +108,20 @@ graphEdges (Graph {edges}) = edges
graphNodes :: SGraph -> Seq.Seq (Record Node) graphNodes :: SGraph -> Seq.Seq (Record Node)
graphNodes (Graph {nodes}) = nodes graphNodes (Graph {nodes}) = nodes
idMap :: forall r t. Traversable t => t { id :: String | r } -> Map.Map String { id :: String | r }
idMap xs = Map.fromFoldable $ (\x@{ id } -> Tuple id x) <$> xs
edgesGraphMap :: SGraph -> EdgesMap edgesGraphMap :: SGraph -> EdgesMap
edgesGraphMap graph = edgesGraphMap graph = idMap $ graphEdges graph
Map.fromFoldable $ map (\e -> Tuple e.id e) $ graphEdges graph
edgesFilter :: (Record Edge -> Boolean) -> SGraph -> SGraph edgesFilter :: (Record Edge -> Boolean) -> SGraph -> SGraph
edgesFilter f (Graph {edges, nodes}) = Graph { edges: Seq.filter f edges, nodes } edgesFilter f (Graph {edges, nodes}) = Graph { edges: Seq.filter f edges, nodes }
nodesMap :: Seq.Seq (Record Node) -> NodesMap nodesMap :: Seq.Seq (Record Node) -> NodesMap
nodesMap nodes = Map.fromFoldable $ map (\n -> Tuple n.id n) nodes nodesMap = idMap
nodesGraphMap :: SGraph -> NodesMap nodesGraphMap :: SGraph -> NodesMap
nodesGraphMap graph = nodesGraphMap graph = idMap $ graphNodes graph
nodesMap $ graphNodes graph
nodesFilter :: (Record Node -> Boolean) -> SGraph -> SGraph nodesFilter :: (Record Node -> Boolean) -> SGraph -> SGraph
nodesFilter f (Graph {edges, nodes}) = Graph { edges, nodes: Seq.filter f nodes } nodesFilter f (Graph {edges, nodes}) = Graph { edges, nodes: Seq.filter f nodes }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment