source: node_modules/d3-force/src/link.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 3.2 KB
Line 
1import constant from "./constant.js";
2import jiggle from "./jiggle.js";
3
4function index(d) {
5 return d.index;
6}
7
8function find(nodeById, nodeId) {
9 var node = nodeById.get(nodeId);
10 if (!node) throw new Error("node not found: " + nodeId);
11 return node;
12}
13
14export default function(links) {
15 var id = index,
16 strength = defaultStrength,
17 strengths,
18 distance = constant(30),
19 distances,
20 nodes,
21 count,
22 bias,
23 random,
24 iterations = 1;
25
26 if (links == null) links = [];
27
28 function defaultStrength(link) {
29 return 1 / Math.min(count[link.source.index], count[link.target.index]);
30 }
31
32 function force(alpha) {
33 for (var k = 0, n = links.length; k < iterations; ++k) {
34 for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) {
35 link = links[i], source = link.source, target = link.target;
36 x = target.x + target.vx - source.x - source.vx || jiggle(random);
37 y = target.y + target.vy - source.y - source.vy || jiggle(random);
38 l = Math.sqrt(x * x + y * y);
39 l = (l - distances[i]) / l * alpha * strengths[i];
40 x *= l, y *= l;
41 target.vx -= x * (b = bias[i]);
42 target.vy -= y * b;
43 source.vx += x * (b = 1 - b);
44 source.vy += y * b;
45 }
46 }
47 }
48
49 function initialize() {
50 if (!nodes) return;
51
52 var i,
53 n = nodes.length,
54 m = links.length,
55 nodeById = new Map(nodes.map((d, i) => [id(d, i, nodes), d])),
56 link;
57
58 for (i = 0, count = new Array(n); i < m; ++i) {
59 link = links[i], link.index = i;
60 if (typeof link.source !== "object") link.source = find(nodeById, link.source);
61 if (typeof link.target !== "object") link.target = find(nodeById, link.target);
62 count[link.source.index] = (count[link.source.index] || 0) + 1;
63 count[link.target.index] = (count[link.target.index] || 0) + 1;
64 }
65
66 for (i = 0, bias = new Array(m); i < m; ++i) {
67 link = links[i], bias[i] = count[link.source.index] / (count[link.source.index] + count[link.target.index]);
68 }
69
70 strengths = new Array(m), initializeStrength();
71 distances = new Array(m), initializeDistance();
72 }
73
74 function initializeStrength() {
75 if (!nodes) return;
76
77 for (var i = 0, n = links.length; i < n; ++i) {
78 strengths[i] = +strength(links[i], i, links);
79 }
80 }
81
82 function initializeDistance() {
83 if (!nodes) return;
84
85 for (var i = 0, n = links.length; i < n; ++i) {
86 distances[i] = +distance(links[i], i, links);
87 }
88 }
89
90 force.initialize = function(_nodes, _random) {
91 nodes = _nodes;
92 random = _random;
93 initialize();
94 };
95
96 force.links = function(_) {
97 return arguments.length ? (links = _, initialize(), force) : links;
98 };
99
100 force.id = function(_) {
101 return arguments.length ? (id = _, force) : id;
102 };
103
104 force.iterations = function(_) {
105 return arguments.length ? (iterations = +_, force) : iterations;
106 };
107
108 force.strength = function(_) {
109 return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initializeStrength(), force) : strength;
110 };
111
112 force.distance = function(_) {
113 return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), initializeDistance(), force) : distance;
114 };
115
116 return force;
117}
Note: See TracBrowser for help on using the repository browser.