UNPKG

48 kBTypeScriptView Raw
1// Type definitions for D3JS d3-force module 3.0
2// Project: https://github.com/d3/d3-force/, https://d3js.org/d3-force
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// denisname <https://github.com/denisname>
7// Nathan Bierema <https://github.com/Methuselah96>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9
10// Last module patch version validated against: 3.0.0
11
12// -----------------------------------------------------------------------
13// Force Simulation
14// -----------------------------------------------------------------------
15
16/**
17 * The base data structure for the datum of a Simulation Node.
18 * The optional properties contained in this data structure are internally assigned
19 * by the Simulation upon (re-)initialization.
20 *
21 * When defining a data type to use for node data, it should be an extension of this interface
22 * and respect the already "earmarked" properties used by the simulation.
23 *
24 * IMPORTANT: Prior to initialization, the following properties are optional: index, x, y, vx, and vy.
25 * After initialization they will be defined. The optional properties fx and fy are ONLY defined,
26 * if the node's position has been fixed.
27 */
28export interface SimulationNodeDatum {
29 /**
30 * Node’s zero-based index into nodes array. This property is set during the initialization process of a simulation.
31 */
32 index?: number | undefined;
33 /**
34 * Node’s current x-position
35 */
36 x?: number | undefined;
37 /**
38 * Node’s current y-position
39 */
40 y?: number | undefined;
41 /**
42 * Node’s current x-velocity
43 */
44 vx?: number | undefined;
45 /**
46 * Node’s current y-velocity
47 */
48 vy?: number | undefined;
49 /**
50 * Node’s fixed x-position (if position was fixed)
51 */
52 fx?: number | null | undefined;
53 /**
54 * Node’s fixed y-position (if position was fixed)
55 */
56 fy?: number | null | undefined;
57}
58
59/**
60 * The base data structure for the datum of a Simulation Link, as used by ForceLink.
61 * The optional properties contained in this data structure are internally assigned
62 * by when initializing with ForceLink.links(...)
63 *
64 *
65 * IMPORTANT: The source and target properties may be internally mutated in type during the
66 * ForceLink initialization process (possibly being changed from a node index in the nodes array,
67 * or a node id string to the simulation node object which was mapped in using the current
68 * ForceLink.id(...) accessor function.)
69 */
70export interface SimulationLinkDatum<NodeDatum extends SimulationNodeDatum> {
71 /**
72 * Link’s source node.
73 * For convenience, a link’s source and target properties may be initialized using numeric or string identifiers rather than object references; see link.id.
74 * When the link force is initialized (or re-initialized, as when the nodes or links change), any link.source or link.target property which is not an object
75 * is replaced by an object reference to the corresponding node with the given identifier.
76 * After initialization, the source property represents the source node object.
77 */
78 source: NodeDatum | string | number;
79 /**
80 * Link’s source link
81 * For convenience, a link’s source and target properties may be initialized using numeric or string identifiers rather than object references; see link.id.
82 * When the link force is initialized (or re-initialized, as when the nodes or links change), any link.source or link.target property which is not an object
83 * is replaced by an object reference to the corresponding node with the given identifier.
84 * After initialization, the target property represents the target node object.
85 */
86 target: NodeDatum | string | number;
87 /**
88 * The zero-based index into the links array. Internally generated when calling ForceLink.links(...)
89 */
90 index?: number | undefined;
91}
92
93/**
94 * A Force Simulation
95 *
96 * The first generic refers to the type of the datum associated with a node in the simulation.
97 * The second generic refers to the type of the datum associated with a link in the simulation, if applicable.
98 *
99 */
100export interface Simulation<NodeDatum extends SimulationNodeDatum, LinkDatum extends SimulationLinkDatum<NodeDatum> | undefined> {
101 /**
102 * Restart the simulation’s internal timer and return the simulation.
103 * In conjunction with simulation.alphaTarget or simulation.alpha, this method can be used to “reheat” the simulation during interaction,
104 * such as when dragging a node, or to resume the simulation after temporarily pausing it with simulation.stop.
105 */
106 restart(): this;
107
108 /**
109 * Stop the simulation’s internal timer, if it is running, and return the simulation. If the timer is already stopped, this method does nothing.
110 * This method is useful for running the simulation manually; see simulation.tick.
111 */
112 stop(): this;
113
114 /**
115 * Manually steps the simulation by the specified number of *iterations*, and returns the simulation. If *iterations* is not specified, it defaults to 1 (single step).
116 *
117 * For each iteration, it increments the current alpha by (alphaTarget - alpha) × alphaDecay; then invokes each registered force, passing the new alpha;
118 * then decrements each node’s velocity by velocity × velocityDecay; lastly increments each node’s position by velocity.
119 *
120 * This method does not dispatch events; events are only dispatched by the internal timer when the simulation is started automatically upon
121 * creation or by calling simulation.restart. The natural number of ticks when the simulation is started is
122 * ⌈log(alphaMin) / log(1 - alphaDecay)⌉; by default, this is 300.
123 */
124 tick(iterations?: number): this;
125
126 /**
127 * Returns the simulation’s array of nodes as specified to the constructor.
128 */
129 nodes(): NodeDatum[];
130 /**
131 * Set the simulation’s nodes to the specified array of objects, initialize their positions and velocities if necessary,
132 * and then re-initialize any bound forces; Returns the simulation.
133 *
134 * Each node must be an object. The following properties are assigned by the simulation:
135 * - index (the node’s zero-based index into nodes)
136 * - x (the node’s current x-position)
137 * - y (the node’s current y-position)
138 * - vx (the node’s current x-velocity)
139 * - vy (the node’s current y-velocity)
140 *
141 * The position [x,y] and velocity [vx,vy] may be subsequently modified by forces and by the simulation.
142 * If either vx or vy is NaN, the velocity is initialized to [0,0]. If either x or y is NaN, the position is initialized in a phyllotaxis arrangement,
143 * so chosen to ensure a deterministic, uniform distribution.
144 *
145 * To fix a node in a given position, you may specify two additional properties:
146 * - fx (the node’s fixed x-position)
147 * - fy (the node’s fixed y-position)
148 *
149 * At the end of each tick, after the application of any forces, a node with a defined node.fx has node.x reset to this value and node.vx set to zero;
150 * likewise, a node with a defined node.fy has node.y reset to this value and node.vy set to zero.
151 * To unfix a node that was previously fixed, set node.fx and node.fy to null, or delete these properties.
152 *
153 * If the specified array of nodes is modified, such as when nodes are added to or removed from the simulation,
154 * this method must be called again with the new (or changed) array to notify the simulation and bound forces of the change;
155 * the simulation does not make a defensive copy of the specified array.
156 */
157 nodes(nodesData: NodeDatum[]): this;
158
159 /**
160 * Return the current alpha of the simulation, which defaults to 1.
161 *
162 * alpha is roughly analogous to temperature in simulated annealing.
163 * It decreases over time as the simulation “cools down”.
164 * When alpha reaches alphaMin, the simulation stops; see simulation.restart.
165 */
166 alpha(): number;
167 /**
168 * Set the current alpha to the specified number in the range [0,1] and return this simulation.
169 * The default is 1.
170 *
171 * alpha is roughly analogous to temperature in simulated annealing.
172 * It decreases over time as the simulation “cools down”.
173 * When alpha reaches alphaMin, the simulation stops; see simulation.restart.
174 *
175 * @param alpha Current alpha of simulation.
176 */
177 alpha(alpha: number): this;
178
179 /**
180 * Return the current minimum alpha value, which defaults to 0.001.
181 */
182 alphaMin(): number;
183 /**
184 * Set the minimum alpha to the specified number in the range [0,1] and return this simulation.
185 * The default is 0.001. The simulation’s internal timer stops when the current alpha is less than the minimum alpha.
186 * The default alpha decay rate of ~0.0228 corresponds to 300 iterations.
187 *
188 * @param min Minimum alpha of simulation.
189 */
190 alphaMin(min: number): this;
191
192 /**
193 * Return the current alpha decay rate, which defaults to 0.0228… = 1 - pow(0.001, 1 / 300) where 0.001 is the default minimum alpha.
194 */
195 alphaDecay(): number;
196 /**
197 * Set the alpha decay rate to the specified number in the range [0,1] and return this simulation.
198 * The default is 0.0228… = 1 - pow(0.001, 1 / 300) where 0.001 is the default minimum alpha.
199 *
200 * The alpha decay rate determines how quickly the current alpha interpolates towards the desired target alpha;
201 * since the default target alpha is zero, by default this controls how quickly the simulation cools.
202 * Higher decay rates cause the simulation to stabilize more quickly, but risk getting stuck in a local minimum;
203 * lower values cause the simulation to take longer to run, but typically converge on a better layout.
204 * To have the simulation run forever at the current alpha, set the decay rate to zero;
205 * alternatively, set a target alpha greater than the minimum alpha.
206 *
207 * @param decay Alpha decay rate.
208 */
209 alphaDecay(decay: number): this;
210
211 /**
212 * Returns the current target alpha value, which defaults to 0.
213 */
214 alphaTarget(): number;
215 /**
216 * Set the current target alpha to the specified number in the range [0,1] and return this simulation.
217 * The default is 0.
218 *
219 * @param target Alpha target value.
220 */
221 alphaTarget(target: number): this;
222
223 /**
224 * Return the current target alpha value, which defaults to 0.4.
225 */
226 velocityDecay(): number;
227 /**
228 * Set the velocity decay factor to the specified number in the range [0,1] and return this simulation.
229 * The default is 0.4.
230 *
231 * The decay factor is akin to atmospheric friction; after the application of any forces during a tick,
232 * each node’s velocity is multiplied by 1 - decay. As with lowering the alpha decay rate,
233 * less velocity decay may converge on a better solution, but risks numerical instabilities and oscillation.
234 *
235 * @param decay Velocity Decay.
236 */
237 velocityDecay(decay: number): this;
238
239 /**
240 * Return the force with the specified name, or undefined if there is no such force.
241 * (By default, new simulations have no forces.)
242 *
243 * Given that it is in general not known, what type of force has been registered under
244 * a specified name, use the generic to cast the result to the appropriate type, if known.
245 *
246 * @param name Name of the registered force.
247 */
248 // eslint-disable-next-line no-unnecessary-generics
249 force<F extends Force<NodeDatum, LinkDatum>>(name: string): F | undefined;
250 /**
251 * If force is specified, assigns the force for the specified name and returns this simulation.
252 * To remove the force with the given name, pass null as the force.
253 */
254 force(name: string, force: null | Force<NodeDatum, LinkDatum>): this;
255
256 /**
257 * Return the node closest to the position [x,y] with the given search radius.
258 * If radius is not specified, it defaults to infinity.
259 * If there is no node within the search area, returns undefined.
260 *
261 * @param x x-coordinate
262 * @param y y-coordinate
263 * @param radius Optional search radius. Defaults to infinity.
264 */
265 find(x: number, y: number, radius?: number): NodeDatum | undefined;
266
267 /**
268 * Returns this simulation’s current random source which defaults to a fixed-seed linear congruential generator.
269 * See also random.source.
270 */
271 randomSource(): () => number;
272 /**
273 * Sets the function used to generate random numbers; this should be a function that returns a number between 0 (inclusive) and 1 (exclusive).
274 *
275 * @param source The function used to generate random numbers.
276 */
277 randomSource(source: () => number): this;
278
279 /**
280 * Return the first currently-assigned listener matching the specified typenames, if any.
281 *
282 * @param typenames The typenames is a string containing one or more typename separated by whitespace. Each typename is a type,
283 * optionally followed by a period (.) and a name, such as "tick.foo" and "tick.bar"; the name allows multiple listeners to be registered for the same type.
284 * The type must be one of the following: "tick" (after each tick of the simulation’s internal timer) or
285 * "end" (after the simulation’s timer stops when alpha < alphaMin).
286 */
287 on(typenames: 'tick' | 'end' | string): ((this: Simulation<NodeDatum, LinkDatum>) => void) | undefined;
288 /**
289 * Sets the event listener for the specified typenames and returns this simulation.
290 * If an event listener was already registered for the same type and name, the existing listener is removed before the new listener is added.
291 * If listener is null, removes the current event listeners for the specified typenames, if any.
292 * When a specified event is dispatched, each listener will be invoked with the this context as the simulation.
293 */
294 on(typenames: 'tick' | 'end' | string, listener: null | ((this: this) => void)): this;
295}
296
297/**
298 * Create a new simulation with the specified array of nodes and no forces.
299 * If nodes is not specified, it defaults to the empty array.
300 * The simulator starts automatically; use simulation.on to listen for tick events as the simulation runs.
301 * If you wish to run the simulation manually instead, call simulation.stop, and then call simulation.tick as desired.
302 *
303 * Use this signature, when creating a simulation WITHOUT link force(s).
304 *
305 * The generic refers to the type of the data for a node.
306 *
307 * @param nodesData Optional array of nodes data, defaults to empty array.
308 */
309export function forceSimulation<NodeDatum extends SimulationNodeDatum>(nodesData?: NodeDatum[]): Simulation<NodeDatum, undefined>;
310/**
311 * Create a new simulation with the specified array of nodes and no forces.
312 * If nodes is not specified, it defaults to the empty array.
313 * The simulator starts automatically; use simulation.on to listen for tick events as the simulation runs.
314 * If you wish to run the simulation manually instead, call simulation.stop, and then call simulation.tick as desired.
315 *
316 * Use this signature, when creating a simulation WITH link force(s).
317 *
318 * The first generic refers to the type of data for a node.
319 * The second generic refers to the type of data for a link.
320 *
321 * @param nodesData Optional array of nodes data, defaults to empty array.
322 */
323// eslint-disable-next-line no-unnecessary-generics
324export function forceSimulation<NodeDatum extends SimulationNodeDatum, LinkDatum extends SimulationLinkDatum<NodeDatum>>(nodesData?: NodeDatum[]): Simulation<NodeDatum, LinkDatum>;
325
326// ----------------------------------------------------------------------
327// Forces
328// ----------------------------------------------------------------------
329
330/**
331 * A force is simply a function that modifies nodespositions or velocities; in this context, a force can apply a classical physical force such as electrical charge or gravity,
332 * or it can resolve a geometric constraint, such as keeping nodes within a bounding box or keeping linked nodes a fixed distance apart.
333 *
334 * Forces typically read the nodes current position [x,y] and then add to (or subtract from) the nodes velocity [vx,vy].
335 * However, forces may alsopeek aheadto the anticipated next position of the node, [x + vx,y + vy]; this is necessary for resolving geometric constraints through iterative relaxation.
336 * Forces may also modify the position directly, which is sometimes useful to avoid adding energy to the simulation, such as when recentering the simulation in the viewport.
337 *
338 * Forces may optionally implement force.initialize to receive the simulations array of nodes.
339 */
340export interface Force<NodeDatum extends SimulationNodeDatum, LinkDatum extends SimulationLinkDatum<NodeDatum> | undefined> {
341 /**
342 * Apply this force, optionally observing the specified alpha.
343 * Typically, the force is applied to the array of nodes previously passed to force.initialize,
344 * however, some forces may apply to a subset of nodes, or behave differently.
345 * For example, d3.forceLink applies to the source and target of each link.
346 */
347 (alpha: number): void;
348 /**
349 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
350 * and when the simulations nodes change via simulation.nodes.
351 *
352 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
353 */
354 initialize?(nodes: NodeDatum[], random: () => number): void;
355}
356
357// Centering ------------------------------------------------------------
358
359/**
360 * The centering force translates nodes uniformly so that the mean position of all nodes
361 * (the center of mass if all nodes have equal weight) is at the given position [x,y].
362 * This force modifies the positions of nodes on each application; it does not modify velocities,
363 * as doing so would typically cause the nodes to overshoot and oscillate around the desired center.
364 * This force helps keeps nodes in the center of the viewport, and unlike the positioning force,
365 * it does not distort their relative positions.
366 *
367 * The generic refers to the type of data for a node.
368 */
369export interface ForceCenter<NodeDatum extends SimulationNodeDatum> extends Force<NodeDatum, any> {
370 /**
371 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
372 * and when the simulations nodes change via simulation.nodes.
373 *
374 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
375 */
376 initialize(nodes: NodeDatum[], random: () => number): void;
377
378 /**
379 * Return the current x-coordinate of the centering position, which defaults to zero.
380 */
381 x(): number;
382 /**
383 * Set the x-coordinate of the centering position.
384 *
385 * @param x x-coordinate.
386 */
387 x(x: number): this;
388
389 /**
390 * Return the current y-coordinate of the centering position, which defaults to zero.
391 */
392 y(): number;
393 /**
394 * Set the y-coordinate of the centering position.
395 *
396 * @param y y-coordinate.
397 */
398 y(y: number): this;
399
400 /**
401 * Returns the forces current strength, which defaults to 1.
402 */
403 strength(): number;
404
405 /**
406 * Sets the centering forces strength.
407 * A reduced strength of e.g. 0.05 softens the movements on interactive graphs in which new nodes enter or exit the graph.
408 * @param strength The centering force's strength.
409 */
410 strength(strength: number): this;
411}
412
413/**
414 * Create a new centering force with the specified x- and y- coordinates.
415 * If x and y are not specified, they default to [0,0].
416 *
417 * The centering force translates nodes uniformly so that the mean position of all nodes
418 * (the center of mass if all nodes have equal weight) is at the given position [x,y].
419 * This force modifies the positions of nodes on each application; it does not modify velocities,
420 * as doing so would typically cause the nodes to overshoot and oscillate around the desired center.
421 * This force helps keeps nodes in the center of the viewport, and unlike the positioning force,
422 * it does not distort their relative positions.
423 *
424 * The generic refers to the type of data for a node.
425 *
426 * @param x An optional x-coordinate for the centering position, defaults to 0.
427 * @param y An optional y-coordinate for the centering position, defaults to 0.
428 */
429// eslint-disable-next-line no-unnecessary-generics
430export function forceCenter<NodeDatum extends SimulationNodeDatum>(x?: number, y?: number): ForceCenter<NodeDatum>;
431
432// Collision ------------------------------------------------------------
433
434/**
435 * The collision force treats nodes as circles with a given radius, rather than points, and prevents nodes from overlapping.
436 * More formally, two nodes a and b are separated so that the distance between a and b is at least radius(a) + radius(b).
437 * To reduce jitter, this is by default asoftconstraint with a configurable strength and iteration count.
438 *
439 * The generic refers to the type of data for a node.
440 */
441export interface ForceCollide<NodeDatum extends SimulationNodeDatum> extends Force<NodeDatum, any> {
442 /**
443 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
444 * and when the simulations nodes change via simulation.nodes.
445 *
446 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
447 */
448 initialize(nodes: NodeDatum[], random: () => number): void;
449
450 /**
451 * Returns the current radius accessor function.
452 */
453 radius(): (node: NodeDatum, i: number, nodes: NodeDatum[]) => number;
454 /**
455 * Sets the radius accessor to the specified number or function, re-evaluates the radius accessor for each node, and returns this force.
456 * The radius accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
457 * The resulting number is then stored internally, such that the radius of each node is only recomputed when the
458 * force is initialized or when this method is called with a new radius, and not on every application of the force.
459 */
460 radius(radius: number | ((node: NodeDatum, i: number, nodes: NodeDatum[]) => number)): this;
461
462 /**
463 * Return the current strength, which defaults to 1.
464 */
465 strength(): number;
466 /**
467 * Set the force strength to the specified number in the range [0,1] and return this force.
468 * The default strength is 0.7.
469 *
470 * Overlapping nodes are resolved through iterative relaxation.
471 * For each node, the other nodes that are anticipated to overlap at the next tick (using the anticipated positions [x + vx,y + vy]) are determined;
472 * the nodes velocity is then modified to push the node out of each overlapping node.
473 * The change in velocity is dampened by the forces strength such that the resolution of simultaneous overlaps can be blended together to find a stable solution.
474 *
475 * @param strength Strength.
476 */
477 strength(strength: number): this;
478
479 /**
480 * Return the current iteration count which defaults to 1.
481 */
482 iterations(): number;
483 /**
484 * Sets the number of iterations per application to the specified number and return this force.
485 *
486 * Increasing the number of iterations greatly increases the rigidity of the constraint and avoids partial overlap of nodes,
487 * but also increases the runtime cost to evaluate the force.
488 *
489 * @param iterations Number of iterations.
490 */
491 iterations(iterations: number): this;
492}
493
494/**
495 * Creates a new circle collision force with the specified radius.
496 * If radius is not specified, it defaults to the constant one for all nodes.
497 */
498export function forceCollide<NodeDatum extends SimulationNodeDatum>(radius?: number | ((node: NodeDatum, i: number, nodes: NodeDatum[]) => number)): ForceCollide<NodeDatum>;
499
500// Link ----------------------------------------------------------------
501
502/**
503 * The link force pushes linked nodes together or apart according to the desired link distance.
504 * The strength of the force is proportional to the difference between the linked nodesdistance and the target distance, similar to a spring force.
505 *
506 * The first generic refers to the type of data for a node.
507 * The second generic refers to the type of data for a link.
508 */
509export interface ForceLink<NodeDatum extends SimulationNodeDatum, LinkDatum extends SimulationLinkDatum<NodeDatum>> extends Force<NodeDatum, LinkDatum> {
510 /**
511 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
512 * and when the simulations nodes change via simulation.nodes.
513 *
514 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
515 */
516 initialize(nodes: NodeDatum[], random: () => number): void;
517
518 /**
519 * Return the current array of links, which defaults to the empty array.
520 *
521 */
522 links(): LinkDatum[];
523 /**
524 * Set the array of links associated with this force, recompute the distance and strength parameters for each link, and return this force.
525 *
526 * Each link is an object with the following properties:
527 * * source - the links source node; see simulation.nodes
528 * * target - the links target node; see simulation.nodes
529 * * index - the zero-based index into links, assigned by this method
530 *
531 * For convenience, a links source and target properties may be initialized using numeric or string identifiers rather than object references; see link.id.
532 * When the link force is initialized (or re-initialized, as when the nodes or links change), any link.source or link.target property which is not an object
533 * is replaced by an object reference to the corresponding node with the given identifier.
534 * If the specified array of links is modified, such as when links are added to or removed from the simulation,
535 * this method must be called again with the new (or changed) array to notify the force of the change;
536 * the force does not make a defensive copy of the specified array.
537 *
538 * @param links An array of link data.
539 */
540 links(links: LinkDatum[]): this;
541
542 /**
543 * Return the current node id accessor, which defaults to the numeric node.index.
544 */
545 id(): (node: NodeDatum, i: number, nodesData: NodeDatum[]) => (string | number);
546 /**
547 * Set the node id accessor to the specified function and return this force.
548 *
549 * The default id accessor allows each link’s source and target to be specified as a zero-based index
550 * into the nodes array.
551 *
552 * The id accessor is invoked for each node whenever the force is initialized,
553 * as when the nodes or links change, being passed the node, the zero-based index of the node in the node array, and the node array.
554 *
555 * @param id A node id accessor function which is invoked for each node in the simulation,
556 * being passed the node, the zero-based index of the node in the node array, and the node array. It returns a string or number to represent the node id which can be used
557 * for matching link source and link target strings during the ForceLink initialization.
558 */
559 id(id: (node: NodeDatum, i: number, nodesData: NodeDatum[]) => (string | number)): this;
560
561 /**
562 * Return the current distance accessor, which defaults to implying a default distance of 30.
563 */
564 distance(): (link: LinkDatum, i: number, links: LinkDatum[]) => number;
565 /**
566 * Sets the distance accessor to the specified number or function, re-evaluates the distance accessor for each link, and returns this force.
567 * The distance accessor is invoked for each link, being passed the link and its zero-based index.
568 * The resulting number is then stored internally, such that the distance of each link is only recomputed when the
569 * force is initialized or when this method is called with a new distance, and not on every application of the force.
570 */
571 distance(distance: number | ((link: LinkDatum, i: number, links: LinkDatum[]) => number)): this;
572
573 /**
574 * Return the current strength accessor.
575 * For details regarding the default behavior see: {@link https://github.com/d3/d3-force#link_strength}
576 */
577 strength(): (link: LinkDatum, i: number, links: LinkDatum[]) => number;
578 /**
579 * Sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each link, and returns this force.
580 * The strength accessor is invoked for each link, being passed the link and its zero-based index.
581 * The resulting number is then stored internally, such that the strength of each link is only recomputed when the
582 * force is initialized or when this method is called with a new strength, and not on every application of the force.
583 */
584 strength(strength: number | ((link: LinkDatum, i: number, links: LinkDatum[]) => number)): this;
585
586 /**
587 * Return the current iteration count which defaults to 1.
588 */
589 iterations(): number;
590 /**
591 * Sets the number of iterations per application to the specified number and return this force.
592 *
593 * Increasing the number of iterations greatly increases the rigidity of the constraint and is useful for complex structures such as lattices,
594 * but also increases the runtime cost to evaluate the force.
595 *
596 * @param iterations Number of iterations.
597 */
598 iterations(iterations: number): this;
599}
600
601/**
602 * Creates a new link force with the specified links and default parameters.
603 * If links is not specified, it defaults to the empty array.
604 */
605export function forceLink<NodeDatum extends SimulationNodeDatum, LinksDatum extends SimulationLinkDatum<NodeDatum>>(links?: LinksDatum[]): ForceLink<NodeDatum, LinksDatum>;
606
607// Many Body ----------------------------------------------------------------
608
609/**
610 * The many-body (or n-body) force applies mutually amongst all nodes. It can be used to simulate gravity (attraction) if the strength is positive,
611 * or electrostatic charge (repulsion) if the strength is negative. This implementation uses quadtrees and the BarnesHut approximation to greatly
612 * improve performance; the accuracy can be customized using the theta parameter.
613 *
614 * Unlike links, which only affect two linked nodes, the charge force is global: every node affects every other node, even if they are on disconnected subgraphs.
615 *
616 * The generic refers to the type of data for a node.
617 */
618export interface ForceManyBody<NodeDatum extends SimulationNodeDatum> extends Force<NodeDatum, any> {
619 /**
620 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
621 * and when the simulations nodes change via simulation.nodes.
622 *
623 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
624 */
625 initialize(nodes: NodeDatum[], random: () => number): void;
626
627 /**
628 * Return the current strength accessor.
629 *
630 * For details regarding the default behavior see: {@link https://github.com/d3/d3-force#manyBody_strength}
631 */
632 strength(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
633 /**
634 * sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force.
635 * A positive value causes nodes to attract each other, similar to gravity, while a negative value causes nodes to repel each other, similar to electrostatic charge.
636 * The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
637 * The resulting number is then stored internally, such that the strength of each node is only recomputed when the
638 * force is initialized or when this method is called with a new strength, and not on every application of the force.
639 */
640 strength(strength: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
641
642 /**
643 * Return the current value of the BarnesHut approximation criterion , which defaults to 0.9
644 */
645 theta(): number;
646 /**
647 * Set the BarnesHut approximation criterion to the specified number and returns this force.
648 *
649 * To accelerate computation, this force implements the BarnesHut approximation which takes O(n log n) per application
650 * where n is the number of nodes. For each application, a quadtree stores the current node positions;
651 * then for each node, the combined force of all other nodes on the given node is computed.
652 * For a cluster of nodes that is far away, the charge force can be approximated by treating the cluster as a single, larger node.
653 * The theta parameter determines the accuracy of the approximation:
654 * if the ratio w / l of the width w of the quadtree cell to the distance l from the node to the cells center of mass is less than theta,
655 * all nodes in the given cell are treated as a single node rather than individually.
656 *
657 * The default value is 0.9.
658 *
659 * @param theta Value for the theta parameter.
660 */
661 theta(theta: number): this;
662
663 /**
664 * Returns the current minimum distance over which this force is considered, which defaults to 1.
665 */
666 distanceMin(): number;
667 /**
668 * Sets the minimum distance between nodes over which this force is considered.
669 *
670 * A minimum distance establishes an upper bound on the strength of the force between two nearby nodes, avoiding instability.
671 * In particular, it avoids an infinitely-strong force if two nodes are exactly coincident; in this case, the direction of the force is random.
672 *
673 * The default value is 1.
674 *
675 * @param distance The minimum distance between nodes over which this force is considered.
676 */
677 distanceMin(distance: number): this;
678
679 /**
680 * Returns the current maximum distance over which this force is considered, which defaults to infinity.
681 */
682 distanceMax(): number;
683 /**
684 * Sets the maximum distance between nodes over which this force is considered.
685 *
686 * Specifying a finite maximum distance improves performance and produces a more localized layout.
687 *
688 * The default value is infinity.
689 *
690 * @param distance The maximum distance between nodes over which this force is considered.
691 */
692 distanceMax(distance: number): this;
693}
694
695/**
696 * Creates a new many-body force with the default parameters.
697 *
698 * The many-body (or n-body) force applies mutually amongst all nodes. It can be used to simulate gravity (attraction) if the strength is positive,
699 * or electrostatic charge (repulsion) if the strength is negative. This implementation uses quadtrees and the BarnesHut approximation to greatly
700 * improve performance; the accuracy can be customized using the theta parameter.
701 *
702 * Unlike links, which only affect two linked nodes, the charge force is global: every node affects every other node, even if they are on disconnected subgraphs.
703 *
704 * The generic refers to the type of data for a node.
705 */
706// eslint-disable-next-line no-unnecessary-generics
707export function forceManyBody<NodeDatum extends SimulationNodeDatum>(): ForceManyBody<NodeDatum>;
708
709// Positioning ----------------------------------------------------------------
710
711/**
712 * The x-positioning force pushes nodes towards a desired position along the given dimension with a configurable strength.
713 * The strength of the force is proportional to the one-dimensional distance between the nodes position and the target position.
714 * While this force can be used to position individual nodes, it is intended primarily for global forces that apply to all (or most) nodes.
715 *
716 * The generic refers to the type of data for a node.
717 */
718export interface ForceX<NodeDatum extends SimulationNodeDatum> extends Force<NodeDatum, any> {
719 /**
720 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
721 * and when the simulations nodes change via simulation.nodes.
722 *
723 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
724 */
725 initialize(nodes: NodeDatum[], random: () => number): void;
726
727 /**
728 * Returns the current strength accessor, which defaults to a constant strength for all nodes of 0.1.
729 */
730 strength(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
731 /**
732 * Sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force.
733 * The strength determines how much to increment the node’s x-velocity: (x - node.x) × strength.
734 * For example, a value of 0.1 indicates that the node should move a tenth of the way from its current x-position to the target x-position with each application.
735 * Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints.
736 * A value outside the range [0,1] is not recommended.
737 * The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
738 * The resulting number is then stored internally, such that the strength of each node is only recomputed when the
739 * force is initialized or when this method is called with a new strength, and not on every application of the force.
740 */
741 strength(strength: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
742
743 /**
744 * Return the current x-accessor, which defaults to a function returning 0 for all nodes.
745 */
746 x(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
747 /**
748 * Sets the x-coordinate accessor to the specified number or function, re-evaluates the x-accessor for each node, and returns this force.
749 * The x-accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
750 * The resulting number is then stored internally, such that the target x-coordinate of each node is only recomputed
751 * when the force is initialized or when this method is called with a new x, and not on every application of the force.
752 */
753 x(x: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
754}
755
756/**
757 * Creates a new positioning force along the x-axis towards the given position x.
758 * If x is not specified, it defaults to 0.
759 */
760export function forceX<NodeDatum extends SimulationNodeDatum>(x?: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): ForceX<NodeDatum>;
761
762/**
763 * The y-positioning force pushes nodes towards a desired position along the given dimension with a configurable strength.
764 * The strength of the force is proportional to the one-dimensional distance between the nodes position and the target position.
765 * While this force can be used to position individual nodes, it is intended primarily for global forces that apply to all (or most) nodes.
766 *
767 * The generic refers to the type of data for a node.
768 */
769export interface ForceY<NodeDatum extends SimulationNodeDatum> extends Force<NodeDatum, any> {
770 /**
771 * Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
772 * and when the simulations nodes change via simulation.nodes.
773 *
774 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
775 */
776 initialize(nodes: NodeDatum[], random: () => number): void;
777
778 /**
779 * Returns the current strength accessor, which defaults to a constant strength for all nodes of 0.1.
780 */
781 strength(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
782 /**
783 * Sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force.
784 * The strength determines how much to increment the node’s y-velocity: (y - node.y) × strength.
785 * For example, a value of 0.1 indicates that the node should move a tenth of the way from its current y-position to the target y-position with each application.
786 * Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints.
787 * A value outside the range [0,1] is not recommended.
788 * The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
789 * The resulting number is then stored internally, such that the strength of each node is only recomputed when the
790 * force is initialized or when this method is called with a new strength, and not on every application of the force.
791 */
792 strength(strength: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
793
794 /**
795 * Return the current y-accessor, which defaults to a function returning 0 for all nodes.
796 */
797 y(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
798 /**
799 * Sets the y-coordinate accessor to the specified number or function, re-evaluates the y-accessor for each node, and returns this force.
800 * The y-accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
801 * The resulting number is then stored internally, such that the target y-coordinate of each node is only recomputed
802 * when the force is initialized or when this method is called with a new y, and not on every application of the force.
803 */
804 y(y: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
805}
806
807/**
808 * Creates a new positioning force along the y-axis towards the given position y.
809 * If y is not specified, it defaults to 0.
810 */
811export function forceY<NodeDatum extends SimulationNodeDatum>(y?: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): ForceY<NodeDatum>;
812
813/**
814 * The radial force is similar to the x- and y-positioning forces, except it pushes nodes towards the closest point on a given circle.
815 * The circle is of the specified radius centered atx,y⟩. If x and y are not specified, they default to ⟨0,0⟩.
816 * The strength of the force is proportional to the one-dimensional distance between the nodes position and the target position.
817 * While this force can be used to position individual nodes, it is intended primarily for global forces that apply to all (or most) nodes.
818 *
819 * The generic refers to the type of data for a node.
820 */
821export interface ForceRadial<NodeDatum extends SimulationNodeDatum> extends Force<NodeDatum, any> {
822 /**
823 * Assigns the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force
824 * and when the simulations nodes change via simulation.nodes.
825 *
826 * A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.
827 */
828 initialize(nodes: NodeDatum[], random: () => number): void;
829
830 /**
831 * Returns the current strength accessor, which defaults to a constant strength for all nodes of 0.1.
832 */
833 strength(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
834 /**
835 * Sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force.
836 * The strength determines how much to increment the node’s x- and y-velocity.
837 * For example, a value of 0.1 indicates that the node should move a tenth of the way from its current position to the closest point on the circle with each application.
838 * Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints.
839 * A value outside the range [0,1] is not recommended.
840 * The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
841 * The resulting number is then stored internally, such that the strength of each node is only recomputed when the
842 * force is initialized or when this method is called with a new strength, and not on every application of the force.
843 */
844 strength(strength: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
845
846 /**
847 * Return the current radius accessor for the circle.
848 */
849 radius(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
850 /**
851 * Sets the circle radius to the specified number or function, re-evaluates the radius accessor for each node, and returns this force.
852 * The radius accessor is invoked for each node in the simulation, being passed the node and its zero-based index.
853 * The resulting number is then stored internally, such that the target radius of each node is only recomputed when
854 * the force is initialized or when this method is called with a new radius, and not on every application of the force.
855 */
856 radius(radius: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
857
858 /**
859 * Return the current x-accessor for the circle center, which defaults to a function returning 0 for all nodes.
860 */
861 x(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
862 /**
863 * Sets the x-coordinate of the circle center to the specified number and returns this force.
864 */
865 x(x: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
866
867 /**
868 * Return the current y-accessor for the circle center, which defaults to a function returning 0 for all nodes.
869 */
870 y(): (d: NodeDatum, i: number, data: NodeDatum[]) => number;
871 /**
872 * Sets the y-coordinate of the circle center to the specified number and returns this force.
873 */
874 y(y: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): this;
875}
876
877/**
878 * Create a new radial positioning force towards a circle of the specified radius centered atx,y⟩.
879 * If x and y are not specified, they default to ⟨0,0⟩.
880 *
881 * The strength of the force is proportional to the one-dimensional distance between the nodes position and the target position.
882 * While this force can be used to position individual nodes, it is intended primarily for global forces that apply to all (or most) nodes.
883 *
884 * The generic refers to the type of data for a node.
885 */
886export function forceRadial<NodeDatum extends SimulationNodeDatum>(radius: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number),
887 x?: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number), y?: number | ((d: NodeDatum, i: number, data: NodeDatum[]) => number)): ForceRadial<NodeDatum>;
888
\No newline at end of file