vvplot 0.4.2 → 0.4.3

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.
@@ -1660,7 +1660,44 @@ function baseParse(input, options) {
1660
1660
  new RegExp(
1661
1661
  "\\b" + "arguments,await,break,case,catch,class,const,continue,debugger,default,delete,do,else,export,extends,finally,for,function,if,import,let,new,return,super,switch,throw,try,var,void,while,with,yield".split(",").join("\\b|\\b") + "\\b"
1662
1662
  );
1663
- class GEnumElement {
1663
+ class AsisString extends String {
1664
+ static #cache = /* @__PURE__ */ new Map();
1665
+ constructor(str) {
1666
+ const s = String(str);
1667
+ if (AsisString.#cache.has(s)) {
1668
+ return AsisString.#cache.get(s);
1669
+ }
1670
+ super(s);
1671
+ AsisString.#cache.set(s, this);
1672
+ }
1673
+ }
1674
+ class AsisNumber extends Number {
1675
+ static #cache = /* @__PURE__ */ new Map();
1676
+ constructor(num2) {
1677
+ const n = Number(num2);
1678
+ if (AsisNumber.#cache.has(n)) {
1679
+ return AsisNumber.#cache.get(n);
1680
+ }
1681
+ super(n);
1682
+ AsisNumber.#cache.set(n, this);
1683
+ }
1684
+ }
1685
+ class Asis extends String {
1686
+ static #cache = /* @__PURE__ */ new Map();
1687
+ constructor(value) {
1688
+ if (typeof value === "string" || value instanceof String) {
1689
+ return new AsisString(value);
1690
+ }
1691
+ if (typeof value === "number" || value instanceof Number) {
1692
+ return new AsisNumber(value);
1693
+ }
1694
+ throw new Error("Unsupported type");
1695
+ }
1696
+ static [Symbol.hasInstance](obj) {
1697
+ return obj instanceof AsisString || obj instanceof AsisNumber;
1698
+ }
1699
+ }
1700
+ class EnumElement {
1664
1701
  constructor(label, value, level) {
1665
1702
  this.label = label;
1666
1703
  this.value = value;
@@ -1676,20 +1713,20 @@ class GEnumElement {
1676
1713
  return this.label;
1677
1714
  }
1678
1715
  }
1679
- class GEnumLevel extends Array {
1716
+ class EnumLevel extends Array {
1680
1717
  /**
1681
1718
  * return:
1682
- * x if x is a GEnumLevel
1683
- * new GEnumLevel from distinct values of x, ordered by natural order, if x is iterable
1684
- * new GEnumLevel from keys of x, orderd by coresponing values, if x is an object
1719
+ * x if x is a EnumLevel
1720
+ * new EnumLevel from distinct values of x, ordered by natural order, if x is iterable
1721
+ * new EnumLevel from keys of x, orderd by coresponing values, if x is an object
1685
1722
  * @param {*} x
1686
1723
  * @param {function} [sortkey]
1687
- * @returns {GEnumLevel}
1724
+ * @returns {EnumLevel}
1688
1725
  */
1689
1726
  static from(x2) {
1690
1727
  if (x2 instanceof this) return x2;
1691
1728
  if (x2[Symbol.iterator]) {
1692
- let lvl = unique(Array.from(x2).map((x3) => String(x3))).sort((a, b) => compare(a, b, { numeric: true }));
1729
+ let lvl = unique(Array.from(x2).filter((x3) => !(x3 instanceof Asis)).map((x3) => String(x3))).sort((a, b) => compare(a, b, { numeric: true }));
1693
1730
  return new this(...lvl);
1694
1731
  } else if (typeof x2 === "object") {
1695
1732
  let lvl = Object.keys(x2).map((x3) => String(x3)).sort((a, b) => compare(x2[a], x2[b]));
@@ -1698,7 +1735,7 @@ class GEnumLevel extends Array {
1698
1735
  throw new Error(`Invalid level values: ${x2}`);
1699
1736
  }
1700
1737
  /**
1701
- * Check if two GEnumLevel are equal, i.e. they have the same labels in the same order
1738
+ * Check if two EnumLevel are equal, i.e. they have the same labels in the same order
1702
1739
  * @param {string[]} level
1703
1740
  */
1704
1741
  static isEqual(a, b) {
@@ -1707,20 +1744,20 @@ class GEnumLevel extends Array {
1707
1744
  }
1708
1745
  }
1709
1746
  /**
1710
- * build a GEnumLevel
1747
+ * build a EnumLevel
1711
1748
  * @param {string[]} level
1712
1749
  */
1713
1750
  constructor(...level) {
1714
- level = level.map((x2, i) => new GEnumElement(String(x2), i));
1751
+ level = level.map((x2, i) => new EnumElement(String(x2), i));
1715
1752
  level.forEach((l) => l.level = level);
1716
1753
  level.mapping = Object.fromEntries(level.map((fct) => [fct, fct]));
1717
- Object.setPrototypeOf(level, GEnumLevel.prototype);
1754
+ Object.setPrototypeOf(level, EnumLevel.prototype);
1718
1755
  return level;
1719
1756
  }
1720
1757
  /**
1721
1758
  * get the level instance by index or key
1722
1759
  * @param {(number|string)} idx
1723
- * @returns {GEnumElement}
1760
+ * @returns {EnumElement}
1724
1761
  */
1725
1762
  getItem(idx) {
1726
1763
  if (typeof idx === "number" || idx instanceof Number) {
@@ -1732,15 +1769,15 @@ class GEnumLevel extends Array {
1732
1769
  /**
1733
1770
  * convert an array to a GEnum array
1734
1771
  * @param {string[]} arr array to be converted, preferably a string array (items of other types will be converted to string)
1735
- * @returns {GEnumElement[]}
1772
+ * @returns {EnumElement[]}
1736
1773
  */
1737
1774
  apply(arr) {
1738
- let result = arr.map((x2) => this.mapping[x2]);
1775
+ let result = arr.map((x2) => x2 instanceof Asis ? x2 : this.mapping[x2]);
1739
1776
  result.level = this;
1740
1777
  return result;
1741
1778
  }
1742
1779
  }
1743
- function isContinuous(x2) {
1780
+ function is_continuous(x2) {
1744
1781
  return typeof x2 === "number" || x2 instanceof Number || x2 instanceof Date;
1745
1782
  }
1746
1783
  function plus$1(a, b = 0) {
@@ -1750,30 +1787,30 @@ function plus$1(a, b = 0) {
1750
1787
  }
1751
1788
  const numutils = {
1752
1789
  min(arr, { na_rm = true, infinity_rm = true } = {}) {
1753
- if (na_rm) arr = arr.filter(isContinuous);
1790
+ if (na_rm) arr = arr.filter(is_continuous);
1754
1791
  if (infinity_rm) arr = arr.filter((x2) => isFinite(x2));
1755
1792
  return Array.from(arr).reduce((a, b) => a < b ? a : b, Infinity);
1756
1793
  },
1757
1794
  max(arr, { na_rm = true, infinity_rm = true } = {}) {
1758
- if (na_rm) arr = arr.filter(isContinuous);
1795
+ if (na_rm) arr = arr.filter(is_continuous);
1759
1796
  if (infinity_rm) arr = arr.filter((x2) => isFinite(x2));
1760
1797
  return Array.from(arr).reduce((a, b) => a > b ? a : b, -Infinity);
1761
1798
  },
1762
1799
  mean(arr, { na_rm = true, infinity_rm = true } = {}) {
1763
- if (na_rm) arr = arr.filter(isContinuous);
1800
+ if (na_rm) arr = arr.filter(is_continuous);
1764
1801
  if (infinity_rm) arr = arr.filter((x2) => isFinite(x2));
1765
1802
  if (arr.length == 0) return NaN;
1766
1803
  return Array.from(arr).reduce((a, v) => a + v, 0) / arr.length;
1767
1804
  },
1768
1805
  sd(arr, { na_rm = true, infinity_rm = true } = {}) {
1769
- if (na_rm) arr = arr.filter(isContinuous);
1806
+ if (na_rm) arr = arr.filter(is_continuous);
1770
1807
  if (infinity_rm) arr = arr.filter((x2) => isFinite(x2));
1771
1808
  if (arr.length <= 1) return NaN;
1772
1809
  let mean = Array.from(arr).reduce((a, v) => a + v, 0) / arr.length;
1773
1810
  return Math.sqrt(Array.from(arr).reduce((a, v) => a + (v - mean) ** 2, 0) / (arr.length - 1));
1774
1811
  },
1775
1812
  quantile(arr, p, { na_rm = true, infinity_rm = true } = {}) {
1776
- if (na_rm) arr = arr.filter(isContinuous);
1813
+ if (na_rm) arr = arr.filter(is_continuous);
1777
1814
  if (infinity_rm) arr = arr.filter((x2) => isFinite(x2));
1778
1815
  if (arr.length == 0) return NaN;
1779
1816
  arr = Array.from(arr).sort((a, b) => a - b);
@@ -1782,7 +1819,7 @@ const numutils = {
1782
1819
  return arr[lo] * (hi - idx) + arr[hi] * (idx - lo);
1783
1820
  },
1784
1821
  extent(arr, { na_rm = true, infinity_rm = true } = {}) {
1785
- if (na_rm) arr = arr.filter(isContinuous);
1822
+ if (na_rm) arr = arr.filter(is_continuous);
1786
1823
  if (infinity_rm) arr = arr.filter((x2) => isFinite(x2));
1787
1824
  if (arr.length == 0) return new Array(2);
1788
1825
  let min = Array.from(arr).reduce((a, b) => a < b ? a : b, Infinity), max = Array.from(arr).reduce((a, b) => a > b ? a : b, -Infinity);
@@ -6590,7 +6627,7 @@ function format_datetime({
6590
6627
  function format_auto(opts) {
6591
6628
  let formatter_number = format_number(opts), formatter_datetime = format_datetime(opts);
6592
6629
  return function(x2, i, arr = [x2]) {
6593
- if (x2 instanceof GEnumElement) return String(x2);
6630
+ if (x2 instanceof EnumElement) return String(x2);
6594
6631
  if (x2 instanceof Date) return formatter_datetime(x2, i, arr);
6595
6632
  return formatter_number(x2, i, arr);
6596
6633
  };
@@ -6671,7 +6708,7 @@ class GLayer {
6671
6708
  this.$fnames = {};
6672
6709
  for (const aes in $$aes) {
6673
6710
  this.$fnames[aes] = String($$aes[aes]);
6674
- data[aes] = $$data.map($$aes[aes]);
6711
+ data[aes] = $$data.map($$aes[aes]).map((x2) => x2 instanceof String || x2 instanceof Number ? new Asis(x2) : x2);
6675
6712
  }
6676
6713
  data.$raw = $$data;
6677
6714
  let core_aes = stat?.core_attrs ?? ["x", "y", "xnudge", "ynudge"];
@@ -6722,9 +6759,9 @@ class GLayer {
6722
6759
  if ($$levels?.[aes] != null) {
6723
6760
  scale.level = $$levels[aes];
6724
6761
  } else if (values.some(is_categorical)) {
6725
- scale.level = GEnumLevel.from(values);
6762
+ scale.level = EnumLevel.from(values);
6726
6763
  } else {
6727
- scale.extent = numutils.extent(values);
6764
+ scale.extent = numutils.extent(values.filter((v) => !(v instanceof Asis)));
6728
6765
  }
6729
6766
  }
6730
6767
  this.applyScale(aes, scale);
@@ -6754,7 +6791,8 @@ class GLayer {
6754
6791
  if (scale.level) values = scale.level.apply(values);
6755
6792
  if (scale.limits) values.extent = scale.limits;
6756
6793
  scale.aes = aes;
6757
- this.data[aes] = scale(values);
6794
+ let scaled = scale(values);
6795
+ this.data[aes] = values.map((v, i) => v instanceof Asis ? v.valueOf() : scaled[i]);
6758
6796
  this.scales[aes] = scale;
6759
6797
  }
6760
6798
  applyCoordScale(coord_levels) {
@@ -6789,9 +6827,9 @@ class GPlot {
6789
6827
  if (levels?.[aes] != null) {
6790
6828
  scale.level = levels[aes];
6791
6829
  } else if (values.some(is_categorical)) {
6792
- scale.level = GEnumLevel.from(values);
6830
+ scale.level = EnumLevel.from(values);
6793
6831
  } else {
6794
- scale.extent = numutils.extent(values);
6832
+ scale.extent = numutils.extent(values.filter((v) => !(v instanceof Asis)));
6795
6833
  }
6796
6834
  }
6797
6835
  scale._title = this.layers.reduce((v, layer) => v ?? layer.$fnames?.[aes], null);
@@ -6823,17 +6861,17 @@ class GPlot {
6823
6861
  useCoordLevels(levels = {}) {
6824
6862
  for (const aes of ["x", "y"]) {
6825
6863
  if (levels[aes]) {
6826
- this.levels[aes] = new GEnumLevel(...levels[aes]);
6864
+ this.levels[aes] = new EnumLevel(...levels[aes]);
6827
6865
  } else {
6828
6866
  let values = this.layers.flatMap((layer) => {
6829
6867
  let fn = vvgeom[layer.geom].get_values ?? vvgeom[layer.geom].get_range;
6830
6868
  return fn?.(layer.$data, aes);
6831
6869
  });
6832
6870
  if (values.some(is_categorical)) {
6833
- this.levels[aes] = GEnumLevel.from(values);
6871
+ this.levels[aes] = EnumLevel.from(values);
6834
6872
  } else {
6835
6873
  values = this.layers.flatMap((layer) => vvgeom[layer.geom].get_range?.(layer.$data, aes)).filter((v) => !isNaN(v));
6836
- this.extents[aes] = numutils.extent(values);
6874
+ this.extents[aes] = numutils.extent(values.filter((v) => !(v instanceof Asis)));
6837
6875
  }
6838
6876
  }
6839
6877
  }
@@ -18032,12 +18070,22 @@ const _sfc_main$w = {
18032
18070
  setup(__props, { emit: __emit }) {
18033
18071
  const model = useModel(__props, "modelValue");
18034
18072
  const config = createPropsRestProxy(__props, ["coord2pos", "pos2coord", "layout", "theme", "flip", "activeTransform", "transition"]);
18073
+ function normalize2({ xmin, xmax, ymin, ymax, xreverse = false, yreverse = false } = {}) {
18074
+ if ([xmin, xmax, ymin, ymax].every((v) => v == null)) return null;
18075
+ if (xmin != null && xmax != null && xmin > xmax) [xmin, xmax, xreverse] = [xmax, xmin, !xreverse];
18076
+ if (ymin != null && ymax != null && ymin > ymax) [ymin, ymax, yreverse] = [ymax, ymin, !yreverse];
18077
+ let coord = { xreverse, yreverse };
18078
+ if (xmin != null && isFinite(xmin)) coord.xmin = xmin;
18079
+ if (xmax != null && isFinite(xmax)) coord.xmax = xmax;
18080
+ if (ymin != null && isFinite(ymin)) coord.ymin = ymin;
18081
+ if (ymax != null && isFinite(ymax)) coord.ymax = ymax;
18082
+ return coord;
18083
+ }
18035
18084
  const position = computed(() => {
18036
18085
  if (model.value == null || model.value.hidden) return null;
18037
- if (["xmin", "xmax", "ymin", "ymax"].every((k) => model.value?.[k] == null)) return null;
18038
- let { hmin, hmax, vmin, vmax } = __props.coord2pos(model.value, { limited: true }), pos = __props.coord2pos(model.value);
18039
- if (hmin != null && hmax != null) [hmin, hmax] = [hmin, hmax].sort((a, b) => a - b);
18040
- if (vmin != null && vmax != null) [vmin, vmax] = [vmin, vmax].sort((a, b) => a - b);
18086
+ let coord = normalize2(model.value);
18087
+ if (coord == null) return null;
18088
+ let { hmin, hmax, vmin, vmax } = __props.coord2pos(coord, { limited: true }), pos = __props.coord2pos(coord);
18041
18089
  return { hmin, hmax, vmin, vmax, pos };
18042
18090
  });
18043
18091
  const transform = computed(() => {
@@ -18150,7 +18198,8 @@ const _sfc_main$w = {
18150
18198
  e.stopPropagation();
18151
18199
  e.preventDefault();
18152
18200
  e.target.setPointerCapture(e.pointerId);
18153
- let pos0 = __props.coord2pos(model.value);
18201
+ let { xreverse: $xreverse, yreverse: $yreverse, ...coord0 } = normalize2(model.value);
18202
+ let pos0 = __props.coord2pos(coord0);
18154
18203
  let { h: coordL, v: coordT } = __props.pos2coord({ h: pos0.hmin, v: pos0.vmin }), { h: coordR, v: coordB } = __props.pos2coord({ h: pos0.hmax, v: pos0.vmax });
18155
18204
  let { hmin: hmin0, hmax: hmax0, vmin: vmin0, vmax: vmax0 } = pos0;
18156
18205
  let pointerMoved = false;
@@ -18217,10 +18266,10 @@ const _sfc_main$w = {
18217
18266
  let res = {
18218
18267
  xmin,
18219
18268
  xmax,
18220
- xreverse: Boolean(xreverse ^ model.value.xreverse),
18269
+ xreverse: Boolean(xreverse ^ $xreverse),
18221
18270
  ymin,
18222
18271
  ymax,
18223
- yreverse: Boolean(yreverse ^ model.value.yreverse)
18272
+ yreverse: Boolean(yreverse ^ $yreverse)
18224
18273
  }, event = new PointerEvent("select", e);
18225
18274
  model.value = dropNull(res);
18226
18275
  emit("selectend");
@@ -18459,13 +18508,13 @@ const _sfc_main$s = {
18459
18508
  },
18460
18509
  setup(__props) {
18461
18510
  const guides = computed(() => {
18462
- let guide_scales = new Map(Array.from(__props.scales).filter(([fn]) => fn.legend !== false));
18511
+ let guide_scales = new Map(Array.from(__props.scales).filter(([fn]) => fn.legend !== false && fn.limits?.min != fn.limits?.max));
18463
18512
  let scale_fns = Array.from(guide_scales.keys());
18464
18513
  let keys = scale_fns.map((s) => s.key);
18465
18514
  let n = 1;
18466
18515
  for (let i = 0; i < keys.length; i++) {
18467
18516
  while (keys.includes("_guide_" + n)) n++;
18468
- keys[i] ??= keys.find((_, j) => keys[j] != null && GEnumLevel.isEqual(scale_fns[j].level, scale_fns[i].level)) ?? "_guide_" + n;
18517
+ keys[i] ??= keys.find((_, j) => keys[j] != null && EnumLevel.isEqual(scale_fns[j].level, scale_fns[i].level)) ?? "_guide_" + n;
18469
18518
  }
18470
18519
  let types = scale_fns.map((s) => getGuideType(s));
18471
18520
  let groups = interaction(keys, types);