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.js CHANGED
@@ -3134,6 +3134,7 @@ function watchText(svg, clock, changed) {
3134
3134
  return () => {
3135
3135
  observer.disconnect();
3136
3136
  stopFrame?.();
3137
+ probe.remove();
3137
3138
  };
3138
3139
  }
3139
3140
  function watchHoverDuration(svg, motion) {
@@ -8133,11 +8134,25 @@ function hostColors(element, root) {
8133
8134
  const color = paint === "fill" ? fill : paintOf$1(element, root, "stroke");
8134
8135
  if (color === void 0 || color === NONE) return void 0;
8135
8136
  return {
8136
- color,
8137
- strong: `color-mix(in oklab, ${color}, ${cssColor("text")} ${STRONG})`,
8137
+ ...strongColors(color),
8138
8138
  paint
8139
8139
  };
8140
8140
  }
8141
+ function strongColors(color) {
8142
+ return {
8143
+ color,
8144
+ strong: `color-mix(in oklab, ${color}, ${cssColor("text")} ${STRONG})`
8145
+ };
8146
+ }
8147
+ function drawnStyle(element, root, property) {
8148
+ const computed = element.ownerDocument.defaultView?.getComputedStyle?.(element).getPropertyValue(property).trim() ?? "";
8149
+ if (attributesComputed(root)) return computed;
8150
+ for (let node = element; node; node = node === root ? null : node.parentElement) {
8151
+ const value = node.style?.getPropertyValue(property) || node.getAttribute(property);
8152
+ if (value) return value.trim();
8153
+ }
8154
+ return computed;
8155
+ }
8141
8156
  const STRONG = "25%";
8142
8157
  const NONE = "none";
8143
8158
  const SVG_NS$1 = "http://www.w3.org/2000/svg";
@@ -8548,6 +8563,8 @@ const WRAPPING_ROLES = /* @__PURE__ */ new Set([
8548
8563
  "graphics-symbol",
8549
8564
  "graphics-object"
8550
8565
  ]);
8566
+ const insides = /* @__PURE__ */ new WeakMap();
8567
+ const insidesOf = (port) => port ? insides.get(port) : void 0;
8551
8568
  function expressive$1(target, options, env) {
8552
8569
  checkOptions$12(options);
8553
8570
  const motion = chartMotion(options.motion, env.motion);
@@ -8877,8 +8894,10 @@ function expressive$1(target, options, env) {
8877
8894
  const runs = make("g", { class: "ec-runs" }, overlay);
8878
8895
  const ring = maskedRing(overlay);
8879
8896
  const find = (key) => records.get(key) ?? leaving.get(key);
8897
+ const measure = textMeasure(runs);
8880
8898
  const surface = svgSurface({
8881
8899
  overlay: runs,
8900
+ measure,
8882
8901
  mark: (key) => ({
8883
8902
  set: (style) => find(key)?.handle.set(style),
8884
8903
  reset: () => find(key)?.handle.reset()
@@ -8896,6 +8915,12 @@ function expressive$1(target, options, env) {
8896
8915
  mark: record.mark.geometry,
8897
8916
  data: record.mark.data
8898
8917
  });
8918
+ function remeasure() {
8919
+ measure.clear();
8920
+ for (const record of order) runner.change(record.mark.geometry, record.mark.data);
8921
+ }
8922
+ let unwatchText = () => {};
8923
+ const unwatchFonts = watchFonts(doc, remeasure);
8899
8924
  function createRecord(mark) {
8900
8925
  const record = {
8901
8926
  key: mark.key,
@@ -9006,6 +9031,7 @@ function expressive$1(target, options, env) {
9006
9031
  function moveTo(next) {
9007
9032
  detach();
9008
9033
  root = next;
9034
+ unwatchText = watchText(root, clock, remeasure);
9009
9035
  root.appendChild(overlay);
9010
9036
  if ((options.keyboard === true || options.markLabel) && root.getAttribute("role") === "img" && !warnedImage) {
9011
9037
  warnedImage = true;
@@ -9037,6 +9063,7 @@ function expressive$1(target, options, env) {
9037
9063
  endHover();
9038
9064
  interaction?.destroy();
9039
9065
  interaction = void 0;
9066
+ unwatchText();
9040
9067
  }
9041
9068
  function ownerOf(eventTarget) {
9042
9069
  for (let node = eventTarget; node && node !== root; node = node.parentElement) {
@@ -9336,6 +9363,15 @@ function expressive$1(target, options, env) {
9336
9363
  return true;
9337
9364
  }
9338
9365
  };
9366
+ insides.set(link, {
9367
+ root: () => root,
9368
+ ancestor: () => ancestor,
9369
+ elements: () => order.flatMap((record) => record.mark.parts.map((part) => part.element)),
9370
+ keyOf: (element) => ownerOf(element)?.record.key,
9371
+ colorOf: (key) => records.get(key)?.mark.parts[0]?.colors?.color,
9372
+ moveTabStop: (key) => keyboard?.moveTabStop(key) ?? false,
9373
+ edits
9374
+ });
9339
9375
  return linkable(link, {
9340
9376
  refresh(next) {
9341
9377
  if (destroyed) return;
@@ -9414,6 +9450,7 @@ function expressive$1(target, options, env) {
9414
9450
  watching?.destroy();
9415
9451
  runner.destroy();
9416
9452
  unsubscribeMotion?.();
9453
+ unwatchFonts();
9417
9454
  keyboard?.destroy();
9418
9455
  detach();
9419
9456
  overlay.remove();
@@ -9564,6 +9601,119 @@ function geometryChanged(from, to) {
9564
9601
  const b = to.segments ?? [];
9565
9602
  return a.length !== b.length || a.some((segment, i) => segment.key !== b[i].key || segment.outline.d !== b[i].outline.d);
9566
9603
  }
9604
+ // src/charts/adapter/host-view.ts
9605
+ const FONT = [
9606
+ "font-family",
9607
+ "font-size",
9608
+ "font-style",
9609
+ "font-weight",
9610
+ "font-variant-numeric",
9611
+ "letter-spacing",
9612
+ "word-spacing",
9613
+ "dominant-baseline",
9614
+ "fill"
9615
+ ];
9616
+ const LENGTHS = /* @__PURE__ */ new Set([
9617
+ "font-size",
9618
+ "letter-spacing",
9619
+ "word-spacing"
9620
+ ]);
9621
+ function first(element, name) {
9622
+ const list = element[name]?.baseVal;
9623
+ if (list && list.numberOfItems > 0) return list.getItem(0).value;
9624
+ return Number.parseFloat(element.getAttribute(name) ?? "") || 0;
9625
+ }
9626
+ function scaled(value, scale) {
9627
+ if (scale === 1) return value;
9628
+ return value.replace(/(-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(px)?(?![\w.%])/gi, (_, n, px) => `${Math.round(Number(n) * scale * 1e3) / 1e3}${px ?? ""}`);
9629
+ }
9630
+ function hostView(attachment, options = {}) {
9631
+ const port = typeof attachment === "object" && attachment !== null ? portOf(attachment) : void 0;
9632
+ const inside = insidesOf(port);
9633
+ if (!port || !inside?.root()) throw new TypeError("hostView needs an attachment that expressive() or fromVegaView() returned, with marks in an <svg>.");
9634
+ 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\" })).");
9635
+ const { edits } = inside;
9636
+ const svg = () => inside.root();
9637
+ let given;
9638
+ let hidden = [];
9639
+ function read() {
9640
+ if (!options.labels) return [];
9641
+ const keys = new Set(attachment.marks().map((mark) => mark.key));
9642
+ try {
9643
+ 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"));
9644
+ } catch (error) {
9645
+ report(error);
9646
+ return [];
9647
+ }
9648
+ }
9649
+ function chartLabel({ element, key, role }) {
9650
+ const root = svg();
9651
+ const m = between$1(view.space, element, root);
9652
+ const at = matrix.apply(m, {
9653
+ x: first(element, "x"),
9654
+ y: first(element, "y")
9655
+ });
9656
+ const scale = scaleOf$2(m);
9657
+ const text = element.ownerDocument.createElementNS(SVG_NS$2, "text");
9658
+ text.textContent = element.textContent;
9659
+ for (const name of ["dx", "dy"]) {
9660
+ const value = element.getAttribute(name);
9661
+ if (value !== null) text.setAttribute(name, scaled(value, scale));
9662
+ }
9663
+ setStyles(text, Object.fromEntries(FONT.map((property) => {
9664
+ const value = drawnStyle(element, root, property);
9665
+ return [property, (LENGTHS.has(property) ? scaled(value, scale) : value) || void 0];
9666
+ })));
9667
+ const anchor = drawnStyle(element, root, "text-anchor");
9668
+ return {
9669
+ key,
9670
+ role,
9671
+ text: element.textContent ?? "",
9672
+ x: at.x,
9673
+ y: at.y,
9674
+ anchor: anchor === "middle" || anchor === "end" ? anchor : "start",
9675
+ element: text
9676
+ };
9677
+ }
9678
+ const view = {
9679
+ get element() {
9680
+ return svg();
9681
+ },
9682
+ get space() {
9683
+ return inside.ancestor() ?? svg();
9684
+ },
9685
+ refresh: (source) => attachment.refresh(source),
9686
+ update: (next) => attachment.update(next),
9687
+ on: (type, listener) => attachment.on(type, listener),
9688
+ marks: () => attachment.marks(),
9689
+ focus: (key) => attachment.focus(key),
9690
+ leave: () => attachment.leave(),
9691
+ destroy() {
9692
+ given = void 0;
9693
+ hidden = [];
9694
+ attachment.destroy();
9695
+ },
9696
+ labels: () => (given ?? read()).map(chartLabel),
9697
+ paintOf(key) {
9698
+ const color = inside.colorOf(key);
9699
+ return color === void 0 ? void 0 : {
9700
+ color: key,
9701
+ drawn: true,
9702
+ cssColor: color
9703
+ };
9704
+ },
9705
+ keyOf: (element) => inside.keyOf(element),
9706
+ moveTabStop: (key) => inside.moveTabStop(key),
9707
+ morphing(on) {
9708
+ for (const element of hidden) edits.style(element, "visibility", void 0);
9709
+ given = on ? read() : void 0;
9710
+ hidden = given ? [...inside.elements(), ...given.map((label) => label.element)] : [];
9711
+ for (const element of hidden) edits.style(element, "visibility", "hidden");
9712
+ edits.toggleClass(svg(), "ec-morphing", on);
9713
+ }
9714
+ };
9715
+ return linkable(port, view);
9716
+ }
9567
9717
  // src/charts/adapter/vega.ts
9568
9718
  const MARK_TYPES = [
9569
9719
  "line",
@@ -10001,13 +10151,14 @@ function chartSwitch$1(target, options, env) {
10001
10151
  const runsLayer = svgElement("g", { class: "ec-switch-runs" }, overlay);
10002
10152
  const labelsLayer = svgElement("g", { class: "ec-switch-labels" }, overlay);
10003
10153
  const announcer = options.announce ? chartAnnouncer(overlay) : void 0;
10154
+ const tokensOf = (paint) => markColorStyles(paint.cssColor === void 0 ? seriesColors(colors.slot(paint.color)) : strongColors(paint.cssColor));
10004
10155
  const orders = /* @__PURE__ */ new WeakMap();
10005
10156
  const surfaceFor = (paints, extras) => ({ group(key, order) {
10006
10157
  const group = svgElement("g", {
10007
10158
  class: "ec-run",
10008
10159
  "data-key": key
10009
10160
  }, runsLayer);
10010
- setStyles(group, markColorStyles(seriesColors(colors.slot(paints[order] ?? key))));
10161
+ setStyles(group, tokensOf(paints[order] ?? { color: key }));
10011
10162
  orders.set(group, order);
10012
10163
  const above = Array.from(runsLayer.children).find((child) => (orders.get(child) ?? -Infinity) > order);
10013
10164
  runsLayer.insertBefore(group, above ?? null);
@@ -10047,7 +10198,7 @@ function chartSwitch$1(target, options, env) {
10047
10198
  ]].forEach(([end, opacity, color]) => {
10048
10199
  if (!end) return;
10049
10200
  const element = endDot();
10050
- setStyles(element, markColorStyles(seriesColors(colors.slot(color))));
10201
+ setStyles(element, tokensOf(color));
10051
10202
  placeDot(element, end);
10052
10203
  dots.push({
10053
10204
  element,
@@ -10122,8 +10273,9 @@ function chartSwitch$1(target, options, env) {
10122
10273
  function readFocus(from) {
10123
10274
  const element = focused();
10124
10275
  const inside = element !== null && from.container.contains(element);
10125
- const keyed = (inside ? element : from.chart.element.querySelector("[tabindex=\"0\"]"))?.closest("[data-key]");
10126
- const key = keyed && from.chart.element.contains(keyed) ? keyed.getAttribute("data-key") : null;
10276
+ const stop = inside ? element : from.chart.element.querySelector("[tabindex=\"0\"]");
10277
+ const keyed = stop?.closest("[data-key]");
10278
+ const key = (stop && from.chart.keyOf?.(stop)) ?? (keyed && from.chart.element.contains(keyed) ? keyed.getAttribute("data-key") : null);
10127
10279
  if (!inside && key === null) return void 0;
10128
10280
  return {
10129
10281
  keys: key === null ? [] : from.chart.standsFor?.(key) ?? [key],
@@ -10176,6 +10328,7 @@ function chartSwitch$1(target, options, env) {
10176
10328
  each.container.classList.remove("ec-switch-inactive");
10177
10329
  each.container.classList.add("ec-switching");
10178
10330
  each.container.setAttribute("inert", "");
10331
+ each.chart.morphing?.(true);
10179
10332
  each.chart.element.classList.add("ec-morphing");
10180
10333
  }
10181
10334
  const { pairs, surface, scene } = prepare(from, to);
@@ -10251,6 +10404,7 @@ function chartSwitch$1(target, options, env) {
10251
10404
  const to = session.forward ? session.b : session.a;
10252
10405
  for (const each of [session.a, session.b]) {
10253
10406
  each.chart.element.classList.remove("ec-morphing");
10407
+ each.chart.morphing?.(false);
10254
10408
  each.container.classList.remove("ec-switching");
10255
10409
  each.container.style.removeProperty("opacity");
10256
10410
  }
@@ -10263,8 +10417,8 @@ function chartSwitch$1(target, options, env) {
10263
10417
  session.settle();
10264
10418
  }
10265
10419
  function prepare(a, b) {
10266
- const toA = between$1(overlay, a.chart.element, wrapper);
10267
- const toB = between$1(overlay, b.chart.element, wrapper);
10420
+ const toA = between$1(overlay, a.chart.space ?? a.chart.element, wrapper);
10421
+ const toB = between$1(overlay, b.chart.space ?? b.chart.element, wrapper);
10268
10422
  const scaleA = scaleOf(toA);
10269
10423
  const scaleB = scaleOf(toB);
10270
10424
  const from = a.chart.marks().map((mark) => mapMark(mark, toA));
@@ -10284,8 +10438,8 @@ function chartSwitch$1(target, options, env) {
10284
10438
  from: was.drawn ? partner : dot$1(partner),
10285
10439
  to: will.drawn ? mark : dot$1(mark)
10286
10440
  });
10287
- paints.push(will.color);
10288
- extras.push(extrasFor([ringOf(was, scaleA), ringOf(will, scaleB)], [endOf(was, partner, scaleA), endOf(will, mark, scaleB)], [was.color, will.color]));
10441
+ paints.push(will);
10442
+ extras.push(extrasFor([ringOf(was, scaleA), ringOf(will, scaleB)], [endOf(was, partner, scaleA), endOf(will, mark, scaleB)], [was, will]));
10289
10443
  }
10290
10444
  const drawings = [];
10291
10445
  if (extras.some((each) => each !== void 0)) drawings.push((s) => {
@@ -10300,16 +10454,18 @@ function chartSwitch$1(target, options, env) {
10300
10454
  const id = (label) => `${label.role}\n${label.key}`;
10301
10455
  const labelsA = a.chart.labels();
10302
10456
  const labelsB = b.chart.labels();
10457
+ const sizeA = a.chart.space ? scaleA : 1;
10458
+ const sizeB = b.chart.space ? scaleB : 1;
10303
10459
  const byId = new Map(labelsA.map((label) => [id(label), label]));
10304
10460
  const matched = /* @__PURE__ */ new Set();
10305
10461
  for (const label of labelsB) {
10306
10462
  const partner = byId.get(id(label));
10307
10463
  if (!partner) continue;
10308
10464
  matched.add(id(label));
10309
- drawings.push(moveLabel(copyLabel(partner, toA), copyLabel(label, toB)));
10465
+ drawings.push(moveLabel(copyLabel(partner, toA, sizeA), copyLabel(label, toB, sizeB)));
10310
10466
  }
10311
- for (const label of labelsA) if (!matched.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toA), fadeOut));
10312
- for (const label of labelsB) if (!byId.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toB), fadeIn));
10467
+ for (const label of labelsA) if (!matched.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toA, sizeA), fadeOut));
10468
+ for (const label of labelsB) if (!byId.has(id(label))) drawings.push(fadeLabel(copyLabel(label, toB, sizeB), fadeIn));
10313
10469
  drawings.push((s) => {
10314
10470
  setOpacity(a.container, 1 - clamp$1(s / OUT_BY));
10315
10471
  setOpacity(b.container, clamp$1((s - HALF) / .5));
@@ -10334,7 +10490,7 @@ function chartSwitch$1(target, options, env) {
10334
10490
  class: "ec-switch-fade",
10335
10491
  "data-key": mark.key
10336
10492
  }, fadesLayer);
10337
- setStyles(group, markColorStyles(seriesColors(colors.slot(paint.color))));
10493
+ setStyles(group, tokensOf(paint));
10338
10494
  const ring = ringOf(paint, scale);
10339
10495
  svgLayer(group).path(mark.outline.d, mark.outline.closed ? {
10340
10496
  fill: "mark",
@@ -10359,7 +10515,7 @@ function chartSwitch$1(target, options, env) {
10359
10515
  });
10360
10516
  return element;
10361
10517
  }
10362
- function copyLabel(label, m) {
10518
+ function copyLabel(label, m, scale) {
10363
10519
  const text = label.element.cloneNode(true);
10364
10520
  for (const name of [
10365
10521
  "id",
@@ -10368,17 +10524,19 @@ function chartSwitch$1(target, options, env) {
10368
10524
  "aria-hidden"
10369
10525
  ]) text.removeAttribute(name);
10370
10526
  text.textContent = label.text;
10371
- const at = matrix.apply(m, label);
10527
+ const copy = {
10528
+ text,
10529
+ at: matrix.apply(m, label),
10530
+ scale
10531
+ };
10372
10532
  setAttributes(text, {
10373
- x: at.x,
10374
- y: at.y,
10375
- "text-anchor": label.anchor
10533
+ x: copy.at.x,
10534
+ y: copy.at.y,
10535
+ "text-anchor": label.anchor,
10536
+ ...scaledAt(copy, copy.at.x, copy.at.y)
10376
10537
  });
10377
10538
  labelsLayer.appendChild(text);
10378
- return {
10379
- text,
10380
- at
10381
- };
10539
+ return copy;
10382
10540
  }
10383
10541
  const api = {
10384
10542
  get current() {
@@ -10430,6 +10588,7 @@ function chartSwitch$1(target, options, env) {
10430
10588
  session.driver?.cancel();
10431
10589
  session.transition?.clear();
10432
10590
  session.scene.clear();
10591
+ for (const each of [session.a, session.b]) each.chart.morphing?.(false);
10433
10592
  session.settle();
10434
10593
  }
10435
10594
  stopMotion();
@@ -10529,15 +10688,25 @@ function moveLabel(a, b) {
10529
10688
  setAttributes(a.text, {
10530
10689
  x,
10531
10690
  y,
10532
- visibility: second ? "hidden" : void 0
10691
+ visibility: second ? "hidden" : void 0,
10692
+ ...scaledAt(a, x, y)
10533
10693
  });
10534
10694
  setAttributes(b.text, {
10535
10695
  x,
10536
10696
  y,
10537
- visibility: second ? void 0 : "hidden"
10697
+ visibility: second ? void 0 : "hidden",
10698
+ ...scaledAt(b, x, y)
10538
10699
  });
10539
10700
  };
10540
10701
  }
10702
+ const scaledAt = (copy, x, y) => copy.scale === 1 ? {} : { transform: transformAttribute([
10703
+ copy.scale,
10704
+ 0,
10705
+ 0,
10706
+ copy.scale,
10707
+ x * (1 - copy.scale),
10708
+ y * (1 - copy.scale)
10709
+ ]) };
10541
10710
  function fadeLabel(label, opacity) {
10542
10711
  return (s) => setAttributes(label.text, { opacity: opacity(s) });
10543
10712
  }
@@ -12713,7 +12882,7 @@ function configure(options) {
12713
12882
  }
12714
12883
  }
12715
12884
  // src/index.ts
12716
- const VERSION = "0.8.0";
12885
+ const VERSION = "0.9.0";
12717
12886
  const env = {
12718
12887
  presets: presetRegistry,
12719
12888
  icons: iconRegistry,
@@ -12743,6 +12912,6 @@ function fromVegaView(view, options) {
12743
12912
  function motionSwitch(target, motion$1 = motion, options) {
12744
12913
  return motionSwitch$1(target, motion$1, options);
12745
12914
  }
12746
- export { VERSION, animated, barChart, chartSwitch, colorAssignment, configure, createClock, createMotion, defaultClock, defaultScaling, defineDataEffect, defineEffect, defineIcon, delta, describe, donutChart, drawIn, easeInOutCubic, easeOutCubic, expressive, flash, fromVegaView, gaits, iconFromSvg, icons, interpolateNumber, interpolatePoint, interpolateRecord, label, lineChart, linear, link, manualClock, matrix, mediaReducedMotion, morph, motion, motionSwitch, offset, only, parallel, playButton, playback, pulse, registerIcon, registerIcons, registerPreset, samplesAlong, scaleDuration, sequence, stagger, threshold, trace, travel, tween, wiggle };
12915
+ export { VERSION, animated, barChart, chartSwitch, colorAssignment, configure, createClock, createMotion, defaultClock, defaultScaling, defineDataEffect, defineEffect, defineIcon, delta, describe, donutChart, drawIn, easeInOutCubic, easeOutCubic, expressive, flash, fromVegaView, gaits, hostView, iconFromSvg, icons, interpolateNumber, interpolatePoint, interpolateRecord, label, lineChart, linear, link, manualClock, matrix, mediaReducedMotion, morph, motion, motionSwitch, offset, only, parallel, playButton, playback, pulse, registerIcon, registerIcons, registerPreset, samplesAlong, scaleDuration, sequence, stagger, threshold, trace, travel, tween, wiggle };
12747
12916
 
12748
12917
  //# sourceMappingURL=index.js.map