-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchord_diagram.js
282 lines (213 loc) · 7.74 KB
/
chord_diagram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
let margin = {top: 200, right: 150, bottom: 200, left: 150},
width = 1000 - margin.left - margin.right,
height = 1000 - margin.top - margin.bottom,
outerRadius = Math.min(width, height) / 2 - 70,
innerRadius = outerRadius - 30;
let height2 = 460,width2 = 460;
let formatPercent = d3.format(".1%");
let chordChart = d3.select("#myDataViz")
.append("svg")
.attr("width",width)
.attr("height",height)
.append("g")
.attr("id","circle").attr("transform",
"translate("+width/2+","+height/2+")");
let testGraph = d3.select("#testDiv")
.append("svg")
.attr("id", "treeGraph")
.attr("width",width2)
.attr("height",height2)
/*testGraph.append("rect")
.attr("width",200)
.attr("height",100)
.attr("stroke","black")
.style("fill","transparent")
testGraph.append("text")
.attr("class","testText")
.attr("x",70)
.attr("y",30)*/
// chordChart.append("circle").attr("r",outerRadius);
//load dataset
const drawChord = async () =>{
const colorPalette = ["#48bf8e", "#075c62", "#a1def0", "#5e2a96",
"#e775cc", "#f3c5fa", "#9a76af", "#1c4585",
"#479abc", "#94ea5b", "#1d6d1f", "#cddb9b",
"#604020", "#d48f4d", "#f24219", "#8e1023",
"#8c956d", "#2cf52b", "#ff0087", "#e9d737"]
let colorScale = d3.scaleOrdinal().range(colorPalette)
const got = await d3.json("relationships1.json");
console.log(got);
let nodes = got.nodes;
let edges = got.edges;
let matrix = [];
let connections = {};
for (let i=0; i<nodes.length; i++) {
let row = [];
for (let j=0; j<nodes.length; j++) { row.push(0); }
matrix.push(row);
connections[i] = [i]; // everything connected to itself!
}
console.log(matrix);
edges.forEach( d => {
if (d["weight"] > 0) {
matrix[d.source_index][d.target_index] = d["weight"];
matrix[d.target_index][d.source_index] = d["weight"];
connections[d.source_index].push(d.target_index);
connections[d.target_index].push(d.source_index);
}
});
console.log(matrix);
console.log(connections);
let arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
let layout = d3.chord()
.padAngle(.04)
.sortSubgroups(d3.descending)
.sortChords(d3.ascending);
let ribbon = d3.ribbon()
.radius(innerRadius);
let chords = layout(matrix);
console.log(chords);
//Draw the diagram
chordChart.attr("transform",`translate(${width/2.0},${height/2.0})`);
let ringContainer = chordChart.append("g");
let rings = ringContainer.selectAll("g.segment")
.data(chords.groups)
.join("g")
.attr("class","segment");
rings.append("path")
.attr("fill", d => colorScale( nodes[ d.index ].name ))
.attr("stroke", d => colorScale( nodes[ d.index ].name ))
.attr("d", arc);
let ribbonContainer = chordChart.append("g");
let ribbons = ribbonContainer.selectAll("path.ribbon")
.data(chords)
.join("path")
.attr("class","ribbon")
.attr("fill-opacity", 0.5)
.attr("stroke", "none")
.attr("fill", d => colorScale( nodes[ d.source.index ].name ))
.attr("d", ribbon);
chords.groups.forEach( d => {
let transform = '';
let midpoint = (d.startAngle + d.endAngle) / 2;
let rotation = ( midpoint ) * ( 180 / Math.PI ) - 90;
transform = transform + ` rotate(${rotation})`;
transform = transform + ` translate(${outerRadius}, 0)`;
if (rotation > 90) {
transform = transform + ' rotate(180)';
// Notice text anchor issue that also first
d.anchor = "end";
}
d.trans = transform;
});
rings.append("text")
.attr("transform", d => d.trans)
.attr("class","text")
.text(d => nodes[ d.index ].name )
.attr("x",-1)
.attr("dy",".5em")
.attr("text-anchor", d => d.anchor);
/*chordChart.selectAll("text")
.data(chords.groups)
.append("text")
.attr("dx", 4)
.attr("dy", ".35em")
.append("textPath")
.attr("class", "label")
.attr("xlink:href", function(d) { return "#group" + d.index; })
.text(d => nodes[ d.index ].name )
//.text(function(d) { return "Arc " + d.index; })
.style("fill", function(d) { return d3.rgb(fill(d.index)).darker(2); });*/
function restoreHighlights() {
rings.attr("opacity", 1); // both text and ring
ribbons.attr("opacity", 1);
}
function lowlightAll() {
rings.attr("opacity", 0.2); // both text and ring
ribbons.attr("opacity", 0.2);
}
function highlightRings(index) {
let targetSegments = rings.filter( d => {
return connections[d.index].includes(index);
});
targetSegments.attr("opacity",1);
}
function highlightRibbons(index) {
let targetRibbons = ribbons.filter( d => {
return d.source.index === index || d.target.index === index;
});
targetRibbons.attr("opacity",1);
}
rings.on("mouseout", function(event,d){
restoreHighlights();
/* testGraph.select("rect").style("fill","transparent");
testGraph.select("text").text(" ")*/
//testGraph.style("visibility", "hidden");
d3.selectAll("#treeGraph > *").remove();
});
rings.on("mouseover", function(event, d) {
lowlightAll();
highlightRings(d.index);
highlightRibbons(d.index);
let targets = edges.filter(edge=>edge.source_index===d.index)
testGraph.style("visibility", "visible");
//Tree Graph starts here
let cluster = d3.cluster().size([height2,width2-100]);
let root = d3.hierarchy(d, d => {
return(edges.filter(edge=>edge.source_index===d.index));
});
cluster(root);
console.log(root);
testGraph.selectAll('path')
.data(root.descendants().slice(1) )
.enter()
.append('path').attr("d", function(d) {
return "M" + d.y + "," + d.x
+ "C" + (d.parent.y + 50) + "," + d.x
+ " " + (d.parent.y + 150) + "," + d.parent.x // 50 and 150 are coordinates of inflexion, play with it to change links shape
+ " " + d.parent.y + "," + d.parent.x;
})
.style("fill", 'none')
.attr("stroke", '#ccc')
testGraph.selectAll("g")
.data(root.descendants())
.enter()
.append("g")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")"
})
/*.append("circle")
.attr("name", d => {
console.log(d.data.target)
return(d.data.target) })
.attr("r", 7)
.style("fill", "#69b3a2")
.attr("stroke", "black")
.style("stroke-width", 2)*/
.append("text")
.text(d=>d.data.target)
/* testGraph.select("text")
.text(nodes[d.index].name)
.style("fill",colorScale(nodes[d.index].name))*/
});
let getGradID = chord => `linkGrad-${chord.source.index}-${chord.target.index}`;
var grads = d3.select("#chord").append("defs")
.selectAll("linearGradient")
.data(chords)
.join("linearGradient")
.attr("id", getGradID)
.attr("gradientUnits", "userSpaceOnUse") // use the coordinate system of whatever is being filled
.attr("x1", d => radius * Math.cos((d.source.endAngle-d.source.startAngle) / 2 + d.source.startAngle - Math.PI/2) )
.attr("y1", d => radius * Math.sin((d.source.endAngle-d.source.startAngle) / 2 + d.source.startAngle - Math.PI/2) )
.attr("x2", d => radius * Math.cos((d.target.endAngle-d.target.startAngle) / 2 + d.target.startAngle - Math.PI/2) )
.attr("y2", d => radius * Math.sin((d.target.endAngle-d.target.startAngle) / 2 + d.target.startAngle - Math.PI/2) )
grads.append("stop")
.attr("offset", "0%")
.attr("stop-color", d => colorScale(nodes[ d.source.index ].name) )
grads.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => colorScale(nodes[ d.target.index ].name) )
}
drawChord();