@internetstiftelsen/charts 0.19.1 → 0.19.2
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/dist/tooltip/geometry.js +89 -0
- package/package.json +1 -1
package/dist/tooltip/geometry.js
CHANGED
|
@@ -525,8 +525,97 @@ function packHorizontalAboveBelowRow(layouts, arrowEdge, bounds) {
|
|
|
525
525
|
top: getHorizontalAboveBelowTop(layout, arrowEdge),
|
|
526
526
|
arrowEdge,
|
|
527
527
|
}));
|
|
528
|
+
if (shouldWrapHorizontalAboveBelowRow(placements, arrowEdge, bounds)) {
|
|
529
|
+
return packHorizontalAboveBelowRows(placements, arrowEdge, bounds);
|
|
530
|
+
}
|
|
528
531
|
return packHorizontalTooltipRow(placements, bounds);
|
|
529
532
|
}
|
|
533
|
+
function packHorizontalAboveBelowRows(placements, arrowEdge, bounds) {
|
|
534
|
+
const rows = getHorizontalTooltipRows(placements, bounds);
|
|
535
|
+
if (arrowEdge === 'bottom') {
|
|
536
|
+
positionHorizontalTooltipRowsAbove(rows);
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
positionHorizontalTooltipRowsBelow(rows);
|
|
540
|
+
}
|
|
541
|
+
return rows.flatMap((row) => packHorizontalTooltipRow(row, bounds));
|
|
542
|
+
}
|
|
543
|
+
function shouldWrapHorizontalAboveBelowRow(placements, arrowEdge, bounds) {
|
|
544
|
+
if (placements.length < 2) {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
const availableWidth = Math.max(0, bounds.maxRight - bounds.minLeft);
|
|
548
|
+
const totalWidth = placements.reduce((sum, placement, index) => {
|
|
549
|
+
const gap = index === 0 ? 0 : SPLIT_TOOLTIP_GAP_PX;
|
|
550
|
+
return sum + gap + placement.layout.width;
|
|
551
|
+
}, 0);
|
|
552
|
+
if (totalWidth <= availableWidth) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
return !hasRoomOnOppositeHorizontalTooltipSide(placements, arrowEdge, bounds);
|
|
556
|
+
}
|
|
557
|
+
function hasRoomOnOppositeHorizontalTooltipSide(placements, arrowEdge, bounds) {
|
|
558
|
+
const rowHeight = getHorizontalTooltipRowHeight(placements);
|
|
559
|
+
if (arrowEdge === 'top') {
|
|
560
|
+
const oppositeTop = Math.min(...placements.map((placement) => placement.layout.anchor.top)) -
|
|
561
|
+
TOOLTIP_OFFSET_PX -
|
|
562
|
+
rowHeight;
|
|
563
|
+
return oppositeTop >= bounds.minTop;
|
|
564
|
+
}
|
|
565
|
+
const oppositeBottom = Math.max(...placements.map((placement) => placement.layout.anchor.bottom)) +
|
|
566
|
+
TOOLTIP_OFFSET_PX +
|
|
567
|
+
rowHeight;
|
|
568
|
+
return oppositeBottom <= bounds.maxBottom;
|
|
569
|
+
}
|
|
570
|
+
function getHorizontalTooltipRows(placements, bounds) {
|
|
571
|
+
const availableWidth = Math.max(0, bounds.maxRight - bounds.minLeft);
|
|
572
|
+
const rows = [];
|
|
573
|
+
let currentRow = [];
|
|
574
|
+
let currentRowWidth = 0;
|
|
575
|
+
placements.forEach((placement) => {
|
|
576
|
+
const nextRowWidth = currentRow.length === 0
|
|
577
|
+
? placement.layout.width
|
|
578
|
+
: currentRowWidth +
|
|
579
|
+
SPLIT_TOOLTIP_GAP_PX +
|
|
580
|
+
placement.layout.width;
|
|
581
|
+
if (currentRow.length > 0 && nextRowWidth > availableWidth) {
|
|
582
|
+
rows.push(currentRow);
|
|
583
|
+
currentRow = [placement];
|
|
584
|
+
currentRowWidth = placement.layout.width;
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
currentRow.push(placement);
|
|
588
|
+
currentRowWidth = nextRowWidth;
|
|
589
|
+
});
|
|
590
|
+
if (currentRow.length > 0) {
|
|
591
|
+
rows.push(currentRow);
|
|
592
|
+
}
|
|
593
|
+
return rows;
|
|
594
|
+
}
|
|
595
|
+
function positionHorizontalTooltipRowsAbove(rows) {
|
|
596
|
+
let nextRowBottom = Math.min(...rows.flatMap((row) => row.map((placement) => placement.top + placement.layout.height)));
|
|
597
|
+
rows.forEach((row) => {
|
|
598
|
+
const rowHeight = getHorizontalTooltipRowHeight(row);
|
|
599
|
+
const rowTop = nextRowBottom - rowHeight;
|
|
600
|
+
row.forEach((placement) => {
|
|
601
|
+
placement.top = rowTop;
|
|
602
|
+
});
|
|
603
|
+
nextRowBottom = rowTop - SPLIT_TOOLTIP_GAP_PX;
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
function positionHorizontalTooltipRowsBelow(rows) {
|
|
607
|
+
let nextRowTop = Math.max(...rows.flatMap((row) => row.map((placement) => placement.top)));
|
|
608
|
+
rows.forEach((row) => {
|
|
609
|
+
const rowHeight = getHorizontalTooltipRowHeight(row);
|
|
610
|
+
row.forEach((placement) => {
|
|
611
|
+
placement.top = nextRowTop;
|
|
612
|
+
});
|
|
613
|
+
nextRowTop += rowHeight + SPLIT_TOOLTIP_GAP_PX;
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
function getHorizontalTooltipRowHeight(row) {
|
|
617
|
+
return Math.max(...row.map((placement) => placement.layout.height));
|
|
618
|
+
}
|
|
530
619
|
function packHorizontalTooltipRow(placements, bounds) {
|
|
531
620
|
if (placements.length === 0) {
|
|
532
621
|
return placements;
|
package/package.json
CHANGED