-
Notifications
You must be signed in to change notification settings - Fork 2
/
usernetworkvis.js.es6
194 lines (168 loc) · 4.84 KB
/
usernetworkvis.js.es6
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
import loadScript from "discourse/lib/load-script";
import DiscourseURL from "discourse/lib/url";
import { notEmpty } from "@ember/object/computed";
import { observes } from 'discourse-common/utils/decorators';
export default Ember.Component.extend({
classNames: "user-network-vis",
results: Ember.computed.alias("model.results"),
hasItems: notEmpty("results"),
ensureD3() {
return loadScript("/plugins/discourse-user-network-vis/d3/d3.min.js");
},
didInsertElement() {
if (!this.site.mobileView) {
this.waitForData()
}
},
@observes("hasItems")
waitForData() {
if(!this.hasItems) {
return;
} else {
this.setup();
}
},
setup() {
var _this = this;
this.ensureD3().then(() => {
function fade(opacity) {
return function (d) {
// check all other nodes to see if they're connected
// to this one. if so, keep the opacity at 1, otherwise
// fade
node.style("opacity", function (o) {
return isConnected(d.currentTarget.__data__, o) ? 1 : opacity;
});
// also style link accordingly
link.style("opacity", function (o) {
return o.source.id === d.currentTarget.__data__.id || o.target.id === d.currentTarget.__data__.id ? 1 : opacity;
});
};
}
var width = 1120,
height = _this.siteSettings.user_network_vis_canvas_height;
var svg = d3
.select(".user-network-vis")
.append("svg")
.attr("width", width)
.attr("height", height);
var color = d3.scaleOrdinal(
_this.siteSettings.user_network_vis_colors.split("|")
);
var simulation = d3
.forceSimulation()
.force(
"link",
d3.forceLink().id((d) => {
return d.id;
})
)
.force(
"charge",
d3
.forceManyBody()
.strength(
-_this.siteSettings.user_network_vis_node_charge_strength
)
)
.force("center", d3.forceCenter(width / 2, height / 2));
var link = svg
.append("g")
.attr("class", "links")
.selectAll("line")
.data(_this.results.user_network_stats.links)
.enter()
.append("line")
.attr("stroke-width", (d) => {
return Math.cbrt(Math.round(d.value) + 1);
});
var node = svg
.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(_this.results.user_network_stats.nodes)
.enter()
.append("g")
.on("mouseover", fade(.1))
.on("mouseout", fade(1));
var circles = node
.append("circle")
.attr("r", _this.siteSettings.user_network_vis_node_radius)
.attr("fill", (d) => {
return color(d.group + 1);
})
.call(
d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.on("click", (event, d) => {
if (d.id) {
DiscourseURL.routeTo(`/u/${d.id}/summary`);
}
})
var labels = node
.append("text")
.text((d) => {
return d.id;
})
.attr("x", _this.siteSettings.user_network_vis_node_radius + 1)
.attr("y", _this.siteSettings.user_network_vis_node_radius / 2 + 1);
node.append("title").text((d) => {
return d.id;
});
simulation
.nodes(_this.results.user_network_stats.nodes)
.on("tick", ticked);
simulation.force("link").links(_this.results.user_network_stats.links);
var linkedByIndex = {};
simulation
.force("link")
.links()
.forEach(function (d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
function isConnected(a, b) {
return (
linkedByIndex[a.index + "," + b.index] ||
linkedByIndex[b.index + "," + a.index] ||
a.index == b.index
);
}
function ticked() {
link
.attr("x1", (d) => {
return d.source.x;
})
.attr("y1", (d) => {
return d.source.y;
})
.attr("x2", (d) => {
return d.target.x;
})
.attr("y2", (d) => {
return d.target.y;
});
node.attr("transform", (d) => {
return "translate(" + d.x + "," + d.y + ")";
});
}
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
});
},
});