expressive-charts 0.8.0 → 0.9.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/index.cjs CHANGED
@@ -5230,6 +5230,7 @@ function watchText(svg, clock, changed) {
5230
5230
  return () => {
5231
5231
  observer.disconnect();
5232
5232
  stopFrame?.();
5233
+ probe.remove();
5233
5234
  };
5234
5235
  }
5235
5236
  function watchHoverDuration(svg, motion) {
@@ -10229,11 +10230,25 @@ function hostColors(element, root) {
10229
10230
  const color = paint === "fill" ? fill : paintOf$1(element, root, "stroke");
10230
10231
  if (color === void 0 || color === NONE) return void 0;
10231
10232
  return {
10232
- color,
10233
- strong: `color-mix(in oklab, ${color}, ${cssColor("text")} ${STRONG})`,
10233
+ ...strongColors(color),
10234
10234
  paint
10235
10235
  };
10236
10236
  }
10237
+ function strongColors(color) {
10238
+ return {
10239
+ color,
10240
+ strong: `color-mix(in oklab, ${color}, ${cssColor("text")} ${STRONG})`
10241
+ };
10242
+ }
10243
+ function drawnStyle(element, root, property) {
10244
+ const computed = element.ownerDocument.defaultView?.getComputedStyle?.(element).getPropertyValue(property).trim() ?? "";
10245
+ if (attributesComputed(root)) return computed;
10246
+ for (let node = element; node; node = node === root ? null : node.parentElement) {
10247
+ const value = node.style?.getPropertyValue(property) || node.getAttribute(property);
10248
+ if (value) return value.trim();
10249
+ }
10250
+ return computed;
10251
+ }
10237
10252
  const STRONG = "25%";
10238
10253
  const NONE = "none";
10239
10254
  const SVG_NS$1 = "http://www.w3.org/2000/svg";
@@ -10644,6 +10659,8 @@ const WRAPPING_ROLES = /* @__PURE__ */ new Set([
10644
10659
  "graphics-symbol",
10645
10660
  "graphics-object"
10646
10661
  ]);
10662
+ const insides = /* @__PURE__ */ new WeakMap();
10663
+ const insidesOf = (port) => port ? insides.get(port) : void 0;
10647
10664
  function expressive$1(target, options, env) {
10648
10665
  checkOptions$12(options);
10649
10666
  const motion = chartMotion(options.motion, env.motion);
@@ -10972,8 +10989,10 @@ function expressive$1(target, options, env) {
10972
10989
  const runs = make("g", { class: "ec-runs" }, overlay);
10973
10990
  const ring = maskedRing(overlay);
10974
10991
  const find = (key) => records.get(key) ?? leaving.get(key);
10992
+ const measure = textMeasure(runs);
10975
10993
  const surface = svgSurface({
10976
10994
  overlay: runs,
10995
+ measure,
10977
10996
  mark: (key) => ({
10978
10997
  set: (style) => find(key)?.handle.set(style),
10979
10998
  reset: () => find(key)?.handle.reset()
@@ -10991,6 +11010,12 @@ function expressive$1(target, options, env) {
10991
11010
  mark: record.mark.geometry,
10992
11011
  data: record.mark.data
10993
11012
  });
11013
+ function remeasure() {
11014
+ measure.clear();
11015
+ for (const record of order) runner.change(record.mark.geometry, record.mark.data);
11016
+ }
11017
+ let unwatchText = () => {};
11018
+ const unwatchFonts = watchFonts(doc, remeasure);
10994
11019
  function createRecord(mark) {
10995
11020
  const record = {
10996
11021
  key: mark.key,
@@ -11101,6 +11126,7 @@ function expressive$1(target, options, env) {
11101
11126
  function moveTo(next) {
11102
11127
  detach();
11103
11128
  root = next;
11129
+ unwatchText = watchText(root, clock, remeasure);
11104
11130
  root.appendChild(overlay);
11105
11131
  if ((options.keyboard === true || options.markLabel) && root.getAttribute("role") === "img" && !warnedImage) {
11106
11132
  warnedImage = true;
@@ -11132,6 +11158,7 @@ function expressive$1(target, options, env) {
11132
11158
  endHover();
11133
11159
  interaction?.destroy();
11134
11160
  interaction = void 0;
11161
+ unwatchText();
11135
11162
  }
11136
11163
  function ownerOf(eventTarget) {
11137
11164
  for (let node = eventTarget; node && node !== root; node = node.parentElement) {
@@ -11431,6 +11458,15 @@ function expressive$1(target, options, env) {
11431
11458
  return true;
11432
11459
  }
11433
11460
  };
11461
+ insides.set(link, {
11462
+ root: () => root,
11463
+ ancestor: () => ancestor,
11464
+ elements: () => order.flatMap((record) => record.mark.parts.map((part) => part.element)),
11465
+ keyOf: (element) => ownerOf(element)?.record.key,
11466
+ colorOf: (key) => records.get(key)?.mark.parts[0]?.colors?.color,
11467
+ moveTabStop: (key) => keyboard?.moveTabStop(key) ?? false,
11468
+ edits
11469
+ });
11434
11470
  return linkable(link, {
11435
11471
  refresh(next) {
11436
11472
  if (destroyed) return;
@@ -11509,6 +11545,7 @@ function expressive$1(target, options, env) {
11509
11545
  watching?.destroy();
11510
11546
  runner.destroy();
11511
11547
  unsubscribeMotion?.();
11548
+ unwatchFonts();
11512
11549
  keyboard?.destroy();
11513
11550
  detach();
11514
11551
  overlay.remove();
@@ -11659,6 +11696,119 @@ function geometryChanged(from, to) {
11659
11696
  const b = to.segments ?? [];
11660
11697
  return a.length !== b.length || a.some((segment, i) => segment.key !== b[i].key || segment.outline.d !== b[i].outline.d);
11661
11698
  }
11699
+ // src/charts/adapter/host-view.ts
11700
+ const FONT = [
11701
+ "font-family",
11702
+ "font-size",
11703
+ "font-style",
11704
+ "font-weight",
11705
+ "font-variant-numeric",
11706
+ "letter-spacing",
11707
+ "word-spacing",
11708
+ "dominant-baseline",
11709
+ "fill"
11710
+ ];
11711
+ const LENGTHS = /* @__PURE__ */ new Set([
11712
+ "font-size",
11713
+ "letter-spacing",
11714
+ "word-spacing"
11715
+ ]);
11716
+ function first(element, name) {
11717
+ const list = element[name]?.baseVal;
11718
+ if (list && list.numberOfItems > 0) return list.getItem(0).value;
11719
+ return Number.parseFloat(element.getAttribute(name) ?? "") || 0;
11720
+ }
11721
+ function scaled(value, scale) {
11722
+ if (scale === 1) return value;
11723
+ return value.replace(/(-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(px)?(?![\w.%])/gi, (_, n, px) => `${Math.round(Number(n) * scale * 1e3) / 1e3}${px ?? ""}`);
11724
+ }
11725
+ function hostView(attachment, options = {}) {
11726
+ const port = typeof attachment === "object" && attachment !== null ? portOf(attachment) : void 0;
11727
+ const inside = insidesOf(port);
11728
+ if (!port || !inside?.root()) throw new TypeError("hostView needs an attachment that expressive() or fromVegaView() returned, with marks in an <svg>.");
11729
+ if (typeof options !== "object" || options === null || options.labels !== void 0 && typeof options.labels !== "function") throw new TypeError("hostView: labels must be a function that returns the host's labels, such as () => names.map((element) => ({ element, key: element.textContent, role: \"category\" })).");
11730
+ const { edits } = inside;
11731
+ const svg = () => inside.root();
11732
+ let given;
11733
+ let hidden = [];
11734
+ function read() {
11735
+ if (!options.labels) return [];
11736
+ const keys = new Set(attachment.marks().map((mark) => mark.key));
11737
+ try {
11738
+ return Array.from(options.labels()).filter((label) => label?.element?.localName === "text" && svg().contains(label.element) && keys.has(label.key) && (label.role === "category" || label.role === "value"));
11739
+ } catch (error) {
11740
+ report(error);
11741
+ return [];
11742
+ }
11743
+ }
11744
+ function chartLabel({ element, key, role }) {
11745
+ const root = svg();
11746
+ const m = between$1(view.space, element, root);
11747
+ const at = matrix.apply(m, {
11748
+ x: first(element, "x"),
11749
+ y: first(element, "y")
11750
+ });
11751
+ const scale = scaleOf$2(m);
11752
+ const text = element.ownerDocument.createElementNS(SVG_NS$2, "text");
11753
+ text.textContent = element.textContent;
11754
+ for (const name of ["dx", "dy"]) {
11755
+ const value = element.getAttribute(name);
11756
+ if (value !== null) text.setAttribute(name, scaled(value, scale));
11757
+ }
11758
+ setStyles(text, Object.fromEntries(FONT.map((property) => {
11759
+ const value = drawnStyle(element, root, property);
11760
+ return [property, (LENGTHS.has(property) ? scaled(value, scale) : value) || void 0];
11761
+ })));
11762
+ const anchor = drawnStyle(element, root, "text-anchor");
11763
+ return {
11764
+ key,
11765
+ role,
11766
+ text: element.textContent ?? "",
11767
+ x: at.x,
11768
+ y: at.y,
11769
+ anchor: anchor === "middle" || anchor === "end" ? anchor : "start",
11770
+ element: text
11771
+ };
11772
+ }
11773
+ const view = {
11774
+ get element() {
11775
+ return svg();
11776
+ },
11777
+ get space() {
11778
+ return inside.ancestor() ?? svg();
11779
+ },
11780
+ refresh: (source) => attachment.refresh(source),
11781
+ update: (next) => attachment.update(next),
11782
+ on: (type, listener) => attachment.on(type, listener),
11783
+ marks: () => attachment.marks(),
11784
+ focus: (key) => attachment.focus(key),
11785
+ leave: () => attachment.leave(),
11786
+ destroy() {
11787
+ given = void 0;
11788
+ hidden = [];
11789
+ attachment.destroy();
11790
+ },
11791
+ labels: () => (given ?? read()).map(chartLabel),
11792
+ paintOf(key) {
11793
+ const color = inside.colorOf(key);
11794
+ return color === void 0 ? void 0 : {
11795
+ color: key,
11796
+ drawn: true,
11797
+ cssColor: color
11798
+ };
11799
+ },
11800
+ keyOf: (element) => inside.keyOf(element),
11801
+ moveTabStop: (key) => inside.moveTabStop(key),
11802
+ morphing(on) {
11803
+ for (const element of hidden) edits.style(element, "visibility", void 0);
11804
+ given = on ? read() : void 0;
11805
+ hidden = given ? [...inside.elements(), ...given.map((label) => label.element)] : [];
11806
+ for (const element of hidden) edits.style(element, "visibility", "hidden");
11807
+ edits.toggleClass(svg(), "ec-morphing", on);
11808
+ }
11809
+ };
11810
+ return linkable(port, view);
11811
+ }
11662
11812
  // src/charts/adapter/vega.ts
11663
11813
  const MARK_TYPES = [
11664
11814
  "line",
@@ -12240,13 +12390,14 @@ function chartSwitch$1(target, options, env) {
12240
12390
  const runsLayer = svgElement("g", { class: "ec-switch-runs" }, overlay);
12241
12391
  const labelsLayer = svgElement("g", { class: "ec-switch-labels" }, overlay);
12242
12392
  const announcer = options.announce ? chartAnnouncer(overlay) : void 0;
12393
+ const tokensOf = (paint) => markColorStyles(paint.cssColor === void 0 ? seriesColors(colors.slot(paint.color)) : strongColors(paint.cssColor));
12243
12394
  const orders = /* @__PURE__ */ new WeakMap();
12244
12395
  const surfaceFor = (paints, extras) => ({ group(key, order) {
12245
12396
  const group = svgElement("g", {
12246
12397
  class: "ec-run",
12247
12398
  "data-key": key
12248
12399
  }, runsLayer);
12249
- setStyles(group, markColorStyles(seriesColors(colors.slot(paints[order] ?? key))));
12400
+ setStyles(group, tokensOf(paints[order] ?? { color: key }));
12250
12401
  orders.set(group, order);
12251
12402
  const above = Array.from(runsLayer.children).find((child) => (orders.get(child) ?? -Infinity) > order);
12252
12403
  runsLayer.insertBefore(group, above ?? null);
@@ -12286,7 +12437,7 @@ function chartSwitch$1(target, options, env) {
12286
12437
  ]].forEach(([end, opacity, color]) => {
12287
12438
  if (!end) return;
12288
12439
  const element = endDot();
12289
- setStyles(element, markColorStyles(seriesColors(colors.slot(color))));
12440
+ setStyles(element, tokensOf(color));
12290
12441
  placeDot(element, end);
12291
12442
  dots.push({
12292
12443
  element,
@@ -12361,8 +12512,9 @@ function chartSwitch$1(target, options, env) {
12361
12512
  function readFocus(from) {
12362
12513
  const element = focused();
12363
12514
  const inside = element !== null && from.container.contains(element);
12364
- const keyed = (inside ? element : from.chart.element.querySelector("[tabindex=\"0\"]"))?.closest("[data-key]");
12365
- const key = keyed && from.chart.element.contains(keyed) ? keyed.getAttribute("data-key") : null;
12515
+ const stop = inside ? element : from.chart.element.querySelector("[tabindex=\"0\"]");
12516
+ const keyed = stop?.closest("[data-key]");
12517
+ const key = (stop && from.chart.keyOf?.(stop)) ?? (keyed && from.chart.element.contains(keyed) ? keyed.getAttribute("data-key") : null);
12366
12518
  if (!inside && key === null) return void 0;
12367
12519
  return {
12368
12520
  keys: key === null ? [] : from.chart.standsFor?.(key) ?? [key],
@@ -12415,6 +12567,7 @@ function chartSwitch$1(target, options, env) {
12415
12567
  each.container.classList.remove("ec-switch-inactive");
12416
12568
  each.container.classList.add("ec-switching");
12417
12569
  each.container.setAttribute("inert", "");
12570
+ each.chart.morphing?.(true);
12418
12571
  each.chart.element.classList.add("ec-morphing");
12419
12572
  }
12420
12573
  const { pairs, surface, scene } = prepare(from, to);
@@ -12490,6 +12643,7 @@ function chartSwitch$1(target, options, env) {
12490
12643
  const to = session.forward ? session.b : session.a;
12491
12644
  for (const each of [session.a, session.b]) {
12492
12645
  each.chart.element.classList.remove("ec-morphing");
12646
+ each.chart.morphing?.(false);
12493
12647
  each.container.classList.remove("ec-switching");
12494
12648
  each.container.style.removeProperty("opacity");
12495
12649
  }
@@ -12502,8 +12656,8 @@ function chartSwitch$1(target, options, env) {
12502
12656
  session.settle();
12503
12657
  }
12504
12658
  function prepare(a, b) {
12505
- const toA = between$1(overlay, a.chart.element, wrapper);
12506
- const toB = between$1(overlay, b.chart.element, wrapper);
12659
+ const toA = between$1(overlay, a.chart.space ?? a.chart.element, wrapper);
12660
+ const toB = between$1(overlay, b.chart.space ?? b.chart.element, wrapper);
12507
12661
  const scaleA = scaleOf(toA);
12508
12662
  const scaleB = scaleOf(toB);
12509
12663
  const from = a.chart.marks().map((mark) => mapMark(mark, toA));
@@ -12523,8 +12677,8 @@ function chartSwitch$1(target, options, env) {
12523
12677
  from: was.drawn ? partner : dot$1(partner),
12524
12678
  to: will.drawn ? mark : dot$1(mark)
12525
12679
  });
12526
- paints.push(will.color);
12527
- extras.push(extrasFor([ringOf(was, scaleA), ringOf(will, scaleB)], [endOf(was, partner, scaleA), endOf(will, mark, scaleB)], [was.color, will.color]));
12680
+ paints.push(will);
12681
+ extras.push(extrasFor([ringOf(was, scaleA), ringOf(will, scaleB)], [endOf(was, partner, scaleA), endOf(will, mark, scaleB)], [was, will]));
12528
12682
  }
12529
12683
  const drawings = [];
12530
12684
  if (extras.some((each) => each !== void 0)) drawings.push((s) => {
@@ -12539,16 +12693,18 @@ function chartSwitch$1(target, options, env) {
12539
12693
  const id = (label) => `${label.role}\n${label.key}`;
12540
12694
  const labelsA = a.chart.labels();
12541
12695
  const labelsB = b.chart.labels();
12696
+ const sizeA = a.chart.space ? scaleA : 1;
12697
+ const sizeB = b.chart.space ? scaleB : 1;
12542
12698
  const byId = new Map(labelsA.map((label) => [id(label), label]));
12543
12699
  const matched = /* @__PURE__ */ new Set();
12544
12700
  for (const label of labelsB) {
12545
12701
  const partner = byId.get(id(label));
12546
12702
  if (!partner) continue;
12547
12703
  matched.add(id(label));
12548
- drawings.push(moveLabel(copyLabel(partner, toA), copyLabel(label, toB)));
12704
+ drawings.push(moveLabel(copyLabel(partner, toA, sizeA), copyLabel(label, toB, sizeB)));
12549
12705
  }
12550
- for (const label of labelsA) if (!matched.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toA), fadeOut));
12551
- for (const label of labelsB) if (!byId.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toB), fadeIn));
12706
+ for (const label of labelsA) if (!matched.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toA, sizeA), fadeOut));
12707
+ for (const label of labelsB) if (!byId.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toB, sizeB), fadeIn));
12552
12708
  drawings.push((s) => {
12553
12709
  setOpacity(a.container, 1 - clamp$1(s / OUT_BY));
12554
12710
  setOpacity(b.container, clamp$1((s - HALF) / .5));
@@ -12573,7 +12729,7 @@ function chartSwitch$1(target, options, env) {
12573
12729
  class: "ec-switch-fade",
12574
12730
  "data-key": mark.key
12575
12731
  }, fadesLayer);
12576
- setStyles(group, markColorStyles(seriesColors(colors.slot(paint.color))));
12732
+ setStyles(group, tokensOf(paint));
12577
12733
  const ring = ringOf(paint, scale);
12578
12734
  svgLayer(group).path(mark.outline.d, mark.outline.closed ? {
12579
12735
  fill: "mark",
@@ -12598,7 +12754,7 @@ function chartSwitch$1(target, options, env) {
12598
12754
  });
12599
12755
  return element;
12600
12756
  }
12601
- function copyLabel(label, m) {
12757
+ function copyLabel(label, m, scale) {
12602
12758
  const text = label.element.cloneNode(true);
12603
12759
  for (const name of [
12604
12760
  "id",
@@ -12607,17 +12763,19 @@ function chartSwitch$1(target, options, env) {
12607
12763
  "aria-hidden"
12608
12764
  ]) text.removeAttribute(name);
12609
12765
  text.textContent = label.text;
12610
- const at = matrix.apply(m, label);
12766
+ const copy = {
12767
+ text,
12768
+ at: matrix.apply(m, label),
12769
+ scale
12770
+ };
12611
12771
  setAttributes(text, {
12612
- x: at.x,
12613
- y: at.y,
12614
- "text-anchor": label.anchor
12772
+ x: copy.at.x,
12773
+ y: copy.at.y,
12774
+ "text-anchor": label.anchor,
12775
+ ...scaledAt(copy, copy.at.x, copy.at.y)
12615
12776
  });
12616
12777
  labelsLayer.appendChild(text);
12617
- return {
12618
- text,
12619
- at
12620
- };
12778
+ return copy;
12621
12779
  }
12622
12780
  const api = {
12623
12781
  get current() {
@@ -12669,6 +12827,7 @@ function chartSwitch$1(target, options, env) {
12669
12827
  session.driver?.cancel();
12670
12828
  session.transition?.clear();
12671
12829
  session.scene.clear();
12830
+ for (const each of [session.a, session.b]) each.chart.morphing?.(false);
12672
12831
  session.settle();
12673
12832
  }
12674
12833
  stopMotion();
@@ -12768,15 +12927,25 @@ function moveLabel(a, b) {
12768
12927
  setAttributes(a.text, {
12769
12928
  x,
12770
12929
  y,
12771
- visibility: second ? "hidden" : void 0
12930
+ visibility: second ? "hidden" : void 0,
12931
+ ...scaledAt(a, x, y)
12772
12932
  });
12773
12933
  setAttributes(b.text, {
12774
12934
  x,
12775
12935
  y,
12776
- visibility: second ? void 0 : "hidden"
12936
+ visibility: second ? void 0 : "hidden",
12937
+ ...scaledAt(b, x, y)
12777
12938
  });
12778
12939
  };
12779
12940
  }
12941
+ const scaledAt = (copy, x, y) => copy.scale === 1 ? {} : { transform: transformAttribute([
12942
+ copy.scale,
12943
+ 0,
12944
+ 0,
12945
+ copy.scale,
12946
+ x * (1 - copy.scale),
12947
+ y * (1 - copy.scale)
12948
+ ]) };
12780
12949
  function fadeLabel(label, opacity) {
12781
12950
  return (s) => setAttributes(label.text, { opacity: opacity(s) });
12782
12951
  }
@@ -15044,7 +15213,7 @@ function configure(options) {
15044
15213
  }
15045
15214
  }
15046
15215
  // src/index.ts
15047
- const VERSION = "0.8.0";
15216
+ const VERSION = "0.9.0";
15048
15217
  const env = {
15049
15218
  presets: presetRegistry,
15050
15219
  icons: iconRegistry,
@@ -15097,6 +15266,7 @@ exports.expressive = expressive;
15097
15266
  exports.flash = flash;
15098
15267
  exports.fromVegaView = fromVegaView;
15099
15268
  exports.gaits = gaits;
15269
+ exports.hostView = hostView;
15100
15270
  exports.iconFromSvg = iconFromSvg;
15101
15271
  exports.icons = icons;
15102
15272
  exports.interpolateNumber = interpolateNumber;