expressive-charts 0.1.0 → 0.3.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +14 -13
  3. package/dist/{chunk-N5IF2RO2.js → chunk-7A7LXXT4.js} +6 -2
  4. package/dist/chunk-7A7LXXT4.js.map +1 -0
  5. package/dist/{chunk-ZIGBM4NY.js → chunk-CJUQHEX6.js} +92 -28
  6. package/dist/chunk-CJUQHEX6.js.map +1 -0
  7. package/dist/{chunk-CVXP2L2J.js → chunk-N2BLUETM.js} +17 -5
  8. package/dist/chunk-N2BLUETM.js.map +1 -0
  9. package/dist/{drawing-Du6DAgrW.d.cts → drawing-CdTgixMY.d.cts} +20 -2
  10. package/dist/{drawing-Du6DAgrW.d.ts → drawing-CdTgixMY.d.ts} +20 -2
  11. package/dist/{effect-dPmXiWlL.d.cts → effect-Bu3MHHeg.d.ts} +4 -2
  12. package/dist/{effect-OcyvtfZi.d.ts → effect-D4vmdYP4.d.cts} +4 -2
  13. package/dist/expressive-charts.css +11 -6
  14. package/dist/expressive-charts.css.d.cts +2 -0
  15. package/dist/expressive-charts.css.d.ts +2 -0
  16. package/dist/expressive-charts.global.js +29 -24
  17. package/dist/expressive-charts.global.js.map +1 -1
  18. package/dist/icons/sport.cjs +5 -1
  19. package/dist/icons/sport.cjs.map +1 -1
  20. package/dist/icons/sport.js +2 -2
  21. package/dist/index.cjs +1610 -669
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +143 -18
  24. package/dist/index.d.ts +143 -18
  25. package/dist/index.js +1448 -591
  26. package/dist/index.js.map +1 -1
  27. package/dist/testing.cjs +93 -42
  28. package/dist/testing.cjs.map +1 -1
  29. package/dist/testing.d.cts +2 -2
  30. package/dist/testing.d.ts +2 -2
  31. package/dist/testing.js +6 -8
  32. package/dist/testing.js.map +1 -1
  33. package/package.json +16 -4
  34. package/dist/chunk-CVXP2L2J.js.map +0 -1
  35. package/dist/chunk-N5IF2RO2.js.map +0 -1
  36. package/dist/chunk-ZIGBM4NY.js.map +0 -1
package/dist/testing.cjs CHANGED
@@ -680,7 +680,8 @@ function pathData(segments, closed = false) {
680
680
 
681
681
  // src/core/geometry/walk.ts
682
682
  var clamp01 = (t) => t > 0 ? t < 1 ? t : 1 : 0;
683
- function createWalk(segments) {
683
+ var NONE = Object.freeze([]);
684
+ function createWalk(segments, gaps = NONE) {
684
685
  const first = segments[0];
685
686
  const last = segments[segments.length - 1];
686
687
  if (!first || !last) throw new RangeError("A walk needs at least one segment.");
@@ -712,40 +713,54 @@ function createWalk(segments) {
712
713
  const { index, u } = locate(at * length);
713
714
  return pointOn(segments[index], u);
714
715
  };
716
+ const draw = (from, to) => {
717
+ let d = moveTo(point(from));
718
+ const a = from * length;
719
+ const b = to * length;
720
+ if (!(b > a)) return d;
721
+ const first2 = locate(a);
722
+ for (let i = first2.index; i < segments.length && cumulative[i] < b; i++) {
723
+ const segment = segments[i];
724
+ if (!(segment.length > 0)) continue;
725
+ const u0 = i === first2.index ? first2.u : 0;
726
+ const u1 = cumulative[i + 1] > b ? Math.min(1, (b - cumulative[i]) / segment.length) : 1;
727
+ if (u1 > u0) d += drawTo(segment, u0, u1);
728
+ }
729
+ return d;
730
+ };
731
+ const slice = (t0, t1) => {
732
+ const from = clamp01(Math.min(t0, t1));
733
+ const to = clamp01(Math.max(t0, t1));
734
+ if (gaps.length === 0) return draw(from, to);
735
+ let d = "";
736
+ let at = from;
737
+ for (const [a, b] of gaps) {
738
+ if (a >= to) break;
739
+ if (a > at) d += draw(at, a);
740
+ if (b > at) at = b;
741
+ }
742
+ if (to > at) d += draw(at, to);
743
+ return d === "" ? moveTo(point(from)) : d;
744
+ };
715
745
  return {
716
746
  segments,
717
747
  length,
718
748
  start,
719
749
  end,
750
+ gaps,
720
751
  point,
721
752
  angle(t) {
722
753
  const { index, u } = locate(clamp01(t) * length);
723
754
  return angleOn(segments[index], u);
724
755
  },
725
- slice(t0, t1) {
726
- const from = clamp01(Math.min(t0, t1));
727
- const to = clamp01(Math.max(t0, t1));
728
- let d = moveTo(point(from));
729
- const a = from * length;
730
- const b = to * length;
731
- if (!(b > a)) return d;
732
- const first2 = locate(a);
733
- for (let i = first2.index; i < segments.length && cumulative[i] < b; i++) {
734
- const segment = segments[i];
735
- if (!(segment.length > 0)) continue;
736
- const u0 = i === first2.index ? first2.u : 0;
737
- const u1 = cumulative[i + 1] > b ? Math.min(1, (b - cumulative[i]) / segment.length) : 1;
738
- if (u1 > u0) d += drawTo(segment, u0, u1);
739
- }
740
- return d;
741
- },
742
- d: pathData(segments)
756
+ slice,
757
+ d: gaps.length === 0 ? pathData(segments) : slice(0, 1)
743
758
  };
744
759
  }
745
760
 
746
761
  // src/core/geometry/outline.ts
747
762
  function closedOutline(sides) {
748
- const walks = sides.map(createWalk);
763
+ const walks = sides.map((side) => createWalk(side));
749
764
  return {
750
765
  d: pathData(sides.flat(), true),
751
766
  closed: true,
@@ -781,7 +796,7 @@ function checkCount(n, multiple) {
781
796
  }
782
797
 
783
798
  // src/core/geometry/describe/mark.ts
784
- function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, segments }) {
799
+ function assemble({ key, series, walk, thickness, outline, bounds, center, pivot, labelBounds, segments }) {
785
800
  return {
786
801
  key,
787
802
  ...series === void 0 ? {} : { series },
@@ -791,11 +806,13 @@ function assemble({ key, series, walk, thickness, outline, bounds, center, pivot
791
806
  path: (t) => walk.point(t),
792
807
  angle: (t) => walk.angle(t),
793
808
  slice: (t0, t1) => walk.slice(t0, t1),
809
+ ...walk.gaps.length === 0 ? {} : { gaps: walk.gaps },
794
810
  thickness,
795
811
  outline,
796
812
  bounds,
797
813
  center,
798
814
  pivot,
815
+ ...labelBounds === void 0 ? {} : { labelBounds },
799
816
  ...segments === void 0 ? {} : { segments }
800
817
  };
801
818
  }
@@ -803,6 +820,18 @@ function finite(builder, field, value) {
803
820
  if (!Number.isFinite(value)) throw new RangeError(`describe.${builder}: ${field} must be a finite number, got ${value}.`);
804
821
  return value;
805
822
  }
823
+ function labelBoxes(builder, boxes) {
824
+ if (!boxes) return boxes;
825
+ for (let i = 0; i < boxes.length; i++) {
826
+ const { x, y, width, height } = boxes[i];
827
+ if (Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(width) && width >= 0 && Number.isFinite(height) && height >= 0) continue;
828
+ finite(builder, `labelBounds[${i}].x`, x);
829
+ finite(builder, `labelBounds[${i}].y`, y);
830
+ size(builder, `labelBounds[${i}].width`, width);
831
+ size(builder, `labelBounds[${i}].height`, height);
832
+ }
833
+ return boxes;
834
+ }
806
835
  function size(builder, field, value) {
807
836
  if (!(Number.isFinite(value) && value >= 0)) {
808
837
  throw new RangeError(`describe.${builder}: ${field} must be a finite number \u2265 0, got ${value}.`);
@@ -837,7 +866,8 @@ function describeArc(spec) {
837
866
  outline: closedOutline(sides),
838
867
  bounds: boundsOf(sides.flat().flatMap(extremes)),
839
868
  center: walk.point(0.5),
840
- pivot: center
869
+ pivot: center,
870
+ labelBounds: labelBoxes("arc", spec.labelBounds)
841
871
  });
842
872
  }
843
873
 
@@ -854,6 +884,7 @@ function describeBar(spec) {
854
884
  size("bar", "height", height);
855
885
  const radius = size("bar", "radius", spec.radius ?? 0);
856
886
  const gap = size("bar", "gap", spec.gap ?? 0);
887
+ const labelBounds = labelBoxes("bar", spec.labelBounds);
857
888
  const horizontal = baseline === "left" || baseline === "right";
858
889
  const length = horizontal ? width : height;
859
890
  const thickness = horizontal ? height : width;
@@ -906,7 +937,7 @@ function describeBar(spec) {
906
937
  })
907
938
  )
908
939
  );
909
- return part(0, length, radius, { key, series, bounds: { x, y, width, height }, segments });
940
+ return part(0, length, radius, { key, series, bounds: { x, y, width, height }, labelBounds, segments });
910
941
  }
911
942
  function frame({ x, y, width: w, height: h, baseline }) {
912
943
  switch (baseline) {
@@ -931,12 +962,13 @@ function describeLine(spec) {
931
962
  finite("line", `points[${i}].x`, p.x);
932
963
  finite("line", `points[${i}].y`, p.y);
933
964
  });
965
+ const breaks = checkBreaks(spec.breaks, points.length);
934
966
  const segments = [];
935
967
  for (let i = 1; i < points.length; i++) {
936
968
  segments.push(line(points[i - 1], points[i], segments[segments.length - 1]?.angle ?? 0));
937
969
  }
938
970
  if (segments.length === 0) segments.push(line(first, first));
939
- const walk = createWalk(segments);
971
+ const walk = createWalk(segments, gapsAt(breaks, segments));
940
972
  const box = boundsOf(points);
941
973
  const pad = width / 2;
942
974
  return assemble({
@@ -947,9 +979,29 @@ function describeLine(spec) {
947
979
  outline: openOutline(walk),
948
980
  bounds: { x: box.x - pad, y: box.y - pad, width: box.width + width, height: box.height + width },
949
981
  center: walk.point(0.5),
950
- pivot: first
982
+ pivot: first,
983
+ labelBounds: labelBoxes("line", spec.labelBounds)
984
+ });
985
+ }
986
+ function gapsAt(breaks, segments) {
987
+ const along = [0];
988
+ for (const segment of segments) along.push(along[along.length - 1] + segment.length);
989
+ const length = along[along.length - 1];
990
+ return breaks.filter((i) => along[i] > along[i - 1]).map((i) => [along[i - 1] / length, along[i] / length]);
991
+ }
992
+ function checkBreaks(breaks, count) {
993
+ if (breaks === void 0) return [];
994
+ if (!Array.isArray(breaks)) throw new RangeError(`describe.line: breaks must be an array of point indices, got ${show(breaks)}.`);
995
+ let after = 0;
996
+ breaks.forEach((index, i) => {
997
+ if (!(Number.isInteger(index) && index > after && index < count)) {
998
+ throw new RangeError(`describe.line: breaks[${i}] must be a whole number more than ${after} and less than ${count}, the number of points, got ${show(index)}.`);
999
+ }
1000
+ after = index;
951
1001
  });
1002
+ return breaks;
952
1003
  }
1004
+ var show = (value) => typeof value === "string" ? JSON.stringify(value) : String(value);
953
1005
 
954
1006
  // src/core/geometry/describe/point.ts
955
1007
  function describePoint(spec) {
@@ -970,7 +1022,8 @@ function describePoint(spec) {
970
1022
  ]),
971
1023
  bounds: { x: center.x - r, y: center.y - r, width: 2 * r, height: 2 * r },
972
1024
  center,
973
- pivot: center
1025
+ pivot: center,
1026
+ labelBounds: labelBoxes("point", spec.labelBounds)
974
1027
  });
975
1028
  }
976
1029
 
@@ -996,6 +1049,7 @@ function makeFixtures(moved) {
996
1049
  const y = moved ? 30 : 0;
997
1050
  const bar = (width, more = {}) => describe.bar({ key: "Okafor", series: "goals", x: 0, y, width, height: 20, baseline: "left", ...more });
998
1051
  const [goals, assists] = moved ? [120, 82] : [160, 62];
1052
+ const points = moved ? [{ x: 0, y: 90 }, { x: 64, y: 58 }, { x: 128, y: 82 }, { x: 192, y: 26 }, { x: 256, y: 40 }] : [{ x: 0, y: 100 }, { x: 80, y: 60 }, { x: 160, y: 90 }, { x: 240, y: 20 }];
999
1053
  return Object.freeze({
1000
1054
  bar: bar(moved ? 160 : 200, { radius: 4 }),
1001
1055
  "negative bar": describe.bar({ key: "n", series: "goals", x: moved ? 30 : 0, y, width: moved ? 90 : 120, height: 20, baseline: "right" }),
@@ -1003,12 +1057,9 @@ function makeFixtures(moved) {
1003
1057
  stacked: bar(goals + assists, { series: void 0, gap: 2, segments: [{ series: "goals", size: goals }, { series: "assists", size: assists }] }),
1004
1058
  "short bar": bar(moved ? 4 : 6),
1005
1059
  slice: moved ? describe.arc({ key: "Saved", cx: 140, cy: 130, inner: 60, outer: 100, start: 0.5, end: 2 }) : describe.arc({ key: "Saved", cx: 150, cy: 150, inner: 70, outer: 116, start: 0.3, end: 2.4 }),
1006
- line: describe.line({
1007
- key: "goals",
1008
- series: "goals",
1009
- points: moved ? [{ x: 0, y: 90 }, { x: 64, y: 58 }, { x: 128, y: 82 }, { x: 192, y: 26 }, { x: 256, y: 40 }] : [{ x: 0, y: 100 }, { x: 80, y: 60 }, { x: 160, y: 90 }, { x: 240, y: 20 }],
1010
- width: 2
1011
- }),
1060
+ line: describe.line({ key: "goals", series: "goals", points, width: 2 }),
1061
+ // The line broken after its second point: a run, a gap, and a run.
1062
+ "gapped line": describe.line({ key: "goals", series: "goals", points, breaks: [2], width: 2 }),
1012
1063
  point: describe.point({ key: "p", x: moved ? 40 : 10, y: moved ? 25 : 10, r: 3 })
1013
1064
  });
1014
1065
  }
@@ -1119,27 +1170,27 @@ async function retraces(effect, a, b, icons) {
1119
1170
  const back = await opening(effect, b, a, 1 - p, icons);
1120
1171
  if (same(back, forward)) continue;
1121
1172
  const index = back.findIndex((item, i) => !same(item, forward[i]));
1122
- const what = back.length !== forward.length ? `${back.length} ${back.length === 1 ? "item" : "items"} (${show(back)}) where the run from a to b from start: ${p} begins with ${forward.length} (${show(forward)})` : `item ${index + 1} unlike the run from a to b from start: ${p}: ${difference(back[index], forward[index])}`;
1173
+ const what = back.length !== forward.length ? `${back.length} ${back.length === 1 ? "item" : "items"} (${show2(back)}) where the run from a to b from start: ${p} begins with ${forward.length} (${show2(forward)})` : `item ${index + 1} unlike the run from a to b from start: ${p}: ${difference(back[index], forward[index])}`;
1123
1174
  return `the run from b to a from start: ${1 - p} begins with ${what}`;
1124
1175
  }
1125
1176
  return void 0;
1126
1177
  }
1127
1178
  function difference(actual, expected) {
1128
- if (typeof actual !== "object" || typeof expected !== "object" || actual === null || expected === null) return `${show(actual)}, not ${show(expected)}`;
1179
+ if (typeof actual !== "object" || typeof expected !== "object" || actual === null || expected === null) return `${show2(actual)}, not ${show2(expected)}`;
1129
1180
  const a = actual;
1130
1181
  const b = expected;
1131
1182
  for (const field of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)])) {
1132
1183
  const x = a[field];
1133
1184
  const y = b[field];
1134
1185
  if (same(x, y)) continue;
1135
- if (typeof x !== "string" || typeof y !== "string") return `its ${field} is ${show(x)}, not ${show(y)}`;
1186
+ if (typeof x !== "string" || typeof y !== "string") return `its ${field} is ${show2(x)}, not ${show2(y)}`;
1136
1187
  let at = 0;
1137
1188
  while (at < x.length && x[at] === y[at]) at++;
1138
1189
  const from = Math.max(0, at - 24);
1139
1190
  const part = (text) => `${from > 0 ? "\u2026" : ""}${text.slice(from, at + 24)}${at + 24 < text.length ? "\u2026" : ""}`;
1140
1191
  return `its ${field} parts at character ${at + 1}: ${JSON.stringify(part(x))}, not ${JSON.stringify(part(y))}`;
1141
1192
  }
1142
- return `${show(actual)}, not ${show(expected)}`;
1193
+ return `${show2(actual)}, not ${show2(expected)}`;
1143
1194
  }
1144
1195
  function namedPairs(pairs) {
1145
1196
  if (!Array.isArray(pairs)) return Object.entries(pairs).map(([name, pair]) => [`the ${name}`, pair]);
@@ -1318,18 +1369,18 @@ function compare(actual, expected, when) {
1318
1369
  if (!same(drawn, wanted)) {
1319
1370
  const index = drawn.findIndex((item, i) => !same(item, wanted[i]));
1320
1371
  problems2.push(
1321
- drawn.length !== wanted.length ? `${drawn.length} ${drawn.length === 1 ? "item is" : "items are"} drawn ${when} but end() draws ${wanted.length} (${show(drawn)} vs ${show(wanted)})` : `item ${index + 1} drawn ${when} is ${show(drawn[index])} but end() draws ${show(wanted[index])}`
1372
+ drawn.length !== wanted.length ? `${drawn.length} ${drawn.length === 1 ? "item is" : "items are"} drawn ${when} but end() draws ${wanted.length} (${show2(drawn)} vs ${show2(wanted)})` : `item ${index + 1} drawn ${when} is ${show2(drawn[index])} but end() draws ${show2(wanted[index])}`
1322
1373
  );
1323
1374
  }
1324
1375
  const style = rounded(actual.mark.style);
1325
1376
  const wantedStyle = rounded(expected.mark.style);
1326
- if (!same(style, wantedStyle)) problems2.push(`the mark's style ${when} is ${show(style)} but end() leaves ${show(wantedStyle)}`);
1377
+ if (!same(style, wantedStyle)) problems2.push(`the mark's style ${when} is ${show2(style)} but end() leaves ${show2(wantedStyle)}`);
1327
1378
  return problems2.length > 0 ? problems2.join("; ") : void 0;
1328
1379
  }
1329
1380
  function leftovers(ctx) {
1330
1381
  const problems2 = [];
1331
- if (ctx.layer.drawn.length > 0) problems2.push(`${show(ctx.layer.picture())} is still drawn`);
1332
- if (Object.keys(ctx.mark.style).length > 0) problems2.push(`the mark's style is still ${show(ctx.mark.style)}`);
1382
+ if (ctx.layer.drawn.length > 0) problems2.push(`${show2(ctx.layer.picture())} is still drawn`);
1383
+ if (Object.keys(ctx.mark.style).length > 0) problems2.push(`the mark's style is still ${show2(ctx.mark.style)}`);
1333
1384
  if (!ctx.clock.idle) problems2.push("animation frames are still running");
1334
1385
  return problems2.length > 0 ? problems2.join("; ") : void 0;
1335
1386
  }
@@ -1351,12 +1402,12 @@ function rounded(value) {
1351
1402
  return value;
1352
1403
  }
1353
1404
  var LONGEST = 240;
1354
- function show(value) {
1405
+ function show2(value) {
1355
1406
  const text = JSON.stringify(value) ?? String(value);
1356
1407
  return text.length > LONGEST ? `${text.slice(0, LONGEST - 1)}\u2026` : text;
1357
1408
  }
1358
1409
  function describeError(error) {
1359
- return error instanceof Error ? `${error.name}: ${error.message}` : show(error);
1410
+ return error instanceof Error ? `${error.name}: ${error.message}` : show2(error);
1360
1411
  }
1361
1412
  // Annotate the CommonJS export names for ESM import in node:
1362
1413
  0 && (module.exports = {