Skip to content

Commit 7a119f0

Browse files
committed
[gephi-lite] add examples on custom-scripts page
1 parent ea681d3 commit 7a119f0

1 file changed

Lines changed: 71 additions & 61 deletions

File tree

gephi-lite/docs/user-manual/custom-scripts.md

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ In many places, this lets you write JavaScript code instead of using the standar
1010

1111
From the **Filters** menu, choosing **Custom script** opens the script editor, allowing you to implement this function:
1212

13-
````js
13+
```js
1414
/**
1515
* Define a custom filter function.
1616
* The function is executed for each node.
@@ -24,38 +24,42 @@ From the **Filters** menu, choosing **Custom script** opens the script editor, a
2424
function filter(id, attributes, graph) {
2525
//
2626
// 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-
// ```
4927
//
5028
return true;
5129
}
52-
````
30+
```
31+
32+
### Examples
33+
34+
- Keep nodes that have a property 'age' superior than `18` :
35+
36+
```js
37+
function filter(id, attributes, graph) {
38+
return attributes.age > 18;
39+
}
40+
```
41+
42+
- Filtering node that have a property 'age' below 18 and with a degree inferior to 10 :
43+
44+
```js
45+
function filter(id, attributes, graph) {
46+
return attributes.age < 18 && graph.degree(id) < 10;
47+
}
48+
```
49+
50+
- Filtering nodes on which the property 'job' is not defined
51+
52+
```js
53+
function filter(id, attributes, graph) {
54+
return attributes.job !== undefined;
55+
}
56+
```
5357

5458
## Custom layout
5559

5660
In the **Layout** menu, selecting **Custom layout** opens the script editor, where you can implement this function:
5761

58-
````js
62+
```js
5963
/**
6064
* Function that returns coordinates for the specified node.
6165
*
@@ -68,32 +72,37 @@ In the **Layout** menu, selecting **Custom layout** opens the script editor, whe
6872
function nodeCoordinates(id, attributes, index, graph) {
6973
//
7074
// 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-
// ```
8775
//
76+
return { x: attributes.x, y: attributes.y };
77+
}
78+
```
79+
80+
### Examples
81+
82+
- Random layout on a 1000x1000 space
83+
84+
```js
85+
function nodeCoordinates(id, attributes, index, graph) {
8886
return { x: Math.random() * 1000, y: Math.random() * 1000 };
8987
}
90-
````
88+
```
89+
90+
- Circular layout
91+
92+
```js
93+
function nodeCoordinates(id, attributes, index, graph) {
94+
return {
95+
x: Math.cos((index * (Math.PI * 2)) / graph.order) * 500,
96+
y: Math.sin((index * (Math.PI * 2)) / graph.order) * 500,
97+
};
98+
}
99+
```
91100

92101
## Scripted node/edge attribute
93102

94103
On the **Data** page, choosing **Create nodes scripted attribute** in the **Data creation** menu opens the script editor, allowing you to implement this function:
95104

96-
````js
105+
```js
97106
/**
98107
* Function that returns a new attribute value for the specified node/edge.
99108
*
@@ -106,27 +115,28 @@ On the **Data** page, choosing **Create nodes scripted attribute** in the **Data
106115
function addAttribut(id, attributes, index, graph) {
107116
//
108117
// 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-
// ```
126118
//
127119
return Math.random();
128120
}
129-
````
121+
```
122+
123+
### Examples
124+
125+
- If you have an attribut named 'valid' which take 0 or 1 and you want to cast it into a boolean
126+
127+
```js
128+
function addAttribut(id, attributes, index, graph) {
129+
return attributes.valid === 1;
130+
}
131+
```
132+
133+
- If you have attributs named 'firstname' and 'lastname' and you want to concatenate them (usefull for graph label)
134+
135+
```js
136+
function addAttribut(id, attributes, index, graph) {
137+
return (attributes.firstname || "") + " " + (attributes.lastname || ");
138+
}
139+
```
130140
131141
## Script editor
132142

0 commit comments

Comments
 (0)