I am trying to create a compound node in Cytoscape.js with labels and an edge from the child node to another node. If you run the snippet below and move the b
north of a
, you see that the label of a
is below the edge between b
and a-1
, which I don’t want.
I could set z-compound-depth
of a
and a-1
to top
, but then the edge would not be visible inside of a
.
I want to position the edge on top of a
, but below the label of a
. How can I achieve this?
JavaScript
x
42
42
1
cytoscape({
2
container: document.querySelector(".graph"),
3
elements: [
4
{
5
data: {
6
id: "a",
7
},
8
},
9
{
10
data: {
11
id: "a-1",
12
parent: "a",
13
},
14
},
15
{
16
data: {
17
id: "b",
18
},
19
},
20
{
21
data: {
22
id: "a-1 -> b",
23
source: "a-1",
24
target: "b",
25
},
26
},
27
],
28
style: [
29
{
30
selector: "node",
31
style: {
32
label: "data(id)",
33
},
34
},
35
{
36
selector: "#a,#a-1",
37
style: {
38
// "z-compound-depth": "top", // moves the edge below the a label, but also below the a node
39
},
40
},
41
],
42
});
JavaScript
1
7
1
body {
2
margin: 0;
3
}
4
5
.graph {
6
height: 100vh;
7
}
JavaScript
1
2
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.17.0/cytoscape.min.js"></script>
2
<div class="graph"></div>
Advertisement
Answer
I know this does not answer your question. But you ask for something that is not simple and efficient to achieve. If you only need better visualization, applying line-opacity
to the edge’s line style is an alternate approach.
JavaScript
1
50
50
1
cytoscape({
2
container: document.querySelector(".graph"),
3
elements: [
4
{
5
data: {
6
id: "a",
7
},
8
},
9
{
10
data: {
11
id: "a-1",
12
parent: "a",
13
},
14
},
15
{
16
data: {
17
id: "b",
18
},
19
},
20
{
21
data: {
22
id: "a-1 -> b",
23
source: "a-1",
24
target: "b",
25
},
26
},
27
],
28
style: [
29
{
30
selector: "node",
31
style: {
32
label: "data(id)",
33
},
34
},
35
{
36
selector: "edge",
37
style: {
38
label: "",
39
"line-color": "red",
40
"line-opacity": 0.5
41
},
42
},
43
{
44
selector: "#a,#a-1",
45
style: {
46
// "z-compound-depth": "top", // moves the edge below the a label, but also below the a node
47
},
48
},
49
],
50
});
JavaScript
1
7
1
body {
2
margin: 0;
3
}
4
5
.graph {
6
height: 100vh;
7
}
JavaScript
1
2
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.17.0/cytoscape.min.js"></script>
2
<div class="graph"></div>