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.
package/dist/testing.cjs CHANGED
@@ -258,6 +258,22 @@ function mergeFields(current, next) {
258
258
  else result[field] = value;
259
259
  return result;
260
260
  }
261
+ // src/core/text-box.ts
262
+ const TEXT_SIZE = 12.5;
263
+ const AVERAGE_WIDTH = .6;
264
+ const ASCENT = 1.05;
265
+ const SIDE = .16;
266
+ function boxAround(at, anchor, width, size) {
267
+ return {
268
+ x: (anchor === "end" ? at.x - width : anchor === "middle" ? at.x - width / 2 : at.x) - SIDE * size,
269
+ y: at.y - ASCENT * size,
270
+ width: width + 2 * SIDE * size,
271
+ height: 1.3 * size
272
+ };
273
+ }
274
+ function estimatedBox(content, style) {
275
+ return boxAround(style.at, style.anchor, content.length * TEXT_SIZE * AVERAGE_WIDTH, TEXT_SIZE);
276
+ }
261
277
  // src/core/easing.ts
262
278
  const easeInOutCubic = (t) => t < .5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
263
279
  // src/core/tween.ts
@@ -345,6 +361,7 @@ function runScope(options) {
345
361
  icons: options.icons,
346
362
  batch: options.batch,
347
363
  ...options.previous ? { previous: options.previous } : {},
364
+ ...options.texts ? { texts: options.texts } : {},
348
365
  tween(tweenOptions) {
349
366
  const own = tweenOptions.signal;
350
367
  const running = tween({
@@ -584,6 +601,18 @@ function testContext(options = {}) {
584
601
  const layer = recordingLayer();
585
602
  const mark = recordingMark();
586
603
  const icons = new Map((options.icons ?? []).map((icon) => [icon.name, icon]));
604
+ const held = [...options.held ?? []];
605
+ let holding;
606
+ const texts = {
607
+ box: estimatedBox,
608
+ held: () => held,
609
+ hold(box) {
610
+ holding = box;
611
+ },
612
+ get holding() {
613
+ return holding;
614
+ }
615
+ };
587
616
  const scope = runScope({
588
617
  clock,
589
618
  motion,
@@ -594,7 +623,8 @@ function testContext(options = {}) {
594
623
  index: 0,
595
624
  count: 1
596
625
  },
597
- previous: options.previous
626
+ previous: options.previous,
627
+ texts
598
628
  });
599
629
  return {
600
630
  ...scope.context,
@@ -602,8 +632,12 @@ function testContext(options = {}) {
602
632
  clock,
603
633
  layer,
604
634
  mark,
635
+ texts,
605
636
  abort: scope.abort,
606
- clear: scope.clear,
637
+ clear() {
638
+ scope.clear();
639
+ holding = void 0;
640
+ },
607
641
  change: scope.change
608
642
  };
609
643
  }
@@ -754,7 +788,7 @@ function pathData(segments, closed = false) {
754
788
  }
755
789
  // src/core/geometry/walk.ts
756
790
  const clamp01 = (t) => t > 0 ? t < 1 ? t : 1 : 0;
757
- const NONE = Object.freeze([]);
791
+ const NONE = /* @__PURE__ */ Object.freeze([]);
758
792
  function createWalk(segments, gaps = NONE) {
759
793
  const first = segments[0];
760
794
  const last = segments[segments.length - 1];
@@ -951,7 +985,7 @@ function checkCount(n, multiple) {
951
985
  }
952
986
  }
953
987
  // src/core/geometry/describe/mark.ts
954
- function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, labelBounds, segments }) {
988
+ function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, labelBounds, nearbyLabels, segments }) {
955
989
  return {
956
990
  key,
957
991
  ...series === void 0 ? {} : { series },
@@ -968,6 +1002,7 @@ function assemble({ key, series, walk, thickness, outline, bounds, center, pivot
968
1002
  center,
969
1003
  pivot,
970
1004
  ...labelBounds === void 0 ? {} : { labelBounds },
1005
+ ...nearbyLabels === void 0 ? {} : { nearbyLabels },
971
1006
  ...segments === void 0 ? {} : { segments }
972
1007
  };
973
1008
  }
@@ -975,15 +1010,15 @@ function finite(builder, field, value) {
975
1010
  if (!Number.isFinite(value)) throw new RangeError(`describe.${builder}: ${field} must be a finite number, got ${value}.`);
976
1011
  return value;
977
1012
  }
978
- function labelBoxes(builder, boxes) {
1013
+ function labelBoxes(builder, boxes, field = "labelBounds") {
979
1014
  if (!boxes) return boxes;
980
1015
  for (let i = 0; i < boxes.length; i++) {
981
1016
  const { x, y, width, height } = boxes[i];
982
1017
  if (Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(width) && width >= 0 && Number.isFinite(height) && height >= 0) continue;
983
- finite(builder, `labelBounds[${i}].x`, x);
984
- finite(builder, `labelBounds[${i}].y`, y);
985
- size(builder, `labelBounds[${i}].width`, width);
986
- size(builder, `labelBounds[${i}].height`, height);
1018
+ finite(builder, `${field}[${i}].x`, x);
1019
+ finite(builder, `${field}[${i}].y`, y);
1020
+ size(builder, `${field}[${i}].width`, width);
1021
+ size(builder, `${field}[${i}].height`, height);
987
1022
  }
988
1023
  return boxes;
989
1024
  }
@@ -1022,7 +1057,8 @@ function describeArc(spec) {
1022
1057
  bounds: boundsOf(sides.flat().flatMap(extremes)),
1023
1058
  center: walk.point(.5),
1024
1059
  pivot: center,
1025
- labelBounds: labelBoxes("arc", spec.labelBounds)
1060
+ labelBounds: labelBoxes("arc", spec.labelBounds),
1061
+ nearbyLabels: labelBoxes("arc", spec.nearbyLabels, "nearbyLabels")
1026
1062
  });
1027
1063
  }
1028
1064
  // src/core/geometry/describe/bar.ts
@@ -1042,6 +1078,7 @@ function describeBar(spec) {
1042
1078
  const radius = size("bar", "radius", spec.radius ?? 0);
1043
1079
  const gap = size("bar", "gap", spec.gap ?? 0);
1044
1080
  const labelBounds = labelBoxes("bar", spec.labelBounds);
1081
+ const nearbyLabels = labelBoxes("bar", spec.nearbyLabels, "nearbyLabels");
1045
1082
  const horizontal = baseline === "left" || baseline === "right";
1046
1083
  const length = horizontal ? width : height;
1047
1084
  const thickness = horizontal ? height : width;
@@ -1106,6 +1143,7 @@ function describeBar(spec) {
1106
1143
  height
1107
1144
  },
1108
1145
  labelBounds,
1146
+ nearbyLabels,
1109
1147
  segments
1110
1148
  });
1111
1149
  }
@@ -1160,7 +1198,8 @@ function describeLine(spec) {
1160
1198
  },
1161
1199
  center: walk.point(.5),
1162
1200
  pivot: first,
1163
- labelBounds: labelBoxes("line", spec.labelBounds)
1201
+ labelBounds: labelBoxes("line", spec.labelBounds),
1202
+ nearbyLabels: labelBoxes("line", spec.nearbyLabels, "nearbyLabels")
1164
1203
  });
1165
1204
  }
1166
1205
  function gapsAt(breaks, segments) {
@@ -1208,7 +1247,8 @@ function describePoint(spec) {
1208
1247
  },
1209
1248
  center,
1210
1249
  pivot: center,
1211
- labelBounds: labelBoxes("point", spec.labelBounds)
1250
+ labelBounds: labelBoxes("point", spec.labelBounds),
1251
+ nearbyLabels: labelBoxes("point", spec.nearbyLabels, "nearbyLabels")
1212
1252
  });
1213
1253
  }
1214
1254
  // src/core/geometry/flatten.ts
@@ -1285,9 +1325,8 @@ function flattenPath(commands) {
1285
1325
  return subpaths;
1286
1326
  }
1287
1327
  function bezier(...p) {
1288
- const bend = (a, b, c) => Math.hypot(a.x - 2 * b.x + c.x, a.y - 2 * b.y + c.y);
1289
1328
  const cubic = p.length === 4;
1290
- 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]));
1329
+ const n = cubic ? cubicPieces(p[0], p[1], p[2], p[3]) : pieces(1, 2 * bend(p[0], p[1], p[2]));
1291
1330
  const points = [p[0]];
1292
1331
  for (let k = 1; k < n; k++) {
1293
1332
  const t = k / n;
@@ -1311,6 +1350,12 @@ function bezier(...p) {
1311
1350
  points.push(p[p.length - 1]);
1312
1351
  return points;
1313
1352
  }
1353
+ function cubicPieces(p0, p1, p2, p3) {
1354
+ return pieces(1, 6 * Math.max(bend(p0, p1, p2), bend(p1, p2, p3)));
1355
+ }
1356
+ function bend(a, b, c) {
1357
+ return Math.hypot(a.x - 2 * b.x + c.x, a.y - 2 * b.y + c.y);
1358
+ }
1314
1359
  function pieces(span, second) {
1315
1360
  const n = Math.ceil(span * Math.sqrt(second / (8 * FLAT)));
1316
1361
  return Number.isFinite(n) ? Math.min(MAX_PIECES, Math.max(1, n)) : MAX_PIECES;
@@ -1597,6 +1642,7 @@ function describeShape(spec) {
1597
1642
  length
1598
1643
  ].every(Number.isFinite)) throw new RangeError(`describe.shape: d must stay finite${m ? " once moved by matrix" : ""}, got ${show$1(d)}.`);
1599
1644
  const labelBounds = labelBoxes("shape", spec.labelBounds);
1645
+ const nearbyLabels = labelBoxes("shape", spec.nearbyLabels, "nearbyLabels");
1600
1646
  if (subpaths.every(closes) && length > 0) {
1601
1647
  const center = {
1602
1648
  x: box.x + box.width / 2,
@@ -1611,7 +1657,8 @@ function describeShape(spec) {
1611
1657
  bounds: box,
1612
1658
  center,
1613
1659
  pivot: center,
1614
- labelBounds
1660
+ labelBounds,
1661
+ nearbyLabels
1615
1662
  });
1616
1663
  }
1617
1664
  const { segments, gaps } = joined(subpaths);
@@ -1631,7 +1678,8 @@ function describeShape(spec) {
1631
1678
  },
1632
1679
  center: walk.point(.5),
1633
1680
  pivot: walk.start,
1634
- labelBounds
1681
+ labelBounds,
1682
+ nearbyLabels
1635
1683
  });
1636
1684
  }
1637
1685
  const closes = ({ closed, start, end }) => closed || start.x === end.x && start.y === end.y;