@bino-bi/bn-template-engine 1.0.0-next.35 → 1.0.0-next.37

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.
@@ -1,4 +1,4 @@
1
- import { h, Host } from "@stencil/core";
1
+ import { forceUpdate, h, Host } from "@stencil/core";
2
2
  import { OrderDirection, TableRowType, TableType } from "../utils/model";
3
3
  import { dataPointAttributes, measure, measureContent, measureSign, readLayoutPoint, } from "../../../utils/layoutState";
4
4
  import { Table } from "../utils/Table";
@@ -9,13 +9,13 @@ import { StringAdapter } from "../../../utils/helper/StringAdapter";
9
9
  import { IBCSRuleSet, stylesBySlot } from "../../../utils/ibcsRuleSet";
10
10
  import { DataFormatDigits, DataFormatDigitsAdapter } from "../../../utils/model/DataFormatDigits";
11
11
  import { OrderAdapter } from "../../../utils/model/Order";
12
- import { CellBarChart } from "../utils/elements/CellBarChart";
12
+ import { CellBarChart, formatValue, zeroOnPositiveSide, } from "../utils/elements/CellBarChart";
13
13
  import { WaterfallBar } from "../utils/elements/WaterfallBar";
14
14
  import withComponentStatus from "../../../utils/withComponentStatus";
15
15
  import withComponentStyle from "../../../utils/withComponentStyle";
16
16
  import { getStores } from "../../../context/context-protocol";
17
17
  import { sourceExpr } from "../../../context/scopedDb";
18
- import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, relativePinGeometry } from "../utils/helper/barColumnGeometry";
18
+ import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, labelWidth, PIN_HEAD_SIZE, pinLabelsFit, relativePinGeometry, } from "../utils/helper/barColumnGeometry";
19
19
  import { NOT_AVAILABLE } from "../../../utils/relativeVariance";
20
20
  import { normalizeFillPattern } from "../../../utils/model/styles/SVGStyle";
21
21
  /**
@@ -108,9 +108,23 @@ export class BnTableRenderer {
108
108
  /** @internal */
109
109
  this.scaleFactor = 1;
110
110
  this.componentDataFormatDigits = new DataFormatDigits();
111
+ this.barColumnLayouts = new Map();
112
+ // labels measured in the fallback font are too narrow or too wide once the web font is there: lay out again
113
+ this.relayout = () => {
114
+ if (this.barColumnLayouts.size > 0)
115
+ forceUpdate(this);
116
+ };
111
117
  withComponentStatus(this);
112
118
  this.componentStyle = withComponentStyle(this);
113
119
  }
120
+ connectedCallback() {
121
+ var _a;
122
+ (_a = document.fonts) === null || _a === void 0 ? void 0 : _a.addEventListener("loadingdone", this.relayout);
123
+ }
124
+ disconnectedCallback() {
125
+ var _a;
126
+ (_a = document.fonts) === null || _a === void 0 ? void 0 : _a.removeEventListener("loadingdone", this.relayout);
127
+ }
114
128
  /**
115
129
  * Collects the table's layout-state contribution: header/body regions,
116
130
  * counts, and - with `detail: "full"` - per-cell geometry paired with the
@@ -228,14 +242,15 @@ export class BnTableRenderer {
228
242
  return h("div", { class: "empty-state" }, errorMsg);
229
243
  }
230
244
  const locale = (_d = (_c = (_b = this.stores) === null || _b === void 0 ? void 0 : _b.context) === null || _c === void 0 ? void 0 : _c.state) === null || _d === void 0 ? void 0 : _d.locale;
231
- return (h("table", null, h("thead", null, this.table.getHeader().map((row) => (h("tr", { class: row.cssStyle }, row.getCells().map((cell) => (h("th", { colSpan: cell.colSpan, class: cell.cssStyle }, cell.getAsHeaderList().map((line, i, arr) => (h("span", null, i === arr.length - 1 ? (h("span", { class: line === " " ? "table-footer" : "" }, line, cell.drawPrefix(), " ")) : (h("span", null, line, cell.drawPrefix(), h("br", null))))))))))))), h("tbody", null, this.table.getBody().map((row) => (h("tr", Object.assign({ class: row.cssStyle }, dataPointAttributes(row.key, "element")), row === null || row === void 0 ? void 0 : row.getCells().map((cell) => (h("td", Object.assign({ colSpan: cell.colSpan, class: cell.type ? `${cell.cssStyle} ${cell.type}` : cell.cssStyle }, (row.key && cell.value != null && cell.value !== "" && cell.value !== "-"
232
- ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
233
- : {})), cell.prefix != null ? (h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
234
- cell.columnStats &&
235
- cell.value !== NOT_AVAILABLE &&
236
- (cell.waterfall !== undefined || row.type !== TableRowType.SUM) ? (cell.waterfall !== undefined ? (h(WaterfallBar, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: false, isPositiveGood: cell.direction === "pos", measureScale: cell.measureScale, locale: locale, columnWidthEm: this.barColumnMaxWidth, styles: this.barChartStyles, waterfall: cell.waterfall })) : (h(CellBarChart, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: cell.key.startsWith("dr"), isPositiveGood: cell.direction === "pos",
237
- // percentage points: the fraction times 100, no measure scale
238
- measureScale: cell.type === "percentpoint" ? 0.01 : cell.measureScale, locale: locale, percentDigits: this.componentDataFormatDigits.percent, columnWidthEm: this.barColumnMaxWidth, styles: this.barChartStyles }))) : (cell.draw(this.componentDataFormatDigits, locale)))))))))));
245
+ return (h("table", null, h("thead", null, this.table.getHeader().map((row) => (h("tr", { class: row.cssStyle }, row.getCells().map((cell) => (h("th", { colSpan: cell.colSpan, class: cell.cssStyle }, cell.getAsHeaderList().map((line, i, arr) => (h("span", null, i === arr.length - 1 ? (h("span", { class: line === " " ? "table-footer" : "" }, line, cell.drawPrefix(), " ")) : (h("span", null, line, cell.drawPrefix(), h("br", null))))))))))))), h("tbody", null, this.table.getBody().map((row) => (h("tr", Object.assign({ class: row.cssStyle }, dataPointAttributes(row.key, "element")), row === null || row === void 0 ? void 0 : row.getCells().map((cell) => {
246
+ var _a, _b, _c, _d, _e;
247
+ return (h("td", Object.assign({ colSpan: cell.colSpan, class: cell.type ? `${cell.cssStyle} ${cell.type}` : cell.cssStyle }, (row.key && cell.value != null && cell.value !== "" && cell.value !== "-"
248
+ ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
249
+ : {})), cell.prefix != null ? (h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
250
+ cell.columnStats &&
251
+ cell.value !== NOT_AVAILABLE &&
252
+ (cell.waterfall !== undefined || row.type !== TableRowType.SUM) ? (cell.waterfall !== undefined ? (h(WaterfallBar, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: false, isPositiveGood: cell.direction === "pos", measureScale: cell.measureScale, locale: locale, columnWidthEm: this.barColumnMaxWidth, styles: this.barChartStyles, waterfall: cell.waterfall })) : (h(CellBarChart, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: cell.key.startsWith("dr"), isPositiveGood: cell.direction === "pos", measureScale: barLabelMeasureScale(cell), locale: locale, percentDigits: (_b = (_a = this.barColumnLayouts.get(cell.key)) === null || _a === void 0 ? void 0 : _a.percentDigits) !== null && _b !== void 0 ? _b : this.componentDataFormatDigits.percent, percentSign: (_c = this.barColumnLayouts.get(cell.key)) === null || _c === void 0 ? void 0 : _c.percentSign, columnWidthEm: this.barColumnMaxWidth, labelWidths: (_d = this.barColumnLayouts.get(cell.key)) === null || _d === void 0 ? void 0 : _d.labels, sideMax: (_e = this.barColumnLayouts.get(cell.key)) === null || _e === void 0 ? void 0 : _e.sideMax, styles: this.barChartStyles }))) : (cell.draw(this.componentDataFormatDigits, locale))));
253
+ })))))));
239
254
  };
240
255
  return h(Host, { class: { "no-data": ((_a = this.table) === null || _a === void 0 ? void 0 : _a.hasNoData()) === true } }, helper());
241
256
  }
@@ -280,6 +295,7 @@ export class BnTableRenderer {
280
295
  else {
281
296
  (_h = this.errors) === null || _h === void 0 ? void 0 : _h.remove("WARN_orderBucket");
282
297
  }
298
+ this.barColumnLayouts = this.collectBarColumnLayouts();
283
299
  this.barChartStyles = this.prepareBarChartStyles();
284
300
  }
285
301
  prepareConfig(db) {
@@ -334,7 +350,7 @@ export class BnTableRenderer {
334
350
  });
335
351
  }
336
352
  return this.componentStyle(($) => {
337
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
353
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
338
354
  const varianceStyles = (_a = $["bn-chart-time"]) === null || _a === void 0 ? void 0 : _a["varianceStyles"];
339
355
  const barStyles = (_b = $["bn-chart-time"]) === null || _b === void 0 ? void 0 : _b["barStyles"];
340
356
  // Default variance styles with fillpattern
@@ -388,13 +404,17 @@ export class BnTableRenderer {
388
404
  fontColor: ((_z = $["global"]) === null || _z === void 0 ? void 0 : _z["fontColor"]) || "#000000",
389
405
  fontSize: (((_0 = $["global"]) === null || _0 === void 0 ? void 0 : _0["fontSizePx"]) || 13.3333) * BAR_LABEL_FONT_RATIO * this.scaleFactor,
390
406
  emPx: (((_1 = $["global"]) === null || _1 === void 0 ? void 0 : _1["fontSizePx"]) || 13.3333) * this.scaleFactor,
391
- fontFamily: ((_2 = $["global"]) === null || _2 === void 0 ? void 0 : _2["fontFamily"]) || "'Noto Sans', sans-serif",
407
+ fontFamily: this.labelFontFamily(),
392
408
  unitScaling: resolvedUnit,
393
409
  percentageScaling: resolvedPercent,
394
410
  scaleFactor: this.scaleFactor,
395
411
  };
396
412
  });
397
413
  }
414
+ /** Font family of the bar and pin labels, also used to measure them. */
415
+ labelFontFamily() {
416
+ return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontFamily"]) || "'Noto Sans', sans-serif"; });
417
+ }
398
418
  /** The table's true em in px - the unit `.category-wN` and the bar columns share. */
399
419
  emPx() {
400
420
  return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontSizePx"]) || 13.3333; }) * this.scaleFactor;
@@ -402,64 +422,107 @@ export class BnTableRenderer {
402
422
  /**
403
423
  * Auto-calculate unitScaling so the largest absolute variance bar
404
424
  * fills ~80% of its side of the bar area, without tripping the overdrive break.
425
+ * One scaling for all columns, so it is the smallest that fits every column.
405
426
  */
406
427
  autoUnitScaling() {
407
428
  const emPx = this.emPx();
408
429
  const fs = emPx * BAR_LABEL_FONT_RATIO;
409
- const { absMax, hasNeg, hasPos } = this.getAbsBarColumnStats();
410
- if (absMax <= 0 || fs <= 0) {
411
- return 0.01;
430
+ let scaling = Infinity;
431
+ for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
432
+ if (isRelative || absMax <= 0)
433
+ continue;
434
+ const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, labels);
435
+ scaling = Math.min(scaling, Math.min(maxBarWidth * 0.8, cutWidth) / (absMax * fs));
412
436
  }
413
- const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
414
- const targetWidth = Math.min(maxBarWidth * 0.8, cutWidth);
415
- return targetWidth / (absMax * fs);
437
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.01;
416
438
  }
417
439
  /**
418
- * Auto-calculate percentageScaling so the largest relative variance pin
419
- * fills ~80% of the pin area.
440
+ * Auto-calculate percentageScaling so the longest relative variance pin, its head and
441
+ * its label exactly fill the column. One scaling for all columns, the smallest that fits.
420
442
  */
421
443
  autoPercentageScaling() {
444
+ var _a, _b;
422
445
  const emPx = this.emPx();
423
446
  const fs = emPx * BAR_LABEL_FONT_RATIO;
424
- const { absMax: maxPct, hasNeg, hasPos } = this.getRelativeBarColumnStats();
425
- if (maxPct <= 0 || fs <= 0) {
426
- return 0.5;
447
+ let scaling = Infinity;
448
+ for (const { isRelative, absMax, labels, sideMax } of this.barColumnLayouts.values()) {
449
+ if (!isRelative || absMax <= 0)
450
+ continue;
451
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
452
+ // the longest pins of both sides share the room, each side takes what its pins need
453
+ const { negPinLength, posPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
454
+ const percent = (((_a = sideMax.neg) !== null && _a !== void 0 ? _a : 0) + ((_b = sideMax.pos) !== null && _b !== void 0 ? _b : 0)) * 100;
455
+ scaling = Math.min(scaling, (negPinLength + posPinLength) / (percent * fs));
427
456
  }
428
- const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
429
- return (maxPinLength * 0.8) / (maxPct * fs);
430
- }
431
- /**
432
- * Maximum value and sign coverage across all absolute variance columns (d_*) in the table body.
433
- * Sign coverage decides how much of the column the bar area gets (see barColumnGeometry.ts);
434
- * OR-ing it across columns keeps the single shared scaling inside the narrowest bar area.
435
- */
436
- getAbsBarColumnStats() {
437
- // waterfall cells scale independently and must not skew variance auto-scaling
438
- return this.collectBarColumnStats((cell) => !cell.key.startsWith("dr") && !cell.waterfall, 1);
457
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.5;
439
458
  }
440
459
  /**
441
- * Same for the relative variance columns (dr_*). Values are stored as decimals
442
- * (e.g. 0.2 = 20%), returned as percentage (20).
460
+ * Largest value and widest label per side of every bar and pin column, from the cells
461
+ * that draw one: total rows and n/a draw none. Interval buckets share the column key,
462
+ * so they share the layout and the axis.
443
463
  */
444
- getRelativeBarColumnStats() {
445
- return this.collectBarColumnStats((cell) => cell.key.startsWith("dr"), 100);
446
- }
447
- collectBarColumnStats(accept, valueFactor) {
448
- const stats = { absMax: 0, hasNeg: false, hasPos: false };
449
- if (!this.table)
450
- return stats;
464
+ collectBarColumnLayouts() {
465
+ var _a, _b, _c, _d;
466
+ const layouts = new Map();
467
+ if (!this.table || this.table.hasNoData())
468
+ return layouts;
469
+ const cellsByKey = new Map();
451
470
  for (const row of this.table.getBody()) {
452
471
  if (row.type === TableRowType.SUM)
453
472
  continue;
454
473
  for (const cell of row.getCells()) {
455
- if (!cell.isBarChart || !cell.columnStats || !accept(cell))
474
+ // waterfall cells scale independently
475
+ if (!cell.isBarChart || !cell.columnStats || cell.waterfall !== undefined)
476
+ continue;
477
+ if (typeof cell.value !== "number" || isNaN(cell.value))
456
478
  continue;
457
- stats.absMax = Math.max(stats.absMax, cell.columnStats.absMax * valueFactor);
458
- stats.hasNeg = stats.hasNeg || cell.columnStats.min < 0;
459
- stats.hasPos = stats.hasPos || cell.columnStats.max > 0;
479
+ cellsByKey.set(cell.key, [...((_a = cellsByKey.get(cell.key)) !== null && _a !== void 0 ? _a : []), cell]);
480
+ }
481
+ }
482
+ const emPx = this.emPx();
483
+ const fs = emPx * BAR_LABEL_FONT_RATIO;
484
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
485
+ const locale = (_d = (_c = (_b = this.stores) === null || _b === void 0 ? void 0 : _b.context) === null || _c === void 0 ? void 0 : _c.state) === null || _d === void 0 ? void 0 : _d.locale;
486
+ const fontFamily = this.labelFontFamily();
487
+ for (const [key, cells] of cellsByKey) {
488
+ const isRelative = key.startsWith("dr");
489
+ const values = cells.map((cell) => cell.value);
490
+ const measure = (percentDigits, percentSign) => {
491
+ var _a;
492
+ const labels = {
493
+ neg: values.some((value) => value < 0) ? 0 : undefined,
494
+ pos: values.some((value) => value > 0) ? 0 : undefined,
495
+ };
496
+ const zeroPositive = zeroOnPositiveSide(labels);
497
+ for (const cell of cells) {
498
+ const value = cell.value;
499
+ const side = value > 0 || (value === 0 && zeroPositive) ? "pos" : "neg";
500
+ const text = formatValue(value, isRelative, barLabelMeasureScale(cell), locale, percentDigits, percentSign);
501
+ labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, labelWidth(text, fs, fontFamily, isRelative));
502
+ }
503
+ return labels;
504
+ };
505
+ // a narrow pin column drops percent digits, then the %, so every label keeps its sign
506
+ let percentDigits = Math.min(20, Math.max(0, Math.trunc(this.componentDataFormatDigits.percent) || 0));
507
+ let percentSign = true;
508
+ let labels = measure(percentDigits, percentSign);
509
+ while (isRelative && (percentDigits > 0 || percentSign)) {
510
+ if (pinLabelsFit(this.barColumnMaxWidth, emPx, labels, headSize))
511
+ break;
512
+ if (percentDigits > 0)
513
+ percentDigits--;
514
+ else
515
+ percentSign = false;
516
+ labels = measure(percentDigits, percentSign);
460
517
  }
518
+ const absMax = Math.max(...values.map(Math.abs)) * (isRelative ? 100 : 1);
519
+ const sideMax = {
520
+ neg: labels.neg === undefined ? undefined : Math.max(0, ...values.map((value) => -value)),
521
+ pos: labels.pos === undefined ? undefined : Math.max(0, ...values),
522
+ };
523
+ layouts.set(key, { isRelative, absMax, labels, sideMax, percentDigits, percentSign });
461
524
  }
462
- return stats;
525
+ return layouts;
463
526
  }
464
527
  static get is() { return "bn-table-renderer"; }
465
528
  static get encapsulation() { return "shadow"; }
@@ -1388,3 +1451,7 @@ export class BnTableRenderer {
1388
1451
  }
1389
1452
  static get elementRef() { return "element"; }
1390
1453
  }
1454
+ /** Percentage points: the fraction times 100, no measure scale. */
1455
+ function barLabelMeasureScale(cell) {
1456
+ return cell.type === "percentpoint" ? 0.01 : cell.measureScale;
1457
+ }
@@ -1,7 +1,7 @@
1
1
  import { h } from "@stencil/core";
2
2
  import { Formatter } from "../../../../utils";
3
3
  import { VarianceScenario } from "../../../../utils/model/VarianceScenario";
4
- import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, relativePinGeometry } from "../helper/barColumnGeometry";
4
+ import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, labelWidth, PIN_HEAD_SIZE, relativePinGeometry, VARIANCE_AXIS_EM, } from "../helper/barColumnGeometry";
5
5
  /** Matches SYSTEM_STYLE_DEFAULTS.global.fontSizePx; only used when `styles` is omitted. */
6
6
  const DEFAULT_EM_PX = 13.3333;
7
7
  // Default fallback colors (should come from componentStyle)
@@ -24,13 +24,8 @@ export const DEFAULT_STYLES = {
24
24
  scaleFactor: 1,
25
25
  };
26
26
  export const AXIS_COLOR = "#666666";
27
- /** Width of the axis of a variance bar or pin, in em: near the 0.3em scenario marker in the header. */
28
- const VARIANCE_AXIS_EM = 0.25;
29
- /** Side of the square pin head and width of the pin line, in px before scaleFactor. */
30
- const PIN_HEAD_SIZE = 10;
27
+ /** Width of the pin line, in px before scaleFactor. */
31
28
  const PIN_LINE_WIDTH = 3;
32
- /** Rough average glyph width, in label font sizes, to tell if an outlier label fits inside its pin. */
33
- const LABEL_CHAR_WIDTH = 0.6;
34
29
  /**
35
30
  * Creates a unique pattern ID for the SVG defs
36
31
  */
@@ -55,16 +50,20 @@ export const PatternDefs = ({ styles }) => {
55
50
  ];
56
51
  return (h("defs", null, patterns.map((p) => (h("pattern", { id: p.id, patternUnits: "userSpaceOnUse", width: patternSize, height: patternSize }, h("rect", { width: patternSize, height: patternSize, fill: "#ffffff" }), h("line", { x1: "0", y1: patternSize, x2: patternSize, y2: "0", stroke: p.stroke, "stroke-width": 1.5 * s }))))));
57
52
  };
58
- /** Characters that fit the pin label gutter (PIN_LABEL_EM), the cell cuts off the rest. */
53
+ /** Longest relative label that keeps all percent digits; a longer one drops digits so its gutter does not eat the pins. */
59
54
  const PIN_LABEL_CHARS = 7;
60
55
  /**
61
56
  * Formats a number for display with appropriate SI prefix.
62
57
  */
63
- export function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0) {
64
- const prefix = value >= 0 ? "+" : "";
58
+ export function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0, percentSign = true) {
59
+ // IBCS prints a value that rounds to zero without a sign
60
+ const signed = (text, zero) => (value > 0 && text !== zero ? "+" : "") + text;
65
61
  if (isRelative) {
66
62
  // Relative variance is stored as decimal (e.g., -2.0 = -200%)
67
- const format = (digits) => prefix + Formatter.number(value, "percent", digits, digits, 1, locale);
63
+ const number = (v, digits) => percentSign
64
+ ? Formatter.number(v, "percent", digits, digits, 1, locale)
65
+ : Formatter.number(v * 100, "decimal", digits, digits, 1, locale);
66
+ const format = (digits) => signed(number(value, digits), number(0, digits));
68
67
  // Intl throws on a digit count outside 0 to 20
69
68
  let digits = Math.min(20, Math.max(0, Math.trunc(percentDigits) || 0));
70
69
  while (digits > 0 && format(digits).length > PIN_LABEL_CHARS) {
@@ -91,7 +90,7 @@ export function formatValue(value, isRelative, measureScale = 1, locale, percent
91
90
  suffix = "K";
92
91
  }
93
92
  const digits = absValue >= unit * 10 ? 0 : 1;
94
- return prefix + Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale) + suffix;
93
+ return (signed(Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale), Formatter.number(0, "decimal", digits, 0, 1, locale)) + suffix);
95
94
  }
96
95
  /**
97
96
  * Checks if the column is a scenario column (ac, pp, fc, pl) vs variance (d_, dr_).
@@ -157,6 +156,17 @@ const VarianceAxis = ({ x, height, look, styles }) => {
157
156
  }
158
157
  return h("g", { class: `variance-axis axis-${referenceSlot}` }, lines);
159
158
  };
159
+ /** A zero value is drawn on the positive side, unless the column only has negative values. */
160
+ export function zeroOnPositiveSide(labels) {
161
+ return labels.pos !== undefined || labels.neg === undefined;
162
+ }
163
+ /** Side coverage from the column statistics, with only this cell's label as the width. */
164
+ function ownLabelWidths(columnStats, width) {
165
+ return {
166
+ neg: columnStats.min < 0 ? width : undefined,
167
+ pos: columnStats.max > 0 ? width : undefined,
168
+ };
169
+ }
160
170
  /**
161
171
  * Gets the variance style based on value and sentiment direction.
162
172
  * pos suffix = positive values are good (green), negative are bad (red)
@@ -245,13 +255,12 @@ function renderZigzagBreak(breakX, breakGap, yTop, barHeight, s) {
245
255
  * exactly `columnWidthEm` em - see barColumnGeometry.ts.
246
256
  */
247
257
  const AbsoluteVarianceBar = (props) => {
248
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
258
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
249
259
  const s = styles.scaleFactor;
250
260
  const fs = styles.fontSize;
251
- // Adaptive axis placement based on data range
252
- const hasNeg = columnStats.min < 0;
253
- const hasPos = columnStats.max > 0;
254
- const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, hasNeg, hasPos);
261
+ const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
262
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, false));
263
+ const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, sides);
255
264
  // Direct unit scaling via fontSize
256
265
  const scaledWidth = Math.abs(value) * styles.unitScaling * fs;
257
266
  // auto scaling puts the largest bar on the cut, float rounding must not turn it into an overdrive bar
@@ -264,7 +273,7 @@ const AbsoluteVarianceBar = (props) => {
264
273
  const padding = 2 * s;
265
274
  const barHeight = height - padding * 2;
266
275
  const barY = padding;
267
- const isPositive = value >= 0;
276
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
268
277
  // Get style based on column type (scenario vs variance)
269
278
  const look = getVarianceLook(scenario, styles);
270
279
  const sentimentStyle = getBarStyle(scenario, value, isPositiveGood, styles);
@@ -284,7 +293,6 @@ const AbsoluteVarianceBar = (props) => {
284
293
  const fillValue = getBarFill(barStyle, patternPrefix);
285
294
  // a hatched variance bar keeps an outline, the shared getBarStroke also serves the waterfall
286
295
  const { stroke: barStroke, strokeWidth: barStrokeWidth } = look && barStyle.fillpattern === "hatched" ? { stroke: barStyle.stroke, strokeWidth: 1 } : getBarStroke(barStyle);
287
- const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
288
296
  // bars start at the outer edge of the thick variance axis
289
297
  const axisOffset = look ? (VARIANCE_AXIS_EM * styles.emPx) / 2 : 0;
290
298
  const rootX = isPositive ? axisX + axisOffset : axisX - axisOffset;
@@ -301,7 +309,7 @@ const AbsoluteVarianceBar = (props) => {
301
309
  * Renders a relative variance pin chart (dr_ prefix).
302
310
  * Line from the axis in the sentiment colour, ending in a square head in the look of the
303
311
  * minuend, with the value label beside the head like an absolute bar label. A pin beyond
304
- * the pin area is cut and ends in an arrow, its label inside the pin before the arrow.
312
+ * the pin area is cut and ends in an arrow, its label after the arrow tip.
305
313
  *
306
314
  * Layout: [left label area][pin area with axis][right label area], summing to exactly
307
315
  * `columnWidthEm` em - see barColumnGeometry.ts.
@@ -309,40 +317,34 @@ const AbsoluteVarianceBar = (props) => {
309
317
  */
310
318
  const RelativeVariancePin = (props) => {
311
319
  var _a, _b;
312
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
320
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, sideMax, styles = DEFAULT_STYLES, } = props;
313
321
  const s = styles.scaleFactor;
314
322
  const fs = styles.fontSize;
315
- // Adaptive axis placement based on data range
316
- const hasNeg = columnStats.min < 0;
317
- const hasPos = columnStats.max > 0;
318
- const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, hasNeg, hasPos);
323
+ const headSize = PIN_HEAD_SIZE * s;
324
+ const pinPx = 100 * styles.percentageScaling * fs;
325
+ const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits, percentSign);
326
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, true));
327
+ const { neg = Math.max(0, -columnStats.min), pos = Math.max(0, columnStats.max) } = sideMax !== null && sideMax !== void 0 ? sideMax : {};
328
+ const { totalWidth, negPinLength, posPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize, { neg: neg * pinPx, pos: pos * pinPx });
319
329
  const centerY = height / 2;
320
330
  // Direct unit scaling via fontSize
321
- const percentValue = Math.abs(value) * 100;
322
- const scaledLength = percentValue * styles.percentageScaling * fs;
323
- const isExtreme = scaledLength > maxPinLength;
331
+ const scaledLength = Math.abs(value) * pinPx;
332
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
333
+ const maxPinLength = isPositive ? posPinLength : negPinLength;
334
+ // auto scaling puts the longest pin on the limit, float rounding must not cut it
335
+ const isExtreme = scaledLength - maxPinLength > 1e-6;
324
336
  const pinLength = Math.min(scaledLength, maxPinLength);
325
- // a zero label goes to the side that has a label gutter
326
- const isPositive = value > 0 || (value === 0 && (hasPos || !hasNeg));
327
337
  const direction = isPositive ? 1 : -1;
328
338
  const pinEndX = axisX + direction * pinLength;
329
339
  // Get style based on column type (scenario vs variance)
330
340
  const barStyle = getBarStyle(scenario, value, isPositiveGood, styles);
331
341
  const look = getVarianceLook(scenario, styles);
332
342
  const headStyle = (_a = look === null || look === void 0 ? void 0 : look.minuend) !== null && _a !== void 0 ? _a : styles.scenarios.ac;
333
- const headSize = PIN_HEAD_SIZE * s;
334
- const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits);
335
- // the arrow tip lands on the outer edge of a full-length head
343
+ // the arrow tip lands on the outer edge of a full-length head, so the label after it stays in the cell
336
344
  const tipX = axisX + direction * (maxPinLength + headSize / 2);
337
345
  const arrowBaseX = tipX - direction * headSize;
338
- const labelFitsInside = maxPinLength - headSize / 2 - labelGap >= displayValue.length * LABEL_CHAR_WIDTH * fs;
339
- const labelInside = isExtreme && labelFitsInside;
340
- let labelX = pinEndX + direction * (headSize / 2 + labelGap);
341
- if (isExtreme) {
342
- labelX = labelInside ? arrowBaseX - direction * labelGap : tipX + direction * labelGap;
343
- }
344
- const labelAtStart = isPositive !== labelInside;
345
- return (h("svg", { width: totalWidth, height: height, class: "cell-bar-chart relative-variance", style: { overflow: "visible" } }, h(PatternDefs, { styles: styles }), look ? (h(VarianceAxis, { x: axisX, height: height, look: look, styles: styles })) : (h("line", { x1: axisX, y1: 0, x2: axisX, y2: height, stroke: AXIS_COLOR, "stroke-width": 1 * s })), h("line", { x1: axisX, y1: centerY, x2: isExtreme ? arrowBaseX : pinEndX, y2: centerY, stroke: barStyle.fill, "stroke-width": PIN_LINE_WIDTH * s }), isExtreme ? (h("polygon", { points: `${tipX},${centerY} ${arrowBaseX},${centerY - headSize / 2} ${arrowBaseX},${centerY + headSize / 2}`, fill: barStyle.fill })) : (h("rect", { x: pinEndX - headSize / 2, y: centerY - headSize / 2, width: headSize, height: headSize, fill: headStyle.fillpattern === "outlined" ? "#ffffff" : getBarFill(headStyle, (_b = look === null || look === void 0 ? void 0 : look.minuendSlot) !== null && _b !== void 0 ? _b : "ac"), stroke: headStyle.stroke, "stroke-width": 1 * s })), h("text", Object.assign({ x: labelX, y: height / 2, dy: "0.35em", "text-anchor": labelAtStart ? "start" : "end", fill: styles.fontColor, "font-size": fs, "font-style": "italic", "font-family": styles.fontFamily }, (labelInside ? { stroke: "#ffffff", "stroke-width": 3 * s, "paint-order": "stroke" } : {})), displayValue)));
346
+ const labelX = (isExtreme ? tipX : pinEndX + direction * (headSize / 2)) + direction * labelGap;
347
+ return (h("svg", { width: totalWidth, height: height, class: "cell-bar-chart relative-variance", style: { overflow: "visible" } }, h(PatternDefs, { styles: styles }), look ? (h(VarianceAxis, { x: axisX, height: height, look: look, styles: styles })) : (h("line", { x1: axisX, y1: 0, x2: axisX, y2: height, stroke: AXIS_COLOR, "stroke-width": 1 * s })), h("line", { x1: axisX, y1: centerY, x2: isExtreme ? arrowBaseX : pinEndX, y2: centerY, stroke: barStyle.fill, "stroke-width": PIN_LINE_WIDTH * s }), isExtreme ? (h("polygon", { points: `${tipX},${centerY} ${arrowBaseX},${centerY - headSize / 2} ${arrowBaseX},${centerY + headSize / 2}`, fill: barStyle.fill })) : (h("rect", { x: pinEndX - headSize / 2, y: centerY - headSize / 2, width: headSize, height: headSize, fill: headStyle.fillpattern === "outlined" ? "#ffffff" : getBarFill(headStyle, (_b = look === null || look === void 0 ? void 0 : look.minuendSlot) !== null && _b !== void 0 ? _b : "ac"), stroke: headStyle.stroke, "stroke-width": 1 * s })), h("text", { x: labelX, y: height / 2, dy: "0.35em", "text-anchor": isPositive ? "start" : "end", fill: styles.fontColor, "font-size": fs, "font-style": "italic", "font-family": styles.fontFamily }, displayValue)));
346
348
  };
347
349
  /**
348
350
  * CellBarChart - Renders IBCS-compliant variance bar charts.