@internetstiftelsen/charts 0.19.1 → 0.19.3

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.
@@ -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;
@@ -636,7 +725,7 @@ function getHorizontalAboveBelowPlacementCost(candidate, layout, preferredEdge,
636
725
  return (connectorPenalty +
637
726
  edgePenalty +
638
727
  xDistance +
639
- yDistance * 0.5 +
728
+ yDistance * 4 +
640
729
  overflowPenalty);
641
730
  }
642
731
  function getPreferredAndOppositeEdges(preferredEdge) {
@@ -757,8 +757,9 @@ function getTooltipPlacementBounds(svgNode) {
757
757
  let bounds = getSplitTooltipViewportBounds();
758
758
  let currentElement = svgNode.parentElement;
759
759
  while (currentElement) {
760
- if (isScrollableElement(currentElement)) {
761
- bounds = intersectTooltipBounds(bounds, getElementTooltipBounds(currentElement));
760
+ const scrollAxes = getScrollableElementAxes(currentElement);
761
+ if (scrollAxes.horizontal || scrollAxes.vertical) {
762
+ bounds = intersectTooltipBounds(bounds, getElementTooltipBounds(currentElement), scrollAxes);
762
763
  }
763
764
  currentElement = currentElement.parentElement;
764
765
  }
@@ -775,15 +776,26 @@ function getElementTooltipBounds(element) {
775
776
  maxBottom: top + rect.height,
776
777
  };
777
778
  }
778
- function intersectTooltipBounds(bounds, nextBounds) {
779
+ function intersectTooltipBounds(bounds, nextBounds, axes = {
780
+ horizontal: true,
781
+ vertical: true,
782
+ }) {
779
783
  if (nextBounds.maxRight <= nextBounds.minLeft ||
780
784
  nextBounds.maxBottom <= nextBounds.minTop) {
781
785
  return bounds;
782
786
  }
783
- const minLeft = Math.max(bounds.minLeft, nextBounds.minLeft);
784
- const maxRight = Math.min(bounds.maxRight, nextBounds.maxRight);
785
- const minTop = Math.max(bounds.minTop, nextBounds.minTop);
786
- const maxBottom = Math.min(bounds.maxBottom, nextBounds.maxBottom);
787
+ const minLeft = axes.horizontal
788
+ ? Math.max(bounds.minLeft, nextBounds.minLeft)
789
+ : bounds.minLeft;
790
+ const maxRight = axes.horizontal
791
+ ? Math.min(bounds.maxRight, nextBounds.maxRight)
792
+ : bounds.maxRight;
793
+ const minTop = axes.vertical
794
+ ? Math.max(bounds.minTop, nextBounds.minTop)
795
+ : bounds.minTop;
796
+ const maxBottom = axes.vertical
797
+ ? Math.min(bounds.maxBottom, nextBounds.maxBottom)
798
+ : bounds.maxBottom;
787
799
  if (maxRight <= minLeft || maxBottom <= minTop) {
788
800
  return bounds;
789
801
  }
@@ -820,6 +832,26 @@ function getTooltipScrollTargets(svgNode) {
820
832
  return [...scrollTargets];
821
833
  }
822
834
  function isScrollableElement(element) {
835
+ const scrollAxes = getScrollableElementAxes(element);
836
+ return scrollAxes.horizontal || scrollAxes.vertical;
837
+ }
838
+ function getScrollableElementAxes(element) {
823
839
  const style = window.getComputedStyle(element);
824
- return [style.overflow, style.overflowX, style.overflowY].some((value) => value === 'auto' || value === 'scroll' || value === 'overlay');
840
+ return {
841
+ horizontal: isScrollableOverflowValue(style.overflowX) &&
842
+ hasScrollableOverflow(element, 'horizontal'),
843
+ vertical: isScrollableOverflowValue(style.overflowY) &&
844
+ hasScrollableOverflow(element, 'vertical'),
845
+ };
846
+ }
847
+ function isScrollableOverflowValue(value) {
848
+ return value === 'auto' || value === 'scroll' || value === 'overlay';
849
+ }
850
+ function hasScrollableOverflow(element, axis) {
851
+ const scrollSize = axis === 'horizontal' ? element.scrollWidth : element.scrollHeight;
852
+ const clientSize = axis === 'horizontal' ? element.clientWidth : element.clientHeight;
853
+ if (scrollSize === 0 && clientSize === 0) {
854
+ return true;
855
+ }
856
+ return scrollSize > clientSize;
825
857
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.19.1",
2
+ "version": "0.19.3",
3
3
  "name": "@internetstiftelsen/charts",
4
4
  "type": "module",
5
5
  "sideEffects": false,