@opendata-ai/openchart-engine 2.8.1 → 2.9.1

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
@@ -21,6 +21,26 @@ var DARK_TEXT_FILL = "#d1d5db";
21
21
  var LIGHT_REFLINE_STROKE = "#888888";
22
22
  var DARK_REFLINE_STROKE = "#9ca3af";
23
23
  var ANCHOR_OFFSET = 8;
24
+ function interpolateInDomain(numValue, domain, positionOf) {
25
+ if (domain.length < 2) return null;
26
+ const nums = domain.map(Number);
27
+ if (!nums.every(Number.isFinite)) return null;
28
+ const sorted = nums.map((n, i) => ({ n, i })).sort((a, b) => a.n - b.n);
29
+ let lower = 0;
30
+ let upper = sorted.length - 1;
31
+ for (let i = 0; i < sorted.length; i++) {
32
+ if (sorted[i].n <= numValue) lower = i;
33
+ if (sorted[i].n >= numValue) {
34
+ upper = i;
35
+ break;
36
+ }
37
+ }
38
+ const lowerPos = positionOf(domain[sorted[lower].i]);
39
+ const upperPos = positionOf(domain[sorted[upper].i]);
40
+ if (lower === upper) return lowerPos;
41
+ const t = (numValue - sorted[lower].n) / (sorted[upper].n - sorted[lower].n);
42
+ return lowerPos + t * (upperPos - lowerPos);
43
+ }
24
44
  function resolvePosition(value, scale) {
25
45
  if (!scale) return null;
26
46
  const s = scale.scale;
@@ -37,15 +57,28 @@ function resolvePosition(value, scale) {
37
57
  }
38
58
  if (type === "band") {
39
59
  const bandScale = s;
40
- const pos = bandScale(String(value));
41
- if (pos === void 0) return null;
42
- return pos + (bandScale.bandwidth?.() ?? 0) / 2;
60
+ const strValue2 = String(value);
61
+ const pos = bandScale(strValue2);
62
+ if (pos !== void 0) return pos + (bandScale.bandwidth?.() ?? 0) / 2;
63
+ const bw = bandScale.bandwidth?.() ?? 0;
64
+ return interpolateInDomain(
65
+ Number(strValue2),
66
+ bandScale.domain(),
67
+ (entry) => (bandScale(entry) ?? 0) + bw / 2
68
+ );
43
69
  }
44
- try {
45
- return s(String(value));
46
- } catch {
47
- return null;
70
+ const strValue = String(value);
71
+ const directResult = s(strValue);
72
+ if (directResult !== void 0) return directResult;
73
+ if (type === "point" || type === "ordinal") {
74
+ const domain = s.domain();
75
+ return interpolateInDomain(
76
+ Number(strValue),
77
+ domain,
78
+ (entry) => s(entry) ?? 0
79
+ );
48
80
  }
81
+ return null;
49
82
  }
50
83
  function makeAnnotationLabelStyle(fontSize, fontWeight, fill, isDark) {
51
84
  const defaultFill = isDark ? DARK_TEXT_FILL : LIGHT_TEXT_FILL;