@krainovsd/graph 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.cjs +203 -138
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/module/GraphCanvas/GraphCanvas.js +197 -138
- package/lib/esm/module/GraphCanvas/GraphCanvas.js.map +1 -1
- package/lib/esm/module/GraphCanvas/constants/settings.js.map +1 -1
- package/lib/esm/module/GraphCanvas/lib/settings/link-settings-getter.js +2 -0
- package/lib/esm/module/GraphCanvas/lib/settings/link-settings-getter.js.map +1 -1
- package/lib/esm/module/GraphCanvas/lib/settings/node-settings-getter.js +4 -0
- package/lib/esm/module/GraphCanvas/lib/settings/node-settings-getter.js.map +1 -1
- package/lib/index.d.ts +12 -6
- package/package.json +1 -1
|
@@ -336,8 +336,9 @@ class GraphCanvas {
|
|
|
336
336
|
function draw() {
|
|
337
337
|
if (!this.context)
|
|
338
338
|
return;
|
|
339
|
+
const state = this.state;
|
|
339
340
|
if (this.listeners.onDraw) {
|
|
340
|
-
this.listeners.onDraw(
|
|
341
|
+
this.listeners.onDraw(state, (status) => {
|
|
341
342
|
this.highlightDrawing = status;
|
|
342
343
|
}, () => {
|
|
343
344
|
if (this.highlightedNeighbors)
|
|
@@ -353,158 +354,216 @@ class GraphCanvas {
|
|
|
353
354
|
this.context.scale(this.areaTransform.k, this.areaTransform.k);
|
|
354
355
|
/** links */
|
|
355
356
|
this.context.beginPath();
|
|
356
|
-
this.links.forEach(
|
|
357
|
+
this.links.forEach(getDrawLink(state).bind(this));
|
|
357
358
|
this.context.stroke();
|
|
358
359
|
/** nodes */
|
|
359
360
|
const textRenders = [];
|
|
360
|
-
this.nodes.forEach(
|
|
361
|
+
this.nodes.forEach(getDrawNode(textRenders, state).bind(this));
|
|
361
362
|
textRenders.forEach((render) => render());
|
|
362
363
|
this.context.restore();
|
|
363
|
-
this.listeners.onDrawFinished?.(
|
|
364
|
+
this.listeners.onDrawFinished?.(state);
|
|
364
365
|
calculateHighlightFading.bind(this)();
|
|
365
366
|
}
|
|
366
|
-
function
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
this.highlightedNode.id != link.target.id) {
|
|
381
|
-
if (linkOptions.highlightFading)
|
|
382
|
-
alpha =
|
|
383
|
-
this.graphSettings.highlightLinkFadingMin +
|
|
384
|
-
(alpha - this.graphSettings.highlightLinkFadingMin) * (1 - this.highlightProgress);
|
|
367
|
+
function getDrawLink(state) {
|
|
368
|
+
return function drawLink(link, index) {
|
|
369
|
+
if (!this.context ||
|
|
370
|
+
typeof link.source !== "object" ||
|
|
371
|
+
typeof link.target !== "object" ||
|
|
372
|
+
!link.source.x ||
|
|
373
|
+
!link.source.y ||
|
|
374
|
+
!link.target.x ||
|
|
375
|
+
!link.target.y)
|
|
376
|
+
return;
|
|
377
|
+
const linkOptions = linkIterationExtractor(link, index, this.links, state, this.linkSettings.options ?? {}, linkOptionsGetter);
|
|
378
|
+
if (linkOptions.drawLink) {
|
|
379
|
+
linkOptions.drawLink(link, linkOptions, state);
|
|
380
|
+
return;
|
|
385
381
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
y1: 0,
|
|
396
|
-
y2: 0,
|
|
397
|
-
};
|
|
398
|
-
this.context.moveTo(x1, y1);
|
|
399
|
-
this.context.lineTo(x2, y2);
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
this.context.moveTo(link.source.x, link.source.y);
|
|
403
|
-
this.context.lineTo(link.target.x, link.target.y);
|
|
404
|
-
}
|
|
405
|
-
if (this.highlightedNeighbors && this.highlightedNode)
|
|
406
|
-
this.context.stroke();
|
|
407
|
-
}
|
|
408
|
-
const drawNode = (textRenders) => function drawNode(node, index) {
|
|
409
|
-
if (!this.context || !node.x || !node.y)
|
|
410
|
-
return;
|
|
411
|
-
const nodeOptions = nodeIterationExtractor(node, index, this.nodes, this.state, this.nodeSettings.options ?? {}, nodeOptionsGetter);
|
|
412
|
-
let alpha = nodeOptions.alpha;
|
|
413
|
-
let color = nodeOptions.color;
|
|
414
|
-
let radiusInitial = nodeOptions.radius ?? this.graphSettings.nodeRadiusInitial;
|
|
415
|
-
let textAlpha = nodeOptions.textAlpha;
|
|
416
|
-
let textSize = nodeOptions.textSize;
|
|
417
|
-
let textShiftX = nodeOptions.textShiftX;
|
|
418
|
-
let textShiftY = nodeOptions.textShiftY;
|
|
419
|
-
let textWeight = nodeOptions.textWeight;
|
|
420
|
-
let textWidth = nodeOptions.textWidth;
|
|
421
|
-
if (this.highlightedNeighbors && this.highlightedNode) {
|
|
422
|
-
/** Not highlighted */
|
|
423
|
-
if (!this.highlightedNeighbors.has(node.id) && this.highlightedNode.id != node.id) {
|
|
424
|
-
if (nodeOptions.highlightFading) {
|
|
425
|
-
alpha =
|
|
426
|
-
this.graphSettings.highlightFadingMin +
|
|
427
|
-
(alpha - this.graphSettings.highlightFadingMin) * (1 - this.highlightProgress);
|
|
382
|
+
let alpha = linkOptions.alpha;
|
|
383
|
+
if (this.highlightedNeighbors && this.highlightedNode) {
|
|
384
|
+
/** Not highlighted */
|
|
385
|
+
if (this.highlightedNode.id != link.source.id &&
|
|
386
|
+
this.highlightedNode.id != link.target.id) {
|
|
387
|
+
if (linkOptions.highlightFading)
|
|
388
|
+
alpha =
|
|
389
|
+
this.graphSettings.highlightLinkFadingMin +
|
|
390
|
+
(alpha - this.graphSettings.highlightLinkFadingMin) * (1 - this.highlightProgress);
|
|
428
391
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
392
|
+
this.context.beginPath();
|
|
393
|
+
}
|
|
394
|
+
this.context.globalAlpha = alpha;
|
|
395
|
+
this.context.strokeStyle = linkOptions.color;
|
|
396
|
+
this.context.lineWidth = linkOptions.width;
|
|
397
|
+
if (linkOptions.pretty) {
|
|
398
|
+
const { x1, x2, y1, y2 } = calculateLinkPositionByRadius(link) ?? {
|
|
399
|
+
x1: 0,
|
|
400
|
+
x2: 0,
|
|
401
|
+
y1: 0,
|
|
402
|
+
y2: 0,
|
|
403
|
+
};
|
|
404
|
+
this.context.moveTo(x1, y1);
|
|
405
|
+
this.context.lineTo(x2, y2);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
this.context.moveTo(link.source.x, link.source.y);
|
|
409
|
+
this.context.lineTo(link.target.x, link.target.y);
|
|
410
|
+
}
|
|
411
|
+
if (this.highlightedNeighbors && this.highlightedNode)
|
|
412
|
+
this.context.stroke();
|
|
413
|
+
if (linkOptions.drawExtraLink) {
|
|
414
|
+
linkOptions.drawExtraLink(link, { ...linkOptions, alpha }, state);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function getDrawNode(textRenders, state) {
|
|
419
|
+
return function drawNode(node, index) {
|
|
420
|
+
if (!this.context || !node.x || !node.y)
|
|
421
|
+
return;
|
|
422
|
+
const nodeOptions = nodeIterationExtractor(node, index, this.nodes, state, this.nodeSettings.options ?? {}, nodeOptionsGetter);
|
|
423
|
+
if (nodeOptions.nodeDraw) {
|
|
424
|
+
nodeOptions.nodeDraw(node, nodeOptions, state);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
let alpha = nodeOptions.alpha;
|
|
428
|
+
let color = nodeOptions.color;
|
|
429
|
+
let radiusInitial = nodeOptions.radius ?? this.graphSettings.nodeRadiusInitial;
|
|
430
|
+
let textAlpha = nodeOptions.textAlpha;
|
|
431
|
+
let textSize = nodeOptions.textSize;
|
|
432
|
+
let textShiftX = nodeOptions.textShiftX;
|
|
433
|
+
let textShiftY = nodeOptions.textShiftY;
|
|
434
|
+
let textWeight = nodeOptions.textWeight;
|
|
435
|
+
let textWidth = nodeOptions.textWidth;
|
|
436
|
+
if (this.highlightedNeighbors && this.highlightedNode) {
|
|
437
|
+
/** Not highlighted */
|
|
438
|
+
if (!this.highlightedNeighbors.has(node.id) && this.highlightedNode.id != node.id) {
|
|
439
|
+
if (nodeOptions.highlightFading) {
|
|
440
|
+
alpha =
|
|
441
|
+
this.graphSettings.highlightFadingMin +
|
|
442
|
+
(alpha - this.graphSettings.highlightFadingMin) * (1 - this.highlightProgress);
|
|
443
|
+
}
|
|
444
|
+
if (nodeOptions.highlightTextFading) {
|
|
445
|
+
textAlpha =
|
|
446
|
+
this.graphSettings.highlightTextFadingMin +
|
|
447
|
+
(textAlpha - this.graphSettings.highlightTextFadingMin) *
|
|
448
|
+
(1 - this.highlightProgress);
|
|
449
|
+
}
|
|
450
|
+
if (nodeOptions.highlightColor) {
|
|
451
|
+
const colorRgb = extractRgb(colorToRgb(color));
|
|
452
|
+
if (colorRgb) {
|
|
453
|
+
const colorRgbFade = fadeRgb(colorRgb, this.graphSettings.highlightColorFadingMin);
|
|
454
|
+
const colorFadeAnimation = rgbAnimationByProgress(colorRgb, colorRgbFade, this.highlightProgress);
|
|
455
|
+
color = `rgb(${colorFadeAnimation.r}, ${colorFadeAnimation.g}, ${colorFadeAnimation.b})`;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
434
458
|
}
|
|
435
|
-
if (
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const
|
|
440
|
-
|
|
459
|
+
else if (!this.graphSettings.highlightOnlyRoot ||
|
|
460
|
+
(this.graphSettings.highlightOnlyRoot && this.highlightedNode.id === node.id)) {
|
|
461
|
+
/** Highlighted */
|
|
462
|
+
if (nodeOptions.highlightSizing) {
|
|
463
|
+
const radiusMax = radiusInitial + this.graphSettings.highlightSizingAdditional;
|
|
464
|
+
radiusInitial += ((radiusMax - radiusInitial) / 100) * (this.highlightProgress * 100);
|
|
465
|
+
}
|
|
466
|
+
if (nodeOptions.highlightTextSizing) {
|
|
467
|
+
const textSizeMax = textSize + this.graphSettings.highlightTextSizingAdditional;
|
|
468
|
+
const textShiftXMax = textShiftX + this.graphSettings.highlightTextShiftXAdditional;
|
|
469
|
+
const textShiftYMax = textShiftY + this.graphSettings.highlightTextShiftYAdditional;
|
|
470
|
+
const textWeightMax = textWeight + this.graphSettings.highlightTextWeightAdditional;
|
|
471
|
+
const textWidthMax = textWidth + this.graphSettings.highlightTextWidthAdditional;
|
|
472
|
+
textSize += ((textSizeMax - textSize) / 100) * (this.highlightProgress * 100);
|
|
473
|
+
textShiftX += ((textShiftXMax - textShiftX) / 100) * (this.highlightProgress * 100);
|
|
474
|
+
textShiftY += ((textShiftYMax - textShiftY) / 100) * (this.highlightProgress * 100);
|
|
475
|
+
textWeight += ((textWeightMax - textWeight) / 100) * (this.highlightProgress * 100);
|
|
476
|
+
textWidth += ((textWidthMax - textWidth) / 100) * (this.highlightProgress * 100);
|
|
441
477
|
}
|
|
442
478
|
}
|
|
443
479
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
480
|
+
const radius = nodeRadiusGetter({
|
|
481
|
+
radiusFlexible: this.graphSettings.nodeRadiusFlexible,
|
|
482
|
+
radiusInitial,
|
|
483
|
+
radiusCoefficient: this.graphSettings.nodeRadiusCoefficient,
|
|
484
|
+
radiusFactor: this.graphSettings.nodeRadiusFactor,
|
|
485
|
+
linkCount: node.linkCount,
|
|
486
|
+
});
|
|
487
|
+
node._radius = radius;
|
|
488
|
+
this.context.beginPath();
|
|
489
|
+
this.context.globalAlpha = alpha;
|
|
490
|
+
/** text */
|
|
491
|
+
if (nodeOptions.textVisible && nodeOptions.text) {
|
|
492
|
+
textRenders.push(() => {
|
|
493
|
+
if (nodeOptions.textDraw) {
|
|
494
|
+
nodeOptions.textDraw(node, {
|
|
495
|
+
...nodeOptions,
|
|
496
|
+
radius,
|
|
497
|
+
alpha,
|
|
498
|
+
color,
|
|
499
|
+
textAlpha,
|
|
500
|
+
textSize,
|
|
501
|
+
textShiftX,
|
|
502
|
+
textShiftY,
|
|
503
|
+
textWeight,
|
|
504
|
+
textWidth,
|
|
505
|
+
}, state);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
if (!this.context || !node.x || !node.y || !nodeOptions.text)
|
|
509
|
+
return;
|
|
510
|
+
this.context.beginPath();
|
|
511
|
+
this.context.globalAlpha = textAlpha;
|
|
512
|
+
drawText({
|
|
513
|
+
id: node.id,
|
|
514
|
+
cachedNodeText: this.cachedNodeText,
|
|
515
|
+
context: this.context,
|
|
516
|
+
text: nodeOptions.text,
|
|
517
|
+
textAlign: nodeOptions.textAlign,
|
|
518
|
+
textColor: nodeOptions.textColor,
|
|
519
|
+
textFont: nodeOptions.textFont,
|
|
520
|
+
textSize,
|
|
521
|
+
x: node.x + textShiftX,
|
|
522
|
+
y: node.y + radius + textShiftY,
|
|
523
|
+
maxWidth: textWidth,
|
|
524
|
+
textStyle: nodeOptions.textStyle,
|
|
525
|
+
textWeight,
|
|
526
|
+
textGap: nodeOptions.textGap,
|
|
527
|
+
});
|
|
528
|
+
if (nodeOptions.textExtraDraw) {
|
|
529
|
+
nodeOptions.textExtraDraw(node, {
|
|
530
|
+
...nodeOptions,
|
|
531
|
+
radius,
|
|
532
|
+
alpha,
|
|
533
|
+
color,
|
|
534
|
+
textAlpha,
|
|
535
|
+
textSize,
|
|
536
|
+
textShiftX,
|
|
537
|
+
textShiftY,
|
|
538
|
+
textWeight,
|
|
539
|
+
textWidth,
|
|
540
|
+
}, state);
|
|
541
|
+
}
|
|
542
|
+
});
|
|
463
543
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
if (!this.context || !node.x || !node.y || !nodeOptions.text)
|
|
479
|
-
return;
|
|
480
|
-
this.context.beginPath();
|
|
481
|
-
this.context.globalAlpha = textAlpha;
|
|
482
|
-
drawText({
|
|
483
|
-
id: node.id,
|
|
484
|
-
cachedNodeText: this.cachedNodeText,
|
|
485
|
-
context: this.context,
|
|
486
|
-
text: nodeOptions.text,
|
|
487
|
-
textAlign: nodeOptions.textAlign,
|
|
488
|
-
textColor: nodeOptions.textColor,
|
|
489
|
-
textFont: nodeOptions.textFont,
|
|
544
|
+
/** circle */
|
|
545
|
+
this.context.lineWidth = nodeOptions.borderWidth;
|
|
546
|
+
this.context.strokeStyle = nodeOptions.borderColor;
|
|
547
|
+
this.context.fillStyle = color;
|
|
548
|
+
this.context.arc(node.x, node.y, radius, 0, 2 * Math.PI);
|
|
549
|
+
this.context.fill();
|
|
550
|
+
this.context.stroke();
|
|
551
|
+
if (nodeOptions.nodeExtraDraw) {
|
|
552
|
+
nodeOptions.nodeExtraDraw(node, {
|
|
553
|
+
...nodeOptions,
|
|
554
|
+
radius,
|
|
555
|
+
alpha,
|
|
556
|
+
color,
|
|
557
|
+
textAlpha,
|
|
490
558
|
textSize,
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
maxWidth: textWidth,
|
|
494
|
-
textStyle: nodeOptions.textStyle,
|
|
559
|
+
textShiftX,
|
|
560
|
+
textShiftY,
|
|
495
561
|
textWeight,
|
|
496
|
-
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
this.context.lineWidth = nodeOptions.borderWidth;
|
|
502
|
-
this.context.strokeStyle = nodeOptions.borderColor;
|
|
503
|
-
this.context.fillStyle = color;
|
|
504
|
-
this.context.arc(node.x, node.y, radius, 0, 2 * Math.PI);
|
|
505
|
-
this.context.fill();
|
|
506
|
-
this.context.stroke();
|
|
507
|
-
};
|
|
562
|
+
textWidth,
|
|
563
|
+
}, state);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
508
567
|
return draw;
|
|
509
568
|
}
|
|
510
569
|
initResize() {
|
|
@@ -714,7 +773,7 @@ class GraphCanvas {
|
|
|
714
773
|
const [coefficientX, coefficientY] = isArray(coefficient)
|
|
715
774
|
? coefficient
|
|
716
775
|
: [coefficient, coefficient];
|
|
717
|
-
const [[minX = -this.width * coefficientX, minY = -this.height *
|
|
776
|
+
const [[minX = -this.width * coefficientX, minY = -this.height * coefficientY], [maxX = this.width * coefficientX, maxY = this.height * coefficientY],] = this.graphSettings.translateExtent;
|
|
718
777
|
zoomInstance.translateExtent([
|
|
719
778
|
[minX, minY],
|
|
720
779
|
[maxX, maxY],
|