@@ -3,9 +3,136 @@ title: Custom scripts
33sidebar_position : 4
44---
55
6- TODO
7- - Since Gephi Lite is a web application, we have a good and simple scripting language: JavaScript
8- - There are various places where users can write some JavaScript code rather than use existing algorithm options:
9- - Filters
10- - Layout
11- - Create new nodes or edges attribute
6+ Gephi-Lite does not have a plugin marketplace, but it includes a native scripting language: JavaScript.
7+ In many places, this lets you write JavaScript code instead of using the standard features.
8+
9+ ## Nodes/Edges filtering
10+
11+ From the ** Filters** menu, choosing ** Custom script** opens the script editor, allowing you to implement this function:
12+
13+ ```` js
14+ /**
15+ * Define a custom filter function.
16+ * The function is executed for each node.
17+ * If it returns true, the node is included in the result set; otherwise, it is excluded.
18+ *
19+ * @param {string} id ID of the item
20+ * @param {GraphNode} attributes Attributes of the item
21+ * @param {AbstractGraph<GraphNode, GraphEdge>} graph Graphology instance (https://graphology.github.io/)
22+ * @return {boolean} TRUE if the item should be kept in the graph, FALSE to filter it
23+ */
24+ function filter (id , attributes , graph ) {
25+ //
26+ // Your code goes here
27+ // ~~~~~~~~~~~~~~~~~~~~
28+ //
29+ // Write here your own function that filter nodes.
30+ // For each nodes, this function will be called, and if its result is true, the node is kept.
31+ //
32+ // Example 1: keeping nodes that have a property 'age' superior than 18
33+ // --------------------------------------------------------------------
34+ // ```
35+ // return attributes.age > 18;
36+ // ```
37+ //
38+ // Example 2: filtering node that have a property 'age' below 18 and with a degree inferior to 10
39+ // ----------------------------------------------------------------------------------------------
40+ // ```
41+ // return attributes.age < 18 ? graph.degree(id) < 10 : true;
42+ // ```
43+ //
44+ // Example 3: filtering nodes on which the property 'job' is not defined
45+ // ---------------------------------------------------------------------
46+ // ```
47+ // return attributes.job !== undefined;
48+ // ```
49+ //
50+ return true ;
51+ }
52+ ````
53+
54+ ## Custom layout
55+
56+ In the ** Layout** menu, selecting ** Custom layout** opens the script editor, where you can implement this function:
57+
58+ ```` js
59+ /**
60+ * Function that returns coordinates for the specified node.
61+ *
62+ * @param {string} id The ID of the node
63+ * @param {GraphNode} attributes Attributes of the node
64+ * @param {number} index The index position of the node in the graph
65+ * @param {AbstractGraph<GraphNode, GraphEdge>} graph The graphology instance (https://graphology.github.io/)
66+ * @returns {x: number, y: number} The computed coordinates of the node
67+ */
68+ function nodeCoordinates (id , attributes , index , graph ) {
69+ //
70+ // Your code goes here
71+ // ~~~~~~~~~~~~~~~~~~~~
72+ //
73+ // Write here your own function that spatialized nodes.
74+ // For each node, this function will be called to get its coordinates.
75+ //
76+ // Example 1: A random layout on a 1000x1000 space
77+ // ------------------------------------------------------------------------
78+ // ```
79+ // return { x: Math.random() * 1000, y: Math.random() * 1000 };
80+ // ```
81+ //
82+ // Example 2: Circular layout
83+ // ----------------------------------------------------------------------
84+ // ```
85+ // return { x: Math.cos(index * (Math.PI *2) / graph.order) * 500, y: Math.sin(index * (Math.PI *2) / graph.order) * 500 };
86+ // ```
87+ //
88+ return { x: Math .random () * 1000 , y: Math .random () * 1000 };
89+ }
90+ ````
91+
92+ ## Scripted node/edge attribute
93+
94+ On the ** Data** page, choosing ** Create nodes scripted attribute** in the ** Data creation** menu opens the script editor, allowing you to implement this function:
95+
96+ ```` js
97+ /**
98+ * Function that returns a new attribute value for the specified node/edge.
99+ *
100+ * @param {string} id The ID of the node/edge
101+ * @param {GraphNode} attributes Attributes of the node/edge
102+ * @param {number} index The index position of the node/edge in the graph
103+ * @param {AbstractGraph<GraphNode, GraphEdge>} graph Graphology instance (https://graphology.github.io/)
104+ * @returns number|string|boolean|null|undefined" The value of the new node/edge's attribut
105+ */
106+ function addAttribut (id , attributes , index , graph ) {
107+ //
108+ // Your code goes here
109+ // ~~~~~~~~~~~~~~~~~~~~
110+ //
111+ // Write here your own function that returns the value for your new attribut.
112+ //
113+ // Example 1: If you have an attribut named 'valid' which take 0 or 1,
114+ // you can cast it into a boolean
115+ // ------------------------------------------------------------------------
116+ // ```
117+ // return attributes.valid === 1;
118+ // ```
119+ //
120+ // Example 2: If you have attributs named 'firstname' and 'lastname'
121+ // you can concatenate them (usefull for graph label)
122+ // -----------------------------------------------------------------------
123+ // ```
124+ // return (attributes.firstname || "") + " " + (attributes.lastname || ");
125+ // ```
126+ //
127+ return Math .random ();
128+ }
129+ ````
130+
131+ ## Script editor
132+
133+ The script editor is displayed in a modal.
134+
135+ ![ Script editor] ( ./assets/script-editor.png )
136+
137+ The editor is based on [ Monaco] ( https://microsoft.github.io/monaco-editor/ ) , so if you use VS Code, it should feel familiar.
138+ Types are defined for each function parameter (such as ` attributes ` or ` graph ` ), enabling autocompletion.
0 commit comments