expressive-charts 0.4.0 → 0.6.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.
@@ -317,7 +317,7 @@ function tween(options, env = {}) {
317
317
  }
318
318
  // src/core/geometry/walk.ts
319
319
  const clamp01 = (t) => t > 0 ? t < 1 ? t : 1 : 0;
320
- const NONE = Object.freeze([]);
320
+ const NONE = /* @__PURE__ */ Object.freeze([]);
321
321
  function createWalk(segments, gaps = NONE) {
322
322
  const first = segments[0];
323
323
  const last = segments[segments.length - 1];
@@ -514,7 +514,7 @@ function checkCount(n, multiple) {
514
514
  }
515
515
  }
516
516
  // src/core/geometry/describe/mark.ts
517
- function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, labelBounds, segments }) {
517
+ function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, labelBounds, nearbyLabels, segments }) {
518
518
  return {
519
519
  key,
520
520
  ...series === void 0 ? {} : { series },
@@ -531,6 +531,7 @@ function assemble({ key, series, walk, thickness, outline, bounds, center, pivot
531
531
  center,
532
532
  pivot,
533
533
  ...labelBounds === void 0 ? {} : { labelBounds },
534
+ ...nearbyLabels === void 0 ? {} : { nearbyLabels },
534
535
  ...segments === void 0 ? {} : { segments }
535
536
  };
536
537
  }
@@ -538,15 +539,15 @@ function finite(builder, field, value) {
538
539
  if (!Number.isFinite(value)) throw new RangeError(`describe.${builder}: ${field} must be a finite number, got ${value}.`);
539
540
  return value;
540
541
  }
541
- function labelBoxes(builder, boxes) {
542
+ function labelBoxes(builder, boxes, field = "labelBounds") {
542
543
  if (!boxes) return boxes;
543
544
  for (let i = 0; i < boxes.length; i++) {
544
545
  const { x, y, width, height } = boxes[i];
545
546
  if (Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(width) && width >= 0 && Number.isFinite(height) && height >= 0) continue;
546
- finite(builder, `labelBounds[${i}].x`, x);
547
- finite(builder, `labelBounds[${i}].y`, y);
548
- size(builder, `labelBounds[${i}].width`, width);
549
- size(builder, `labelBounds[${i}].height`, height);
547
+ finite(builder, `${field}[${i}].x`, x);
548
+ finite(builder, `${field}[${i}].y`, y);
549
+ size(builder, `${field}[${i}].width`, width);
550
+ size(builder, `${field}[${i}].height`, height);
550
551
  }
551
552
  return boxes;
552
553
  }
@@ -585,7 +586,8 @@ function describeArc(spec) {
585
586
  bounds: boundsOf(sides.flat().flatMap(extremes)),
586
587
  center: walk.point(.5),
587
588
  pivot: center,
588
- labelBounds: labelBoxes("arc", spec.labelBounds)
589
+ labelBounds: labelBoxes("arc", spec.labelBounds),
590
+ nearbyLabels: labelBoxes("arc", spec.nearbyLabels, "nearbyLabels")
589
591
  });
590
592
  }
591
593
  // src/core/geometry/describe/bar.ts
@@ -605,6 +607,7 @@ function describeBar(spec) {
605
607
  const radius = size("bar", "radius", spec.radius ?? 0);
606
608
  const gap = size("bar", "gap", spec.gap ?? 0);
607
609
  const labelBounds = labelBoxes("bar", spec.labelBounds);
610
+ const nearbyLabels = labelBoxes("bar", spec.nearbyLabels, "nearbyLabels");
608
611
  const horizontal = baseline === "left" || baseline === "right";
609
612
  const length = horizontal ? width : height;
610
613
  const thickness = horizontal ? height : width;
@@ -669,6 +672,7 @@ function describeBar(spec) {
669
672
  height
670
673
  },
671
674
  labelBounds,
675
+ nearbyLabels,
672
676
  segments
673
677
  });
674
678
  }
@@ -723,7 +727,8 @@ function describeLine(spec) {
723
727
  },
724
728
  center: walk.point(.5),
725
729
  pivot: first,
726
- labelBounds: labelBoxes("line", spec.labelBounds)
730
+ labelBounds: labelBoxes("line", spec.labelBounds),
731
+ nearbyLabels: labelBoxes("line", spec.nearbyLabels, "nearbyLabels")
727
732
  });
728
733
  }
729
734
  function gapsAt(breaks, segments) {
@@ -771,7 +776,8 @@ function describePoint(spec) {
771
776
  },
772
777
  center,
773
778
  pivot: center,
774
- labelBounds: labelBoxes("point", spec.labelBounds)
779
+ labelBounds: labelBoxes("point", spec.labelBounds),
780
+ nearbyLabels: labelBoxes("point", spec.nearbyLabels, "nearbyLabels")
775
781
  });
776
782
  }
777
783
  // src/core/geometry/flatten.ts
@@ -848,9 +854,8 @@ function flattenPath(commands) {
848
854
  return subpaths;
849
855
  }
850
856
  function bezier(...p) {
851
- const bend = (a, b, c) => Math.hypot(a.x - 2 * b.x + c.x, a.y - 2 * b.y + c.y);
852
857
  const cubic = p.length === 4;
853
- const n = pieces(1, cubic ? 6 * Math.max(bend(p[0], p[1], p[2]), bend(p[1], p[2], p[3])) : 2 * bend(p[0], p[1], p[2]));
858
+ const n = cubic ? cubicPieces(p[0], p[1], p[2], p[3]) : pieces(1, 2 * bend(p[0], p[1], p[2]));
854
859
  const points = [p[0]];
855
860
  for (let k = 1; k < n; k++) {
856
861
  const t = k / n;
@@ -874,6 +879,12 @@ function bezier(...p) {
874
879
  points.push(p[p.length - 1]);
875
880
  return points;
876
881
  }
882
+ function cubicPieces(p0, p1, p2, p3) {
883
+ return pieces(1, 6 * Math.max(bend(p0, p1, p2), bend(p1, p2, p3)));
884
+ }
885
+ function bend(a, b, c) {
886
+ return Math.hypot(a.x - 2 * b.x + c.x, a.y - 2 * b.y + c.y);
887
+ }
877
888
  function pieces(span, second) {
878
889
  const n = Math.ceil(span * Math.sqrt(second / (8 * FLAT)));
879
890
  return Number.isFinite(n) ? Math.min(MAX_PIECES, Math.max(1, n)) : MAX_PIECES;
@@ -1322,6 +1333,7 @@ function describeShape(spec) {
1322
1333
  length
1323
1334
  ].every(Number.isFinite)) throw new RangeError(`describe.shape: d must stay finite${m ? " once moved by matrix" : ""}, got ${show(d)}.`);
1324
1335
  const labelBounds = labelBoxes("shape", spec.labelBounds);
1336
+ const nearbyLabels = labelBoxes("shape", spec.nearbyLabels, "nearbyLabels");
1325
1337
  if (subpaths.every(closes) && length > 0) {
1326
1338
  const center = {
1327
1339
  x: box.x + box.width / 2,
@@ -1336,7 +1348,8 @@ function describeShape(spec) {
1336
1348
  bounds: box,
1337
1349
  center,
1338
1350
  pivot: center,
1339
- labelBounds
1351
+ labelBounds,
1352
+ nearbyLabels
1340
1353
  });
1341
1354
  }
1342
1355
  const { segments, gaps } = joined(subpaths);
@@ -1356,7 +1369,8 @@ function describeShape(spec) {
1356
1369
  },
1357
1370
  center: walk.point(.5),
1358
1371
  pivot: walk.start,
1359
- labelBounds
1372
+ labelBounds,
1373
+ nearbyLabels
1360
1374
  });
1361
1375
  }
1362
1376
  const closes = ({ closed, start, end }) => closed || start.x === end.x && start.y === end.y;
@@ -1464,6 +1478,22 @@ function mergeFields(current, next) {
1464
1478
  else result[field] = value;
1465
1479
  return result;
1466
1480
  }
1481
+ // src/core/text-box.ts
1482
+ const TEXT_SIZE = 12.5;
1483
+ const AVERAGE_WIDTH = .6;
1484
+ const ASCENT = 1.05;
1485
+ const SIDE = .16;
1486
+ function boxAround(at, anchor, width, size) {
1487
+ return {
1488
+ x: (anchor === "end" ? at.x - width : anchor === "middle" ? at.x - width / 2 : at.x) - SIDE * size,
1489
+ y: at.y - ASCENT * size,
1490
+ width: width + 2 * SIDE * size,
1491
+ height: 1.3 * size
1492
+ };
1493
+ }
1494
+ function estimatedBox(content, style) {
1495
+ return boxAround(style.at, style.anchor, content.length * TEXT_SIZE * AVERAGE_WIDTH, TEXT_SIZE);
1496
+ }
1467
1497
  // src/core/runner.ts
1468
1498
  function runScope(options) {
1469
1499
  const { clock, motion, layer, mark } = options;
@@ -1478,6 +1508,7 @@ function runScope(options) {
1478
1508
  icons: options.icons,
1479
1509
  batch: options.batch,
1480
1510
  ...options.previous ? { previous: options.previous } : {},
1511
+ ...options.texts ? { texts: options.texts } : {},
1481
1512
  tween(tweenOptions) {
1482
1513
  const own = tweenOptions.signal;
1483
1514
  const running = tween({
@@ -1518,6 +1549,8 @@ const SLOTS = [
1518
1549
  "exit",
1519
1550
  "hover"
1520
1551
  ];
1552
+ const byOrder = (a, b) => a.order - b.order;
1553
+ const sameBox = (a, b) => a === b || a !== void 0 && b !== void 0 && a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
1521
1554
  const NO_ICONS$1 = { get: () => void 0 };
1522
1555
  const INERT$1 = {
1523
1556
  place() {},
@@ -1535,6 +1568,10 @@ function createRunner(options) {
1535
1568
  let transitioning = false;
1536
1569
  let destroyed = false;
1537
1570
  let order = 0;
1571
+ const box = (content, style) => surface.box ? surface.box(content, style) : estimatedBox(content, style);
1572
+ const texts = /* @__PURE__ */ new Map();
1573
+ let holds = 0;
1574
+ let flushing = false;
1538
1575
  const unsubscribe = motion$2.subscribe(() => {
1539
1576
  if (motion$2.mode === "off") endRunning();
1540
1577
  });
@@ -1586,7 +1623,14 @@ function createRunner(options) {
1586
1623
  batch,
1587
1624
  previous,
1588
1625
  layer: runLayer(entry.key, slot, () => run.order, () => run.state !== "cleared"),
1589
- mark: runMark(() => run)
1626
+ mark: runMark(() => run),
1627
+ texts: {
1628
+ box,
1629
+ held: () => [...texts].flatMap(([other, held]) => held.box && other.order < run.order ? [held.box] : []),
1630
+ hold(next) {
1631
+ if (run.state !== "cleared") hold(run, next);
1632
+ }
1633
+ }
1590
1634
  })
1591
1635
  };
1592
1636
  entry.runs.set(slot, run);
@@ -1633,8 +1677,44 @@ function createRunner(options) {
1633
1677
  report(error);
1634
1678
  }
1635
1679
  run.scope.clear();
1680
+ hold(run, void 0);
1636
1681
  release(run);
1637
1682
  }
1683
+ function tell(run, mark) {
1684
+ try {
1685
+ run.scope.change(mark, latestData(run.entry, run));
1686
+ } catch (error) {
1687
+ report(error);
1688
+ clearRun(run);
1689
+ }
1690
+ }
1691
+ function hold(run, next) {
1692
+ let held = texts.get(run);
1693
+ if (!held) {
1694
+ if (!next) return;
1695
+ texts.set(run, held = {
1696
+ box: void 0,
1697
+ placed: 0,
1698
+ moved: 0
1699
+ });
1700
+ }
1701
+ held.placed = ++holds;
1702
+ if (sameBox(held.box, next)) return;
1703
+ held.box = next;
1704
+ held.moved = holds;
1705
+ if (flushing) return;
1706
+ flushing = true;
1707
+ queueMicrotask(flush);
1708
+ }
1709
+ function flush() {
1710
+ let moved = 0;
1711
+ for (const [run, held] of [...texts].sort(([a], [b]) => byOrder(a, b))) {
1712
+ if (held.box && held.placed < moved) tell(run, run.entry.mark);
1713
+ if (!held.box) texts.delete(run);
1714
+ moved = Math.max(moved, held.moved);
1715
+ }
1716
+ flushing = false;
1717
+ }
1638
1718
  function runLayer(key, slot, order, live) {
1639
1719
  let group;
1640
1720
  const target = () => live() ? group ?? (group = surface.group(key, slot, order())) : void 0;
@@ -1813,15 +1893,7 @@ function createRunner(options) {
1813
1893
  entry.data = data;
1814
1894
  if (!transitioning) entry.resting = data;
1815
1895
  }
1816
- for (const run of [...entry.runs.values()]) {
1817
- if (run.state === "cleared") continue;
1818
- try {
1819
- run.scope.change(mark, latestData(entry, run));
1820
- } catch (error) {
1821
- report(error);
1822
- clearRun(run);
1823
- }
1824
- }
1896
+ for (const run of [...entry.runs.values()].sort(byOrder)) if (run.state !== "cleared") tell(run, mark);
1825
1897
  },
1826
1898
  interrupt() {
1827
1899
  if (destroyed) return;
@@ -2048,6 +2120,6 @@ function describeEffect(value) {
2048
2120
  if (kind === "transition") return "a transition effect without run(), end() and clear()";
2049
2121
  return kind === void 0 ? "an object with no kind" : `an effect of kind ${JSON.stringify(kind)}`;
2050
2122
  }
2051
- export { linear as A, createMotion as C, scaleDuration as D, motion as E, report as F, createClock as M, defaultClock as N, easeInOutCubic as O, manualClock as P, TIMINGS as S, mediaReducedMotion as T, describePoint as _, mergeFields as a, describeArc as b, describeValue as c, describe as d, describeShape as f, transformPath as g, shapeOutline as h, runScope as i, unease as j, easeOutCubic as k, isMarkEffect as l, parsePath as m, playTransition as n, EFFECT_SLOTS as o, formatPath as p, createRunner as r, createRegistry as s, isTransitionEffect as t, resolveEffects as u, describeLine as v, defaultScaling as w, tween as x, describeBar as y };
2123
+ export { TIMINGS as A, createClock as B, flattenPath as C, describeArc as D, describeBar as E, scaleDuration as F, manualClock as H, easeInOutCubic as I, easeOutCubic as L, defaultScaling as M, mediaReducedMotion as N, loopOutline as O, motion as P, linear as R, cubicPieces as S, describeLine as T, report as U, defaultClock as V, formatPath as _, AVERAGE_WIDTH as a, transformPath as b, estimatedBox as c, createRegistry as d, describeValue as f, describeShape as g, describe as h, runScope as i, createMotion as j, tween as k, mergeFields as l, resolveEffects as m, playTransition as n, TEXT_SIZE as o, isMarkEffect as p, createRunner as r, boxAround as s, isTransitionEffect as t, EFFECT_SLOTS as u, parsePath as v, describePoint as w, FLAT as x, shapeOutline as y, unease as z };
2052
2124
 
2053
- //# sourceMappingURL=transition-DSRt2DH3.js.map
2125
+ //# sourceMappingURL=transition-romki_A6.js.map