|
27
|
1 |
function Carte(parametres, articles, links) { |
|
|
2 |
|
|
|
3 |
var object = this; |
|
|
4 |
|
|
|
5 |
this.parametres = { |
|
|
6 |
// Module dimensions and caracteristics |
|
|
7 |
width: 1440, |
|
|
8 |
height: 800, |
|
|
9 |
top: 50, |
|
|
10 |
left: -240, |
|
|
11 |
name: null, |
|
|
12 |
boxID: "box", |
|
|
13 |
selectorID: "carto", |
|
|
14 |
canvasID: "canvas", |
|
|
15 |
info: "infos", |
|
|
16 |
|
|
|
17 |
couleurs: null, |
|
|
18 |
relations: null, |
|
|
19 |
|
|
|
20 |
// How to deal with the data |
|
|
21 |
isPositions: false, |
|
|
22 |
|
|
|
23 |
charge: -30, |
|
|
24 |
gravity: .13, |
|
|
25 |
theta: 3, |
|
|
26 |
|
|
|
27 |
// Node rendering |
|
|
28 |
nodeSizeInterval: [3, 15], |
|
|
29 |
log: false, |
|
|
30 |
exp: false, |
|
|
31 |
layerStyle: { |
|
|
32 |
opacity: 0.6 |
|
|
33 |
}, |
|
|
34 |
handle_len_rate: 2.2, |
|
|
35 |
maxDistance: 35, |
|
|
36 |
|
|
|
37 |
scaleInterval: [0.5, 10], |
|
|
38 |
|
|
|
39 |
infobulleHtml: function(node) { |
|
|
40 |
return node.titre + "<p>cluster: " + node.group +"</p>"; |
|
|
41 |
}, |
|
|
42 |
|
|
|
43 |
activerZoom: true, |
|
|
44 |
activerNodeZoom: true, |
|
|
45 |
activerInfobulle: true, |
|
|
46 |
zoomNode: false, |
|
|
47 |
logZoomNode: false, |
|
|
48 |
logZoomNodeParameter: 2 |
|
|
49 |
}; |
|
|
50 |
|
|
|
51 |
this.beginDate = new Date(); |
|
|
52 |
this.etendre(parametres); |
|
|
53 |
|
|
|
54 |
if (null==this.parametres.name) { |
|
|
55 |
this.parametres.name = this.parametres.selectorID; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
//création du contenant sur la page, si inexistant |
|
|
59 |
if (null == (document.getElementById(this.parametres.boxID))) { |
|
|
60 |
d3.select('.content').append('div') |
|
|
61 |
.attr("id", this.parametres.boxID) |
|
|
62 |
.classed("gallery", true) |
|
|
63 |
.style("position", "absolute") |
|
|
64 |
.style("border", "1 px dashed"); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
d3.select('#' + this.parametres.boxID) |
|
|
68 |
.style("width", this.parametres.width) |
|
|
69 |
.style("height", this.parametres.height) |
|
|
70 |
.style("position", "absolute") |
|
|
71 |
.style("top", this.parametres.top) |
|
|
72 |
.style("left", this.parametres.left) |
|
|
73 |
.style("overflow", "hidden") |
|
|
74 |
.style('z-index', 0); |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
if (null == (document.getElementById(this.parametres.selectorID))) { |
|
|
78 |
d3.select('.gallery').append('div') |
|
|
79 |
.attr("id", this.parametres.selectorID); |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
d3.select('#' + this.parametres.selectorID) |
|
|
83 |
.attr("width", this.parametres.width) |
|
|
84 |
.attr("height", this.parametres.height) |
|
|
85 |
.style("position", "absolute") |
|
|
86 |
.style('z-index', 2); |
|
|
87 |
|
|
|
88 |
// Create drag interaction with the module |
|
|
89 |
this.drag = d3.behavior.drag() |
|
|
90 |
.on("dragstart", function(d, i) {object.dragstart(d, i, object);}) |
|
|
91 |
.on("drag", object.dragmove) |
|
|
92 |
.on("dragend", function(d, i) {object.dragend(d, i, object);}); |
|
|
93 |
|
|
|
94 |
this.static = false; |
|
|
95 |
|
|
|
96 |
if (this.parametres.activerNodeZoom) { |
|
|
97 |
this.nodeMouseover = function(node) { |
|
|
98 |
var scale = object.getScale(object.rect); |
|
|
99 |
object.grow(node, scale); |
|
|
100 |
}; |
|
|
101 |
|
|
|
102 |
this.nodeMouseout = function(node) { |
|
|
103 |
var radius = object.radius(node.datum("scaledPoids")); |
|
|
104 |
object.shrink(radius, node); |
|
|
105 |
}; |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
if (this.parametres.activerInfobulle) { |
|
|
109 |
|
|
|
110 |
this.nodeClick = function(d) { |
|
|
111 |
object.highlight(d); |
|
|
112 |
}; |
|
|
113 |
|
|
|
114 |
this.infoClick = function(d) { |
|
|
115 |
object.blur(); |
|
|
116 |
}; |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
//création de l'infobulle, si inexistante |
|
|
120 |
if (null == (this.infoBulle = d3.select('#' + this.parametres.info)[0][0])) { |
|
|
121 |
this.infoBulle = d3.select('#' + this.parametres.selectorID).append('div') |
|
|
122 |
.attr("id", this.parametres.info) |
|
|
123 |
.style("top", this.parametres.height) |
|
|
124 |
.style("opacity", 0) |
|
|
125 |
.style("z-index", 1); |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
if (null!=this.parametres.relations) { |
|
|
129 |
this.parametres.relations.add( |
|
|
130 |
this.parametres.name + ".infobulle", this.infoBulle) |
|
|
131 |
.click(this.infoClick); |
|
|
132 |
} |
|
|
133 |
else { |
|
|
134 |
this.infoBulle.on("click", this.infoClick); |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
|
|
|
138 |
//création du canvas sur la page, si inexistant |
|
|
139 |
if (null == (this.canvas = document.getElementById(this.parametres.canvasID))) { |
|
|
140 |
d3.select('.gallery').insert('canvas', '#' + this.parametres.selectorID) |
|
|
141 |
.attr("id", this.parametres.canvasID) |
|
|
142 |
.attr("resize", false); |
|
|
143 |
this.canvas = document.getElementById(this.parametres.canvasID); |
|
|
144 |
} |
|
|
145 |
d3.select('#' + this.parametres.canvasID) |
|
|
146 |
.attr("width", d3.select('#' + this.parametres.selectorID).attr("width")) |
|
|
147 |
.attr("height", d3.select('#' + this.parametres.selectorID).attr("height")) |
|
|
148 |
.style("position", "absolute") |
|
|
149 |
.style("z-index", 1); |
|
|
150 |
|
|
|
151 |
this.mouse = {}; |
|
|
152 |
this.isDown = false; |
|
|
153 |
this.drag2 = false; |
|
|
154 |
|
|
|
155 |
this.svg = d3.select('#' + this.parametres.selectorID).append('svg') |
|
|
156 |
.attr("width", this.parametres.width) |
|
|
157 |
.attr("height", this.parametres.height) |
|
|
158 |
.on("mousedown", function() {object.mouseDown(event);}) |
|
|
159 |
.on("mousemove", function() {object.mouseMove(event);}) |
|
|
160 |
.on("mouseup", function() {object.mouseUp(event);}); |
|
|
161 |
|
|
|
162 |
paper.setup(this.canvas); |
|
|
163 |
paper.view.viewSize = new paper.Size(object.parametres.width, object.parametres.height); |
|
|
164 |
|
|
|
165 |
this.clusters = articles.clusters; |
|
|
166 |
|
|
|
167 |
//génération d'une palette de couleurs |
|
|
168 |
if (null == this.parametres.couleurs) { |
|
|
169 |
//this.parametres.couleurs = this.selectColors(this.clusters.length); |
|
|
170 |
this.parametres.couleurs = new Couleurs(this.clusters.length); |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
console.log("Environnement OK."); |
|
|
174 |
|
|
|
175 |
this.data = {}; |
|
|
176 |
this.zones = []; |
|
|
177 |
|
|
|
178 |
if (this.parametres.isPositions) { |
|
|
179 |
// Use directly data |
|
|
180 |
this.data.nodes = this.generateNodes(articles, this.parametres.isPositions); |
|
|
181 |
this.length = articles.nbItems; |
|
|
182 |
this.display(); |
|
|
183 |
|
|
|
184 |
} |
|
|
185 |
else { |
|
|
186 |
//initialisation du Force Directed Graph |
|
|
187 |
this.data.nodes = this.generateNodes(articles, this.parametres.isPositions); |
|
|
188 |
this.data.links = this.generateLinks(links, this.data.nodes); |
|
|
189 |
|
|
|
190 |
this.force = d3.layout.force() |
|
|
191 |
.charge(this.parametres.charge) //-20 |
|
|
192 |
.nodes(this.data.nodes) |
|
|
193 |
.links(this.data.links) |
|
|
194 |
.linkDistance(function(link) {return link.linkDistance;}) //20 |
|
|
195 |
.linkStrength(function(link) {return link.linkStrength;}) |
|
|
196 |
.size([this.parametres.width, this.parametres.height]) |
|
|
197 |
.gravity(this.parametres.gravity) //.1 |
|
|
198 |
.theta(this.parametres.theta) |
|
|
199 |
.start(); |
|
|
200 |
|
|
|
201 |
console.log("Graphe en cours de stabilisation . . ."); |
|
|
202 |
|
|
|
203 |
this.force.on("tick", function() { |
|
|
204 |
console.log("Stabilisation OK."); |
|
|
205 |
object.display(); |
|
|
206 |
}); |
|
|
207 |
|
|
|
208 |
} |
|
|
209 |
}; |
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
Carte.prototype = { |
|
|
214 |
constructor: Carte, |
|
|
215 |
|
|
|
216 |
//gestion des paramètres |
|
|
217 |
etendre: function(parametres) { |
|
|
218 |
if (null==parametres || "object"!=typeof(parametres)) { |
|
|
219 |
return; |
|
|
220 |
} |
|
|
221 |
|
|
|
222 |
for (var cle in parametres) { |
|
|
223 |
this.parametres[cle] = parametres[cle]; |
|
|
224 |
} |
|
|
225 |
}, |
|
|
226 |
|
|
|
227 |
display: function () { |
|
|
228 |
|
|
|
229 |
this.showNodes(); |
|
|
230 |
if (this.parametres.activerZoom) { |
|
|
231 |
this.activateZoom(); |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
paper.setup(this.canvas); |
|
|
235 |
paper.view.viewSize = new paper.Size( |
|
|
236 |
this.parametres.width, this.parametres.height); |
|
|
237 |
this.createBackground(); |
|
|
238 |
}, |
|
|
239 |
|
|
|
240 |
activateZoom: function() { |
|
|
241 |
this.rect = this.svg.selectAll("rect") |
|
|
242 |
.data([{ |
|
|
243 |
x: this.parametres.width*(1 - this.parametres.scaleInterval[0])/(this.parametres.scaleInterval[1] - this.parametres.scaleInterval[0]), |
|
|
244 |
y: 0 |
|
|
245 |
}]) |
|
|
246 |
.enter().append("rect") |
|
|
247 |
.attr("x", function(d) { return d.x; }) |
|
|
248 |
.attr("y", function(d) { return d.y; }) |
|
|
249 |
.attr("width", 15) |
|
|
250 |
.attr("height", 15) |
|
|
251 |
.style("stroke", "black") |
|
|
252 |
.style("fill", "#00f") |
|
|
253 |
.style("z-index", 2) |
|
|
254 |
.call(this.drag); |
|
|
255 |
|
|
|
256 |
if (null!=this.parametres.relations) { |
|
|
257 |
this.parametres.relations.add( |
|
|
258 |
this.parametres.name + ".zoomButton", this.rect); |
|
|
259 |
} |
|
|
260 |
}, |
|
|
261 |
|
|
|
262 |
//génération d'une palette de couleurs |
|
|
263 |
selectColors: function(nbClusters) { |
|
|
264 |
var colors = colorbrewer; |
|
|
265 |
var sets = ["BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdYlBu", "RdYlGn"]; |
|
|
266 |
var selectorID, finalColors = []; |
|
|
267 |
|
|
|
268 |
while (finalColors.length < nbClusters) { |
|
|
269 |
selectorID = Math.floor(Math.random()*7); |
|
|
270 |
selectorID2 = Math.floor(Math.random()*11); |
|
|
271 |
color = new paper.RgbColor(colors[sets[selectorID]][11][selectorID2]); |
|
|
272 |
|
|
|
273 |
var available = true; |
|
|
274 |
for (var i = 0; i < finalColors.length; i++) { |
|
|
275 |
if (Math.floor(finalColors[i].hue) == Math.floor(color.hue)) { |
|
|
276 |
available = false; |
|
|
277 |
} |
|
|
278 |
} |
|
|
279 |
|
|
|
280 |
if (color.brightness < 0.9 && available) { |
|
|
281 |
finalColors.push(color.toCssString()); |
|
|
282 |
} |
|
|
283 |
} |
|
|
284 |
|
|
|
285 |
return finalColors; |
|
|
286 |
}, |
|
|
287 |
|
|
|
288 |
//génération des noeuds √†partir du json |
|
|
289 |
generateNodes: function(items, isPos) { |
|
|
290 |
|
|
|
291 |
var nodes = []; |
|
|
292 |
var poidsMin = 100000, poidsMax = 0; |
|
|
293 |
|
|
|
294 |
// on crée les noeuds |
|
|
295 |
for (var i = 0 ; i<items.nbItems; ++i) { |
|
|
296 |
nodes.push({index: i, titre: "Titre numero " + i, weigth: 1}); |
|
|
297 |
} |
|
|
298 |
|
|
|
299 |
// on donne àchaque noeud son poids et couleurs |
|
|
300 |
// on traite identiquement core et agregated |
|
|
301 |
// pour les clusters |
|
|
302 |
var nbClusters = items.clusters.length; |
|
|
303 |
for (var i =0; i< nbClusters; ++i) { |
|
|
304 |
var cluster = items.clusters[i], |
|
|
305 |
clusterId = cluster.id, |
|
|
306 |
groups = []; |
|
|
307 |
if (cluster.items) { |
|
|
308 |
groups.push("items"); |
|
|
309 |
} |
|
|
310 |
else { |
|
|
311 |
groups.push("core"); |
|
|
312 |
groups.push("agregated"); |
|
|
313 |
} |
|
|
314 |
for (var a in groups) { |
|
|
315 |
var group = cluster[groups[a]]; |
|
|
316 |
for (var j =0; j<group.length; ++j) { |
|
|
317 |
var item = group[j], poids = parseInt(item.value); |
|
|
318 |
nodes[item.key]["poids"] = poids; |
|
|
319 |
nodes[item.key]["scaledPoids"] = poids; |
|
|
320 |
nodes[item.key]["group"] = clusterId; |
|
|
321 |
nodes[item.key]["key"] = item.key; |
|
|
322 |
if (isPos) { |
|
|
323 |
nodes[item.key]["x"] = parseFloat(item.x); |
|
|
324 |
nodes[item.key]["y"] = parseFloat(item.y); |
|
|
325 |
} |
|
|
326 |
else { |
|
|
327 |
nodes[item.key]["x"] = this.parametres.width/2*(1 + Math.cos(i*Math.Pi/nbClusters)); |
|
|
328 |
nodes[item.key]["y"] = this.parametres.height/2*(1 + Math.sin(i*Math.Pi/nbClusters)); |
|
|
329 |
} |
|
|
330 |
if (null != this.parametres.relations) { |
|
|
331 |
this.parametres.relations.add(this.parametres.selectorID + ".node." + item.key, nodes[item.key]); |
|
|
332 |
} |
|
|
333 |
if (poids>poidsMax) { |
|
|
334 |
poidsMax = poids; |
|
|
335 |
} |
|
|
336 |
else if (poids<poidsMin) { |
|
|
337 |
poidsMin = poids; |
|
|
338 |
} |
|
|
339 |
} |
|
|
340 |
} |
|
|
341 |
} |
|
|
342 |
|
|
|
343 |
this.poidsMin = poidsMin; |
|
|
344 |
this.poidsMax = poidsMax; |
|
|
345 |
|
|
|
346 |
// pour les unclustered |
|
|
347 |
var unclusteredId = items.clusters.length; |
|
|
348 |
var unclusteredItems = (items.unclustered.items)? |
|
|
349 |
items.unclustered.items: items.unclustered; |
|
|
350 |
for (var i =0; i< unclusteredItems.length; ++i) { |
|
|
351 |
var item = unclusteredItems[i]; |
|
|
352 |
nodes[item.key]["poids"] = poidsMin; |
|
|
353 |
nodes[item.key]["scaledPoids"] = poidsMin; |
|
|
354 |
nodes[item.key]["group"] = unclusteredId; |
|
|
355 |
nodes[item.key]["key"] = item.key; |
|
|
356 |
if (isPos) { |
|
|
357 |
nodes[item.key]["x"] = parseFloat(item.x); |
|
|
358 |
nodes[item.key]["y"] = parseFloat(item.y); |
|
|
359 |
} |
|
|
360 |
else { |
|
|
361 |
nodes[item.key]["x"] = this.parametres.width/2; |
|
|
362 |
nodes[item.key]["y"] = this.parametres.height/2; |
|
|
363 |
} |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
console.log("Data articles extraites."); |
|
|
367 |
return nodes; |
|
|
368 |
}, |
|
|
369 |
|
|
|
370 |
//génération des liens à partir du json |
|
|
371 |
generateLinks: function(liens, nodes) { |
|
|
372 |
//var linkMax = 0, linkMin = 500; |
|
|
373 |
var links = []; |
|
|
374 |
|
|
|
375 |
for (var i = 0; i< liens.images.length; ++i) { |
|
|
376 |
var item = liens.images[i]; |
|
|
377 |
for (var j = 0; j < 20 && j < item.v.length; ++j) { |
|
|
378 |
var voisin = item.v[j]; |
|
|
379 |
|
|
|
380 |
var linkDistance = 200/Math.max(voisin.d, 4), linkStrength; |
|
|
381 |
|
|
|
382 |
if (nodes[item.id]["group"] == nodes[voisin.id]["group"]) { |
|
|
383 |
linkStrength = 1; |
|
|
384 |
linkDistance *= 0.5; |
|
|
385 |
} |
|
|
386 |
else { |
|
|
387 |
linkStrength = 0.5; |
|
|
388 |
} |
|
|
389 |
|
|
|
390 |
links.push({source: item.id, target: voisin.id, value: voisin.d, linkDistance: linkDistance, linkStrength: linkStrength}); |
|
|
391 |
} |
|
|
392 |
} |
|
|
393 |
|
|
|
394 |
console.log("Data liens extraites."); |
|
|
395 |
return links; |
|
|
396 |
}, |
|
|
397 |
|
|
|
398 |
//calcul du rayon |
|
|
399 |
radius: function(poids) { |
|
|
400 |
|
|
|
401 |
var diff = this.parametres.nodeSizeInterval[1] - this.parametres.nodeSizeInterval[0]; |
|
|
402 |
scale = this.getScale(this.rect); |
|
|
403 |
|
|
|
404 |
if (this.parametres.log) { |
|
|
405 |
return this.parametres.nodeSizeInterval[0] + Math.log(poids/(this.poidsMin*scale))*diff/Math.log(this.poidsMax/this.poidsMin); |
|
|
406 |
|
|
|
407 |
} |
|
|
408 |
else if (this.parametres.exp) { |
|
|
409 |
return this.parametres.nodeSizeInterval[0]*Math.exp(Math.log(this.parametres.nodeSizeInterval[1]/this.parametres.nodeSizeInterval[0])*(poids-(this.poidsMin*scale))/(scale*(this.poidsMax - this.poidsMin))); |
|
|
410 |
} |
|
|
411 |
else { |
|
|
412 |
return scale*((this.parametres.nodeSizeInterval[0] + diff*(poids - scale*this.poidsMin)/(scale*(this.poidsMax - this.poidsMin)))); |
|
|
413 |
} |
|
|
414 |
}, |
|
|
415 |
|
|
|
416 |
//affichages des noeuds |
|
|
417 |
showNodes: function() { |
|
|
418 |
var object = this; |
|
|
419 |
node = this.svg.selectAll("circle.node") |
|
|
420 |
.data(this.data.nodes) |
|
|
421 |
.enter().append("circle") |
|
|
422 |
.attr("class", "node") |
|
|
423 |
.attr("cx", function(d) { return d.x; }) |
|
|
424 |
.attr("cy", function(d) { return d.y; }) |
|
|
425 |
.attr("z-index", 0) |
|
|
426 |
.attr("r", function(d) {return object.radius(d.poids);}) |
|
|
427 |
.style("fill", function(d) { |
|
|
428 |
return object.parametres.couleurs.get(d.group); |
|
|
429 |
}); |
|
|
430 |
/*.on("mouseover", this.nodeMouseover) |
|
|
431 |
.on("mouseout", this.nodeMouseout)*/; |
|
|
432 |
|
|
|
433 |
// Register all nodes |
|
|
434 |
if (null!=object.parametres.relations) { |
|
|
435 |
node.each(function(datum, index) { |
|
|
436 |
datum["registrationName"] = object.parametres.name + ".node." + index; |
|
|
437 |
object.parametres.relations.add( |
|
|
438 |
datum.registrationName, d3.select(this)) |
|
|
439 |
.mouseover(object.nodeMouseover) |
|
|
440 |
.mouseout(object.nodeMouseout); |
|
|
441 |
}); |
|
|
442 |
} |
|
|
443 |
|
|
|
444 |
node.append("p") |
|
|
445 |
.text(function(d) { return d.titre; }); |
|
|
446 |
node.on('click', this.nodeClick); |
|
|
447 |
console.log("Noeuds OK."); |
|
|
448 |
}, |
|
|
449 |
|
|
|
450 |
//génération du fond de la carte |
|
|
451 |
createBackground: function() { |
|
|
452 |
var circlePaths = []; |
|
|
453 |
scale = this.getScale(this.rect); |
|
|
454 |
object = this; |
|
|
455 |
|
|
|
456 |
this.svg.selectAll('circle.node').each(function(d) { |
|
|
457 |
var node = d3.select(this); |
|
|
458 |
var circle = new paper.Path.Circle( |
|
|
459 |
new paper.Point(d.x, d.y), object.radius(d.scaledPoids)*2); |
|
|
460 |
circle.fillColor = node.style("fill"); |
|
|
461 |
circlePaths.push(circle); |
|
|
462 |
}); |
|
|
463 |
|
|
|
464 |
console.log("Fond des noeuds OK."); |
|
|
465 |
this.generateConnections(circlePaths); |
|
|
466 |
paper.view.draw(); |
|
|
467 |
|
|
|
468 |
/*var endDate = new Date(); |
|
|
469 |
var timer = endDate.getTime() - beginDate.getTime(); |
|
|
470 |
console.log(timer);*/ |
|
|
471 |
}, |
|
|
472 |
|
|
|
473 |
//generate background connections between nodes of a same cluster |
|
|
474 |
generateConnections: function(paths) { |
|
|
475 |
for (var c=0; c<this.clusters.length; ++c) { |
|
|
476 |
var cluster = this.clusters[c]; |
|
|
477 |
|
|
|
478 |
// définir les données suivant le type passé |
|
|
479 |
var group = cluster.items; |
|
|
480 |
if (undefined==group) { |
|
|
481 |
group = cluster.core.concat(cluster.agregated); |
|
|
482 |
} |
|
|
483 |
|
|
|
484 |
var layer = new paper.Layer(); |
|
|
485 |
|
|
|
486 |
// Donner un design au fond |
|
|
487 |
for (var attr in this.parametres.layerStyle) { |
|
|
488 |
if (layer[attr]) { |
|
|
489 |
layer[attr] = this.parametres.layerStyle[attr]; |
|
|
490 |
} |
|
|
491 |
else { |
|
|
492 |
layer.style[attr] = this.parametres.layerStyle[attr]; |
|
|
493 |
} |
|
|
494 |
} |
|
|
495 |
|
|
|
496 |
var maxDistance = this.scale(this.rect[0][0].getAttribute("x"))*this.parametres.maxDistance, |
|
|
497 |
handle = this.parametres.handle_len_rate; |
|
|
498 |
for (var i = 1; i<group.length; ++i) { |
|
|
499 |
var circle = paths[group[i].key]; |
|
|
500 |
if ((circle.position.x + maxDistance > 0) && (circle.position.x < this.parametres.width + maxDistance) && (circle.position.y + maxDistance > 0) && (circle.position.y < this.parametres.height + maxDistance)) { |
|
|
501 |
layer.appendTop(circle); |
|
|
502 |
for (var j = 0; j<i; ++j) { |
|
|
503 |
var path = this.connect(circle, paths[group[j].key], 0.5, handle, maxDistance); |
|
|
504 |
if (null!=path) { |
|
|
505 |
layer.appendTop(path); |
|
|
506 |
} |
|
|
507 |
} |
|
|
508 |
} |
|
|
509 |
} |
|
|
510 |
|
|
|
511 |
this.zones[c] = layer; |
|
|
512 |
if (null != this.parametres.relations) { |
|
|
513 |
this.parametres.relations.add(this.parametres.selectorID + ".layer." + c, layer); |
|
|
514 |
} |
|
|
515 |
|
|
|
516 |
} |
|
|
517 |
paper.project.layers[0].remove(); |
|
|
518 |
console.log("Connexions OK."); |
|
|
519 |
}, |
|
|
520 |
|
|
|
521 |
/* |
|
|
522 |
* Create a connection between two balls |
|
|
523 |
*/ |
|
|
524 |
connect: function(ball1, ball2, v, handle_len_rate, maxDistance) { |
|
|
525 |
var center1 = ball1.position; |
|
|
526 |
var center2 = ball2.position; |
|
|
527 |
var radius1 = ball1.bounds.width / 2; |
|
|
528 |
var radius2 = ball2.bounds.width / 2; |
|
|
529 |
var pi2 = Math.PI / 2; |
|
|
530 |
var d = center1.getDistance(center2); |
|
|
531 |
var u1, u2; |
|
|
532 |
|
|
|
533 |
if (radius1 == 0 || radius2 == 0) { |
|
|
534 |
return null; |
|
|
535 |
} |
|
|
536 |
|
|
|
537 |
if (d > maxDistance || d <= Math.abs(radius1 - radius2)) { |
|
|
538 |
return; |
|
|
539 |
} else if (d < radius1 + radius2) { // case circles are overlapping |
|
|
540 |
return null; |
|
|
541 |
} else { |
|
|
542 |
u1 = 0; |
|
|
543 |
u2 = 0; |
|
|
544 |
} |
|
|
545 |
|
|
|
546 |
var center3 = center2.clone(); |
|
|
547 |
center3.x = center2.x - center1.x; |
|
|
548 |
center3.y = center2.y - center1.y; |
|
|
549 |
|
|
|
550 |
var angle1 = center3.getAngleInRadians(); |
|
|
551 |
var angle2 = Math.acos((radius1 - radius2) / d); |
|
|
552 |
var angle1a = angle1 + u1 + (angle2 - u1) * v; |
|
|
553 |
var angle1b = angle1 - u1 - (angle2 - u1) * v; |
|
|
554 |
var angle2a = angle1 + Math.PI - u2 - (Math.PI - u2 - angle2) * v; |
|
|
555 |
var angle2b = angle1 - Math.PI + u2 + (Math.PI - u2 - angle2) * v; |
|
|
556 |
var p1a = center1.add(this.getVector(angle1a, radius1)); |
|
|
557 |
var p1b = center1.add(this.getVector(angle1b, radius1)); |
|
|
558 |
var p2a = center2.add(this.getVector(angle2a, radius2)); |
|
|
559 |
var p2b = center2.add(this.getVector(angle2b, radius2)); |
|
|
560 |
|
|
|
561 |
// define handle length by the distance between |
|
|
562 |
// both ends of the curve to draw |
|
|
563 |
var totalRadius = (radius1 + radius2); |
|
|
564 |
var p3a = p2a.clone(); |
|
|
565 |
p3a.x = p1a.x - p2a.x; |
|
|
566 |
p3a.y = p1a.y - p2a.y; |
|
|
567 |
var d2 = Math.min(v * handle_len_rate, p3a.length / totalRadius); |
|
|
568 |
|
|
|
569 |
// case circles are overlapping: |
|
|
570 |
d2 *= Math.min(1, d * 2 / (radius1 + radius2)); |
|
|
571 |
|
|
|
572 |
radius1 *= d2; |
|
|
573 |
radius2 *= d2; |
|
|
574 |
|
|
|
575 |
var path = new paper.Path([p1a, p2a, p2b, p1b]); |
|
|
576 |
path.style = ball1.style; |
|
|
577 |
path.closed = true; |
|
|
578 |
var segments = path.segments; |
|
|
579 |
segments[0].handleOut = this.getVector(angle1a - pi2, radius1); |
|
|
580 |
segments[1].handleIn = this.getVector(angle2a + pi2, radius2); |
|
|
581 |
segments[2].handleOut = this.getVector(angle2b - pi2, radius2); |
|
|
582 |
segments[3].handleIn = this.getVector(angle1b + pi2, radius1); |
|
|
583 |
return path; |
|
|
584 |
}, |
|
|
585 |
|
|
|
586 |
// ------------------------------------------------ |
|
|
587 |
getVector: function(radians, length) { |
|
|
588 |
var angle = radians * 180 / Math.PI; |
|
|
589 |
return new paper.Point({"angle": angle, |
|
|
590 |
"length": length |
|
|
591 |
}); |
|
|
592 |
}, |
|
|
593 |
|
|
|
594 |
//apparition de l'infoBulle |
|
|
595 |
highlight: function(node) { |
|
|
596 |
scale = this.parametres.activerZoom ? this.scale(this.rect[0][0].getAttribute("x")) : 1; |
|
|
597 |
this.infoBulle.html(this.parametres.infobulleHtml(node)); |
|
|
598 |
if (this.infoBulle.classed("selected")) { |
|
|
599 |
this.infoBulle.transition() |
|
|
600 |
.duration(500) |
|
|
601 |
.style("left", node.x + "px") |
|
|
602 |
.style("top", node.y + "px"); |
|
|
603 |
} |
|
|
604 |
else { |
|
|
605 |
this.infoBulle.classed("selected", true) |
|
|
606 |
.style("left", node.x + "px") |
|
|
607 |
.style("top", node.y + "px") |
|
|
608 |
.transition() |
|
|
609 |
.duration(300) |
|
|
610 |
.style("opacity", 0.5); |
|
|
611 |
} |
|
|
612 |
}, |
|
|
613 |
|
|
|
614 |
//disparition de l'infoBulle |
|
|
615 |
blur: function() { |
|
|
616 |
this.infoBulle.classed("selected", false) |
|
|
617 |
.transition() |
|
|
618 |
.duration(400) |
|
|
619 |
.style("opacity", 0); |
|
|
620 |
this.infoBulle.style("top", this.parametres.height); |
|
|
621 |
}, |
|
|
622 |
|
|
|
623 |
//zoom sur un point |
|
|
624 |
grow: function(node, scale) { |
|
|
625 |
if (!this.static) { |
|
|
626 |
node.attr("z-index", 1) |
|
|
627 |
.transition() |
|
|
628 |
.duration(200) |
|
|
629 |
.attr("r", this.parametres.nodeSizeInterval[1]*scale); |
|
|
630 |
} |
|
|
631 |
}, |
|
|
632 |
|
|
|
633 |
//dézoom sur un point |
|
|
634 |
shrink: function(radius, node) { |
|
|
635 |
if (!this.static) { |
|
|
636 |
node.transition() |
|
|
637 |
.duration(70) |
|
|
638 |
.attr("z-index", 0) |
|
|
639 |
.attr("r", radius); |
|
|
640 |
} |
|
|
641 |
|
|
|
642 |
}, |
|
|
643 |
|
|
|
644 |
//réaffichage du fond sur recadrage du navigateur |
|
|
645 |
onResize: function(event) { |
|
|
646 |
paper.view.draw(); |
|
|
647 |
d3.select('#' + this.parametres.canvasID) |
|
|
648 |
.attr("width", d3.select('#' + this.parametres.selectorID).attr("width")) |
|
|
649 |
.attr("height", d3.select('#' + this.parametres.selectorID).attr("height")); |
|
|
650 |
}, |
|
|
651 |
|
|
|
652 |
dragstart: function(d,i, object) { |
|
|
653 |
object.rect.xi = object.scale(d3.event.x); |
|
|
654 |
object.static = true; |
|
|
655 |
}, |
|
|
656 |
|
|
|
657 |
dragmove: function(d, i) { |
|
|
658 |
d.x += d3.event.dx; |
|
|
659 |
d3.select(this).attr("x", d.x); |
|
|
660 |
}, |
|
|
661 |
|
|
|
662 |
dragend: function(d, i, object) { |
|
|
663 |
|
|
|
664 |
var ratio = object.scale(d3.event.x)/object.rect.xi; |
|
|
665 |
object.infoBulle.classed("selected", false) |
|
|
666 |
.style("opacity", 0); |
|
|
667 |
|
|
|
668 |
object.zoom(ratio); |
|
|
669 |
object.resetCanvas(); |
|
|
670 |
|
|
|
671 |
object.zoomTransition(ratio); |
|
|
672 |
|
|
|
673 |
object.static = false; |
|
|
674 |
var timer = new Date().getTime() - object.beginDate.getTime(); |
|
|
675 |
console.log(timer); |
|
|
676 |
}, |
|
|
677 |
|
|
|
678 |
resetCanvas: function() { |
|
|
679 |
paper.view.remove(); |
|
|
680 |
this.hideView(); |
|
|
681 |
paper.setup(this.canvas); |
|
|
682 |
paper.view.viewSize = new paper.Size(this.parametres.width, this.parametres.height); |
|
|
683 |
}, |
|
|
684 |
|
|
|
685 |
hideView: function() { |
|
|
686 |
var ctx = this.canvas.getContext('2d'); |
|
|
687 |
ctx.fillStyle = 'rgb(0,0,0)'; |
|
|
688 |
ctx.fillRect(this.canvas.left, this.canvas.top, this.canvas.width, this.canvas.height); |
|
|
689 |
}, |
|
|
690 |
|
|
|
691 |
scale: function(x) { |
|
|
692 |
return (this.parametres.scaleInterval[0] + (this.parametres.scaleInterval[1] - this.parametres.scaleInterval[0])*(x/this.parametres.width)); |
|
|
693 |
}, |
|
|
694 |
|
|
|
695 |
getScale: function(rect) { |
|
|
696 |
if (rect == undefined || !this.parametres.activerZoom) { |
|
|
697 |
return 1; |
|
|
698 |
} |
|
|
699 |
else { |
|
|
700 |
if (this.parametres.zoomNode) { |
|
|
701 |
return this.scale(rect[0][0].getAttribute("x")); |
|
|
702 |
} |
|
|
703 |
else if (this.parametres.logZoomNode) { |
|
|
704 |
return Math.log(1 + this.scale(rect[0][0].getAttribute("x"))*this.parametres.logZoomNodeParameter)/(Math.log(1 + this.parametres.logZoomNodeParameter)); |
|
|
705 |
} |
|
|
706 |
else { |
|
|
707 |
return 1; |
|
|
708 |
} |
|
|
709 |
} |
|
|
710 |
}, |
|
|
711 |
|
|
|
712 |
zoom: function(ratio) { |
|
|
713 |
var object = this; |
|
|
714 |
var scale = this.getScale(this.rect); |
|
|
715 |
|
|
|
716 |
this.svg.selectAll("circle.node") |
|
|
717 |
.each(function(d) { |
|
|
718 |
d.x = object.parametres.width/2 |
|
|
719 |
- ratio*(object.parametres.width/2- d.x); |
|
|
720 |
|
|
|
721 |
d.y = object.parametres.height/2 |
|
|
722 |
- ratio*(object.parametres.height/2 - d.y); |
|
|
723 |
|
|
|
724 |
d.scaledPoids = d.poids*scale; |
|
|
725 |
}); |
|
|
726 |
}, |
|
|
727 |
|
|
|
728 |
zoomTransition: function(ratio) { |
|
|
729 |
var object = this; |
|
|
730 |
var c = 0; |
|
|
731 |
this.svg.selectAll("circle.node") |
|
|
732 |
.transition() |
|
|
733 |
.duration(Math.max(400*ratio, 400/ratio)) |
|
|
734 |
.attr("cx", function(d) { |
|
|
735 |
return d.x; |
|
|
736 |
}) |
|
|
737 |
.attr("cy", function(d) { |
|
|
738 |
return d.y; |
|
|
739 |
}) |
|
|
740 |
.attr("r", function(d) { return object.radius(d.scaledPoids);}) |
|
|
741 |
.each("end", function() { |
|
|
742 |
c++; |
|
|
743 |
if (c == object.length) { |
|
|
744 |
object.static = false; |
|
|
745 |
object.createBackground(); |
|
|
746 |
} |
|
|
747 |
}); |
|
|
748 |
}, |
|
|
749 |
|
|
|
750 |
mouseDown: function(e) { |
|
|
751 |
this.isDown = true; |
|
|
752 |
this.mouse.x = e.x; |
|
|
753 |
this.mouse.y = e.y; |
|
|
754 |
this.center = {}; |
|
|
755 |
/*this.center.x = paper.view.center.x; |
|
|
756 |
this.center.y = paper.view.center.y;*/ |
|
|
757 |
var object = this; |
|
|
758 |
setTimeout(function() { |
|
|
759 |
if (object.isDown) { |
|
|
760 |
object.blur(); |
|
|
761 |
//d3.select(object.canvas).style("visibility", "hidden"); |
|
|
762 |
object.resetCanvas(); |
|
|
763 |
object.static = true; |
|
|
764 |
object.drag2 = true; |
|
|
765 |
} |
|
|
766 |
}, 100); |
|
|
767 |
}, |
|
|
768 |
|
|
|
769 |
mouseMove: function(e) { |
|
|
770 |
if (this.drag2) { |
|
|
771 |
diff = {}; |
|
|
772 |
diff.x = e.x - this.mouse.x; |
|
|
773 |
diff.y = e.y - this.mouse.y; |
|
|
774 |
/*this.center.x -= diff.x; |
|
|
775 |
this.center.y -= diff.y;*/ |
|
|
776 |
this.svg.selectAll('circle.node') |
|
|
777 |
.attr("cx", function(d) { |
|
|
778 |
d.x += diff.x; |
|
|
779 |
return d.x; |
|
|
780 |
}) |
|
|
781 |
.attr("cy", function(d) { |
|
|
782 |
d.y += diff.y; |
|
|
783 |
return d.y; |
|
|
784 |
}); |
|
|
785 |
this.mouse.x = e.x; this.mouse.y = e.y; |
|
|
786 |
} |
|
|
787 |
}, |
|
|
788 |
|
|
|
789 |
mouseUp: function(e) { |
|
|
790 |
if (this.drag2) { |
|
|
791 |
//paper.view.center = new paper.Point(this.center.x, this.center.y); |
|
|
792 |
//d3.select(object.canvas).style("visibility", "visible"); |
|
|
793 |
this.createBackground(); |
|
|
794 |
this.static = false; |
|
|
795 |
this.drag2 = false; |
|
|
796 |
} |
|
|
797 |
this.isDown = false; |
|
|
798 |
this.mouse = {}; |
|
|
799 |
} |
|
|
800 |
|
|
|
801 |
}; |