UNPKG

22.4 kBTypeScriptView Raw
1// Last module patch version validated against: 3.0.1
2
3// ---------------------------------------------------------------------
4// Chord
5// ---------------------------------------------------------------------
6
7/**
8 * A chord subgroup serving as source or target of a chord between two nodes i an j (where i may be equal to j).
9 */
10export interface ChordSubgroup {
11 /**
12 * Start angle of the chord subgroup in radians
13 */
14 startAngle: number;
15
16 /***
17 * End angle of the chord subgroup in radians
18 */
19 endAngle: number;
20
21 /**
22 * The flow value in matrix[i][j] from node i to node j
23 */
24 value: number;
25
26 /**
27 * The node index i
28 */
29 index: number;
30}
31
32/**
33 * A chord represents the combined bidirectional flow between two nodes i and j (where i may be equal to j)
34 */
35export interface Chord {
36 /**
37 * Chord subgroup constituting the source of Chord
38 */
39 source: ChordSubgroup;
40 /**
41 * Chord subgroup constituting the Target of Chord
42 */
43 target: ChordSubgroup;
44}
45
46/**
47 * A chord group for a given node i representing the combined outflow for node i,
48 * corresponding to the elements matrix[i][0 … n - 1].
49 */
50export interface ChordGroup {
51 /**
52 * The start angle of the chord group in radians
53 */
54 startAngle: number;
55
56 /**
57 * The end angle of the chord group in radians
58 */
59 endAngle: number;
60
61 /**
62 * The total outgoing flow value for node i
63 */
64 value: number;
65
66 /**
67 * The node index i
68 */
69 index: number;
70}
71
72/**
73 * An array of chords, where each chord represents the combined bidirectional flow between two nodes i and j (where i may be equal to j).
74 * The chords are based on a (n x n) matrix of flows between nodes.
75 *
76 * The chords are typically passed to d3.ribbon to display the network relationships.
77 * The returned array includes only chord objects for which the value matrix[i][j] or matrix[j][i] is non-zero.
78 * Furthermore, the returned array only contains unique chords: a given chord ij represents the bidirectional flow from i to j and from j to i,
79 * and does not contain a duplicate chord ji; i and j are chosen such that the chord’s source always represents the larger of matrix[i][j] and matrix[j][i].
80 * In other words, chord.source.index equals chord.target.subindex, chord.source.subindex equals chord.target.index,
81 * chord.source.value is greater than or equal to chord.target.value, and chord.source.value is always greater than zero.
82 */
83export interface Chords extends Array<Chord> {
84 /**
85 * An array of length n, where each group represents the combined outflow for node i,
86 * corresponding to the elements matrix[i][0 … n - 1]
87 */
88 groups: ChordGroup[];
89}
90
91/**
92 * A D3 chord diagram Layout to visualize relationships or network flow with an aesthetically-pleasing circular layout.
93 *
94 * The relationships are represented as a square matrix of size n×n, where the matrix represents the directed flow amongst a network (a complete digraph) of n nodes.
95 */
96export interface ChordLayout {
97 /**
98 * Computes the chord layout for the specified square matrix of size n×n, where the matrix represents the directed flow amongst a network (a complete digraph) of n nodes.
99 *
100 * @param matrix An (n x n) matrix representing the directed flow amongst a network (a complete digraph) of n nodes.The given matrix must be an array of length n,
101 * where each element matrix[i] is an array of n numbers, where each matrix[i][j] represents the flow from the ith node in the network to the jth node.
102 * Each number matrix[i][j] must be nonnegative, though it can be zero if there is no flow from node i to node j.
103 */
104 (matrix: number[][]): Chords;
105
106 /**
107 * Returns the current pad angle in radians, which defaults to zero.
108 */
109 padAngle(): number;
110 /**
111 * Sets the pad angle between adjacent groups to the specified number in radians and returns this chord layout.
112 *
113 * The default is zero.
114 *
115 * @param angle Pad angle between adjacent groups in radians.
116 */
117 padAngle(angle: number): this;
118
119 /**
120 * Returns the current group comparator, which defaults to null.
121 */
122 sortGroups(): ((a: number, b: number) => number) | null;
123 /**
124 * Sets the group comparator to the specified function or null and returns this chord layout.
125 * If the group comparator is non-null, it is used to sort the groups by their total outflow.
126 * See also d3.ascending and d3.descending.
127 */
128 sortGroups(compare: null | ((a: number, b: number) => number)): this;
129
130 /**
131 * Returns the current subgroup comparator, which defaults to null.
132 */
133 sortSubgroups(): ((a: number, b: number) => number) | null;
134 /**
135 * Sets the subgroup comparator to the specified function or null and returns this chord layout.
136 * If the subgroup comparator is non-null, it is used to sort the subgroups corresponding to matrix[i][0 … n - 1] for a given group i by their total outflow.
137 * See also d3.ascending and d3.descending.
138 */
139 sortSubgroups(compare: null | ((a: number, b: number) => number)): this;
140
141 /**
142 * Returns the current chord comparator, which defaults to null.
143 */
144 sortChords(): ((a: number, b: number) => number) | null;
145 /**
146 * Sets the chord comparator to the specified function or null and returns this chord layout.
147 * If the chord comparator is non-null, it is used to sort the chords by their combined flow; this only affects the z-order of the chords.
148 * See also d3.ascending and d3.descending.
149 */
150 sortChords(compare: null | ((a: number, b: number) => number)): this;
151}
152
153/**
154 * Constructs a new chord diagram layout with the default settings.
155 */
156export function chord(): ChordLayout;
157
158/**
159 * A chord layout for directional flows. The chord from i to j is generated from the value in matrix[i][j] only.
160 */
161export function chordDirected(): ChordLayout;
162
163/**
164 * A transposed chord layout. Useful to highlight outgoing (rather than incoming) flows.
165 */
166export function chordTranspose(): ChordLayout;
167
168// ---------------------------------------------------------------------
169// Ribbon
170// ---------------------------------------------------------------------
171
172/**
173 * A minimal interface to support the default accessors used by RibbonGenerator for properties of
174 * source and target objects of a Ribbon.
175 *
176 * (Corresponds to ChordSubgroup)
177 */
178export interface RibbonSubgroup {
179 /**
180 * Start angle of the ribbon subgroup in radians
181 */
182 startAngle: number;
183 /**
184 * End angle of the ribbon subgroup in radians
185 */
186 endAngle: number;
187 /**
188 * Radius of the ribbon subgroup
189 */
190 radius: number;
191}
192
193/**
194 * A minimal interface to support the default source and target accessors used by RibbonGenerator.
195 * (Corresponds to Chord)
196 */
197export interface Ribbon {
198 /**
199 * Ribbon subgroup constituting the source of the Ribbon
200 */
201 source: RibbonSubgroup;
202 /**
203 * Ribbon subgroup constituting the target of the Ribbon
204 */
205 target: RibbonSubgroup;
206}
207
208/**
209 * A ribbon generator to support rendering of chords in a chord diagram.
210 *
211 * The first generic corresponds to the type of the "this" context within which the ribbon generator and its accessor functions will be invoked.
212 *
213 * The second generic corresponds to the datum type representing a chord for which the ribbon is to be generated. The default type is Ribbon.
214 *
215 * The third generic corresponds to the datum type of the chord subgroup, i.e. source or target of the cord. The default type is RibbonSubgroup.
216 */
217export interface RibbonGenerator<This, RibbonDatum, RibbonSubgroupDatum> {
218 /**
219 * Generates a ribbon for the given arguments.
220 *
221 * IMPORTANT: If the ribbon generator has been configured with a rendering context,
222 * then the ribbon is rendered to this context as a sequence of path method calls and this function returns void.
223 *
224 * The "this" context within which this function is invoked, will be the context within which the accessor methods of the generator are invoked.
225 * All arguments passed into this function, will be passed to the accessor functions of the generator.
226 *
227 * @param d The datum representing the chord for which the ribbon is to be generated.
228 */
229 (this: This, d: RibbonDatum, ...args: any[]): void;
230 /**
231 * Generates a ribbon for the given arguments.
232 *
233 * IMPORTANT: If the rendering context of the ribbon generator is null,
234 * then the ribbon is returned as a path data string.
235 *
236 * The "this" context within which this function is invoked, will be the context within which the accessor methods of the generator are invoked.
237 * All arguments passed into this function, will be passed to the accessor functions of the generator.
238 *
239 * @param d The datum representing the chord for which the ribbon is to be generated.
240 */
241 (this: This, d: RibbonDatum, ...args: any[]): string | null;
242
243 /**
244 * Returns the current source accessor, which defaults to a function returning the "source" property of the first argument passed into the accessor.
245 */
246 source(): (this: This, d: RibbonDatum, ...args: any[]) => RibbonSubgroupDatum;
247 /**
248 * Sets the source accessor to the specified function and returns this ribbon generator.
249 *
250 * @param source An accessor function returning the source datum of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
251 * receives the same arguments that were passed into the ribbon generator.
252 */
253 source(source: (this: This, d: RibbonDatum, ...args: any[]) => RibbonSubgroupDatum): this;
254
255 /**
256 * Returns the current target accessor, which defaults to a function returning the "target" property of the first argument passed into the accessor.
257 */
258 target(): (this: This, d: RibbonDatum, ...args: any[]) => RibbonSubgroupDatum;
259 /**
260 * Sets the target accessor to the specified function and returns this ribbon generator.
261 *
262 * @param target An accessor function returning the target datum of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
263 * receives the same arguments that were passed into the ribbon generator.
264 */
265 target(target: (this: This, d: RibbonDatum, ...args: any[]) => RibbonSubgroupDatum): this;
266
267 /**
268 * Returns the current radius accessor, which defaults to a function returning the "radius" property (assumed to be a number) of the source or
269 * target object returned by the source or target accessor, respectively.
270 */
271 radius(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
272 /**
273 * Sets the radius to a fixed number and returns this ribbon generator.
274 *
275 * @param radius A fixed numeric value for the radius.
276 */
277 radius(radius: number): this;
278 /**
279 * Sets the radius accessor to the specified function and returns this ribbon generator.
280 *
281 * @param radius An accessor function which is invoked for the source and target of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
282 * receives as the first argument the source or target object returned by the respective source or target accessor function of the generator.
283 * It is also passed any additional arguments that were passed into the generator, with the exception of the first element representing the chord datum itself.
284 * The function returns the radius value.
285 */
286 radius(radius: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
287
288 /**
289 * Returns the current source radius accessor, which defaults to a function returning the "radius" property (assumed to be a number) of the source or
290 * target object returned by the source or target accessor, respectively.
291 */
292 sourceRadius(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
293 /**
294 * Sets the source radius to a fixed number and returns this ribbon generator.
295 *
296 * @param radius A fixed numeric value for the source radius.
297 */
298 sourceRadius(radius: number): this;
299 /**
300 * Sets the source radius accessor to the specified function and returns this ribbon generator.
301 *
302 * @param radius An accessor function which is invoked for the source and target of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
303 * receives as the first argument the source or target object returned by the respective source or target accessor function of the generator.
304 * It is also passed any additional arguments that were passed into the generator, with the exception of the first element representing the chord datum itself.
305 * The function returns the source radius value.
306 */
307 sourceRadius(radius: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
308
309 /**
310 * Returns the current target radius accessor, which defaults to a function returning the "radius" property (assumed to be a number) of the source or
311 * target object returned by the source or target accessor, respectively.
312 */
313 targetRadius(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
314 /**
315 * Sets the target radius to a fixed number and returns this ribbon generator.
316 *
317 * @param radius A fixed numeric value for the target radius.
318 */
319 targetRadius(radius: number): this;
320 /**
321 * Sets the target radius accessor to the specified function and returns this ribbon generator.
322 *
323 * @param radius An accessor function which is invoked for the source and target of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
324 * receives as the first argument the source or target object returned by the respective source or target accessor function of the generator.
325 * It is also passed any additional arguments that were passed into the generator, with the exception of the first element representing the chord datum itself.
326 * The function returns the target radius value.
327 */
328 targetRadius(radius: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
329
330 /**
331 * Returns the current start angle accessor, which defaults to a function returning the "startAngle" property (assumed to be a number in radians) of the source or
332 * target object returned by the source or target accessor, respectively.
333 */
334 startAngle(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
335 /**
336 * Sets the start angle to a fixed number in radians and returns this ribbon generator.
337 *
338 * @param angle A fixed numeric value for the start angle in radians.
339 */
340 startAngle(angle: number): this;
341 /**
342 * Sets the start angle accessor to the specified function and returns this ribbon generator.
343 *
344 * @param angle An accessor function which is invoked for the source and target of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
345 * receives as the first argument the source or target object returned by the respective source or target accessor function of the generator.
346 * It is also passed any additional arguments that were passed into the generator, with the exception of the first element representing the chord datum itself.
347 * The function returns the start angle in radians.
348 */
349 startAngle(angle: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
350
351 /**
352 * Returns the current end angle accessor, which defaults to a function returning the "endAngle" property (assumed to be a number in radians) of the source or
353 * target object returned by the source or target accessor, respectively.
354 */
355 endAngle(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
356 /**
357 * Sets the end angle to a fixed number in radians and returns this ribbon generator.
358 *
359 * @param angle A fixed numeric value for the end angle in radians.
360 */
361 endAngle(angle: number): this;
362 /**
363 * Sets the end angle accessor to the specified function and returns this ribbon generator.
364 *
365 * @param angle An accessor function which is invoked for the source and target of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
366 * receives as the first argument the source or target object returned by the respective source or target accessor function of the generator.
367 * It is also passed any additional arguments that were passed into the generator, with the exception of the first element representing the chord datum itself.
368 * The function returns the end angle in radians.
369 */
370 endAngle(angle: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
371
372 /**
373 * Returns the current pad angle accessor, which defaults to a function returning 0.
374 */
375 padAngle(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
376 /**
377 * Sets the pad angle to a fixed number in radians and returns this ribbon generator.
378 *
379 * @param angle A fixed numeric value for the pad angle in radians.
380 */
381 padAngle(angle: number): this;
382 /**
383 * Sets the pad angle accessor to the specified function and returns this ribbon generator.
384 *
385 * @param angle An accessor function which is invoked for the source and target of the chord. The accessor function is invoked in the same "this" context as the generator was invoked in and
386 * receives as the first argument the source or target object returned by the respective source or target accessor function of the generator.
387 * It is also passed any additional arguments that were passed into the generator, with the exception of the first element representing the chord datum itself.
388 * The function returns the pad angle in radians.
389 */
390 padAngle(angle: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
391
392 /**
393 * Returns the current rendering context, which defaults to null.
394 */
395 context(): CanvasRenderingContext2D | null;
396 /**
397 * Sets the context and returns this ribbon generator.
398 * If the context is not null, then the generated ribbon is rendered to this context as a sequence of path method calls.
399 * Otherwise, a path data string representing the generated ribbon is returned.
400 * See also d3-path.
401 */
402 context(context: CanvasRenderingContext2D | null): this;
403}
404
405export interface RibbonArrowGenerator<This, RibbonDatum, RibbonSubgroupDatum>
406 extends RibbonGenerator<This, RibbonDatum, RibbonSubgroupDatum>
407{
408 headRadius(): (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number;
409
410 headRadius(radius: number): this;
411
412 headRadius(radius: (this: This, d: RibbonSubgroupDatum, ...args: any[]) => number): this;
413}
414
415/**
416 * Creates a new ribbon generator with the default settings.
417 */
418export function ribbon(): RibbonGenerator<any, Ribbon, RibbonSubgroup>;
419/**
420 * Creates a new ribbon generator with the default settings.
421 *
422 * Accessor functions must be configured for the ribbon generator, should the datum types differ from the defaults.
423 *
424 * The first generic corresponds to the datum type representing a chord for which the ribbon is to be generated. The default type is Chord.
425 *
426 * The second generic corresponds to the datum type of the chord subgroup, i.e. source or target of the cord. The default type is ChordSubgroup.
427 */
428// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
429export function ribbon<Datum, SubgroupDatum>(): RibbonGenerator<any, Datum, SubgroupDatum>;
430/**
431 * Creates a new ribbon generator with the default settings.
432 *
433 * Accessor functions must be configured for the ribbon generator, should the datum types differ from the defaults.
434 *
435 * The first generic corresponds to the type of the "this" context within which the ribbon generator and its accessor functions will be invoked.
436 *
437 * The second generic corresponds to the datum type representing a chord for which the ribbon is to be generated. The default type is Chord.
438 *
439 * The third generic corresponds to the datum type of the chord subgroup, i.e. source or target of the cord. The default type is ChordSubgroup.
440 */
441// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
442export function ribbon<This, Datum, SubgroupDatum>(): RibbonGenerator<This, Datum, SubgroupDatum>;
443
444/**
445 * Creates a new arrow ribbon generator with the default settings.
446 */
447export function ribbonArrow(): RibbonArrowGenerator<any, Ribbon, RibbonSubgroup>;
448/**
449 * Creates a new arrow ribbon generator with the default settings.
450 *
451 * Accessor functions must be configured for the ribbon generator, should the datum types differ from the defaults.
452 *
453 * The first generic corresponds to the datum type representing a chord for which the ribbon is to be generated. The default type is Chord.
454 *
455 * The second generic corresponds to the datum type of the chord subgroup, i.e. source or target of the cord. The default type is ChordSubgroup.
456 */
457// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
458export function ribbonArrow<Datum, SubgroupDatum>(): RibbonArrowGenerator<any, Datum, SubgroupDatum>;
459/**
460 * Creates a new arrow ribbon generator with the default settings.
461 *
462 * Accessor functions must be configured for the ribbon generator, should the datum types differ from the defaults.
463 *
464 * The first generic corresponds to the type of the "this" context within which the ribbon generator and its accessor functions will be invoked.
465 *
466 * The second generic corresponds to the datum type representing a chord for which the ribbon is to be generated. The default type is Chord.
467 *
468 * The third generic corresponds to the datum type of the chord subgroup, i.e. source or target of the cord. The default type is ChordSubgroup.
469 */
470// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
471export function ribbonArrow<This, Datum, SubgroupDatum>(): RibbonArrowGenerator<This, Datum, SubgroupDatum>;