Skip to content

Commit 822138a

Browse files
committed
Added context menu to sankey node titles that will help traverse pathways.
1 parent 0a87f0f commit 822138a

File tree

6 files changed

+127
-25
lines changed

6 files changed

+127
-25
lines changed

contextmenu.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function contextmenu(d){
2+
//d3.select(".custom_menu").remove();
3+
d3.event.preventDefault();
4+
menu = d3.select(".custom_menu")
5+
.style("left", d3.event.pageX + "px")
6+
.style("top", d3.event.pageY + "px")
7+
.style("position","absolute")
8+
.style("display", 'block');
9+
10+
menu.selectAll('li')
11+
.on("click", function(e, i){
12+
contextOptions(d, i);
13+
14+
});
15+
16+
}
17+
18+
d3.select('body').on('click', function() {
19+
d3.select('.custom_menu').style('display', 'none');
20+
});
21+
22+
function contextOptions(d, i){
23+
console.log(i);
24+
switch(i){
25+
case(0):
26+
console.log(focus);
27+
focus.children.forEach(function(n){
28+
if(n.data.name == d.name){
29+
transition(n);
30+
}else{
31+
console.log(d.name + " not in focus");
32+
}
33+
});
34+
// if (focus.children.some(n => )) {
35+
// transition(n);
36+
// }else{
37+
// console.log(d.name + " not in focus");
38+
// }
39+
break;
40+
case(1):
41+
break;
42+
}
43+
}
20.9 KB
Binary file not shown.

index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<link rel="stylesheet" href="styles.css">
77
<script src="https://d3js.org/d3.v4.min.js"></script>
88
<script src="https://cdn.jsdelivr.net/gh/holtzy/D3-graph-gallery@master/LIB/sankey.js"></script>
9+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
910
</head>
1011
<body>
1112
<div class="container" align="center" >
@@ -15,15 +16,22 @@
1516
<div class="md-form mb-3">
1617
<input class="form-control" type="text" placeholder="Search" aria-label="Search" style="background: none">
1718
</div>
18-
<text style="color:white; font-size:12px">//Grey matter:</text>
19+
<text style="color:white; font-size:13px">//Grey matter:</text>
1920
<div id="treemapChart"></div>
20-
<text style="color:white; font-size:12px">//White matter:</text>
21+
<text style="color:white; font-size:13px">//White matter:</text>
2122
<div id="sankeyChart">
2223
</div>
2324
</div>
2425
</div>
2526
</div>
27+
28+
<ul class='custom_menu'>
29+
<li data-action = "view_outgoing">view outgoing</li>
30+
<li data-action = "view_incoming">view incoming</li>
31+
</ul>
32+
2633
<script src="treemap.js"></script>
34+
<script src="contextmenu.js"></script>
2735
<script src="sankey.js"></script>
2836
</body>
2937
</html>

sankey.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,22 @@ function loadSankey(graph){
9292
return d.name + "\n" + format(d.value); });
9393

9494
// add in the title for the nodes
95-
node.append("text")
95+
nodeText = node.append("text")
9696
.attr("x", -6)
9797
.attr("y", function(d) { return d.dy / 2; })
9898
.attr("dy", ".35em")
9999
.attr("text-anchor", "end")
100100
.attr("transform", null)
101101
.text(function(d) { return d.name; })
102-
.filter(function(d) { return d.x < width / 2; })
102+
.on("contextmenu", function(d) {
103+
contextmenu(d);
104+
})
105+
.filter(function(d) { return d.x < width / 2; })
103106
.attr("x", 6 + sankey.nodeWidth())
104107
.attr("text-anchor", "start")
105108
.style("fill", function(d) {
106-
return d.value > 0 ? "black": "white";
107-
});
109+
return d.value > 0 ? "black": "white";});
110+
108111

109112
// the function for moving the nodes
110113
function dragmove(d) {

styles.css

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11

2+
3+
@font-face {
4+
font-family: LiberationSans;
5+
src: url('fonts/LiberationSans-Regular-webfont.woff');
6+
}
7+
8+
29
body {
310
background-color: #24282e;
11+
font-family: LiberationSans;
412
}
513

614
#treemapChart {
715
width: 750px;
816
overflow:auto;
917
}
18+
#sankeyChart {
19+
overflow: hidden;
20+
}
1021

1122
text {
1223
pointer-events: none;
13-
font-family: "courier new";
24+
font-family: "LiberationSans";
1425

1526
}
1627
.grandparent text {
@@ -70,7 +81,7 @@ body {
7081
overflow:none;
7182
}
7283
.textdiv .title {
73-
font-family: "courier new";
84+
font-family: "LiberationSans";
7485
font-size: 102%;
7586
font-weight: bold;
7687
margin-top: 8px;
@@ -89,7 +100,8 @@ body {
89100
}
90101

91102
.node text {
92-
pointer-events: none;
103+
cursor: move;
104+
pointer-events: auto;
93105
font-size: 13px;
94106
}
95107

@@ -100,5 +112,31 @@ body {
100112

101113
.link:hover {
102114
stroke-opacity: .5;
103-
stroke: #8f3d3d;
115+
}
116+
117+
.custom_menu {
118+
padding: 0;
119+
display: none;
120+
list-style-type:none;
121+
z-index: 1000;
122+
position: absolute;
123+
overflow: hidden;
124+
border: 1px solid black;
125+
white-space: nowrap;
126+
font-family: LiberationSans;
127+
font-size: 14px;
128+
129+
background: black;
130+
opacity: .7;
131+
color: white;
132+
133+
}
134+
135+
.custom_menu li {
136+
padding: 1px 10px;
137+
cursor: pointer;
138+
}
139+
140+
.custom_menu li:hover {
141+
background-color: #8f3d3d;
104142
}

treemap.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,29 @@ var margin = {
1717
width = divWidth - 1,
1818
height = 200 - margin.top,
1919
formatNumber = d3.format(","),
20-
transitioning;
20+
transitioning,
21+
data,
22+
root, focus;
2123

2224
// sets x and y scale to determine size of visible boxes
23-
var x = d3.scaleLinear().domain([0, width]).range([0, width]);
25+
let x = d3.scaleLinear().domain([0, width]).range([0, width]);
2426

25-
var y = d3.scaleLinear().domain([0, height]).range([0, height]);
27+
let y = d3.scaleLinear().domain([0, height]).range([0, height]);
2628
// adding a color scale
27-
var colorwheel = 0;
29+
let colorwheel = 0;
2830

29-
var color = d3.scaleOrdinal(d3.schemeCategory20);
31+
let color = d3.scaleOrdinal(d3.schemeCategory20);
3032

31-
var treemap = d3.treemap().size([width, height]).paddingInner(0).round(false);
33+
let treemap = d3.treemap().size([width, height]).paddingInner(0).round(false);
3234

33-
var svg = d3.select('#' + el_id)
35+
let svg = d3.select('#' + el_id)
3436
.append("svg").attr("id", "treemap")
3537
.attr("width", width)
3638
.attr("height", height + margin.top)
3739
.append("g").attr("transform", "translate(0," + margin.top + ")")
3840
.style("shape-rendering", "crispEdges");
3941

40-
var grandparent = svg.append("g")
42+
let grandparent = svg.append("g")
4143
.attr("class", "grandparent");
4244

4345
grandparent.append("rect")
@@ -48,25 +50,31 @@ grandparent.append("text")
4850
.attr("x", 6).attr("y", 6 - margin.top)
4951
.attr("dy", ".75em");
5052

51-
d3.json("whitematter.json", function(data) {
52-
var root = d3.hierarchy(data);
53+
d3.json("whitematter.json", function(d) {
54+
data = d;
55+
root = d3.hierarchy(data);
5356
treemap(root.sum(function(d) {
5457
return d.value;
5558
}).sort(function(a, b) {
5659
return b.height - a.height || b.value - a.value
5760
}));
5861
display(root);
5962

63+
});
64+
6065
function display(d) {
66+
67+
focus = d;
68+
6169
// write text into grandparent
6270
// and activate click's handler
6371
grandparent.datum(d.parent).on("click", transition).select("text").text(name(d));
6472
// grandparent color
6573
grandparent.datum(d.parent).select("rect").attr("fill", function() {
6674
return 'grey'
6775
});
68-
var g1 = svg.insert("g", ".grandparent").datum(d).attr("class", "depth");
69-
var g = g1.selectAll("g").data(d.children).enter().append("g");
76+
let g1 = svg.insert("g", ".grandparent").datum(d).attr("class", "depth");
77+
let g = g1.selectAll("g").data(d.children).enter().append("g");
7078

7179
// add class and click handler to all g's with children
7280
g.filter(function(d) {
@@ -89,12 +97,14 @@ d3.json("whitematter.json", function(data) {
8997

9098
function transition(d) {
9199

100+
focus = d;
101+
92102
generateSankey(d);
93103

94104

95105
if (transitioning || !d) return;
96106
transitioning = true;
97-
var g2 = display(d),
107+
let g2 = display(d),
98108
t1 = g1.transition().duration(650),
99109
t2 = g2.transition().duration(650);
100110
// Update the domain only after entering new elements.
@@ -215,7 +225,7 @@ d3.json("whitematter.json", function(data) {
215225

216226
function generateSankey(d){
217227

218-
var sankeyMap = {
228+
let sankeyMap = {
219229
nodes: [],
220230
links: []
221231
};
@@ -224,6 +234,7 @@ d3.json("whitematter.json", function(data) {
224234
if (!sankeyMap.nodes.some(n => n.name === d.children[i].data.name)) {
225235
sankeyMap.nodes.push({
226236
name: d.children[i].data.name,
237+
value: 1
227238
})
228239
}
229240
comb(d.children[i], d.children[i].depth, sankeyMap, i);
@@ -234,4 +245,3 @@ d3.json("whitematter.json", function(data) {
234245

235246
}
236247

237-
});

0 commit comments

Comments
 (0)