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

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.
@@ -28311,60 +28311,103 @@ const BAR_LABEL_FONT_RATIO = 0.85;
28311
28311
  * `.category-wN` (bn-table-renderer.scss:50-83) produces.
28312
28312
  */
28313
28313
  const BAR_CELL_PADDING_EM = 0.5;
28314
- /** Value-label gutter beside a bar, in em (was a hardcoded 38 px at the 13.3333 px em). */
28314
+ /** Value-label gutter beside a waterfall bar, in em (was a hardcoded 38 px at the 13.3333 px em). */
28315
28315
  const BAR_LABEL_EM = 2.85;
28316
- /** Value-label gutter beside a pin, in em (was 45 px). */
28317
- const PIN_LABEL_EM = 3.375;
28318
- /** Gap between the axis and the start of a pin's reserved side, in em (was 5 px). */
28319
- const PIN_PADDING_EM = 0.375;
28320
28316
  /** Zigzag break gap of an overdrive bar, in em (was 4 px). */
28321
28317
  const BREAK_GAP_EM = 0.3;
28322
28318
  /** Gap between a bar/pin end and its value label, in em (was 4 px). */
28323
28319
  const LABEL_GAP_EM = 0.3;
28320
+ /** Pin extent per side kept before a pin column drops label digits, in em. */
28321
+ const MIN_EXTENT_EM = 1;
28324
28322
  /** Share of the per-side bar extent reserved for the overdrive stub. */
28325
28323
  const OVERDRIVE_FRACTION = 0.15;
28324
+ /** Width of the axis of a variance bar or pin, in em: near the 0.3em scenario marker in the header. */
28325
+ const VARIANCE_AXIS_EM = 0.25;
28326
+ /** Side of the square pin head, in px before scaleFactor. */
28327
+ const PIN_HEAD_SIZE = 10;
28328
+ /**
28329
+ * Advance widths in em, the larger of Noto Sans and Arial. The engine ships no font, so a
28330
+ * wider fallback (DejaVu Sans, Verdana) is possible: LABEL_WIDTH_MARGIN and the cell
28331
+ * padding cover it. Any glyph not listed counts as a wide letter.
28332
+ */
28333
+ const GLYPH_EM = {
28334
+ ".": 0.28,
28335
+ ",": 0.28,
28336
+ "-": 0.34,
28337
+ "%": 0.89,
28338
+ " ": 0.28,
28339
+ "\u00a0": 0.28,
28340
+ "\u202f": 0.28,
28341
+ M: 0.9,
28342
+ };
28343
+ const DIGIT_EM = 0.59;
28344
+ const OTHER_EM = 0.72;
28345
+ /** Labels fill the cell to the edge, so the estimate keeps room for a wider fallback font. */
28346
+ const LABEL_WIDTH_MARGIN = 1.1;
28347
+ /** Estimated rendered width of a value label in px, with a margin for wider fonts. */
28348
+ function estimateLabelWidth(text, fontSize) {
28349
+ var _a;
28350
+ let em = 0;
28351
+ for (const char of text) {
28352
+ em += (_a = GLYPH_EM[char]) !== null && _a !== void 0 ? _a : (/[0-9+]/.test(char) ? DIGIT_EM : OTHER_EM);
28353
+ }
28354
+ return em * fontSize * LABEL_WIDTH_MARGIN;
28355
+ }
28326
28356
  /** The SVG fills the cell's content box: the requested column width minus the cell padding. */
28327
28357
  function contentWidth(widthEm, emPx) {
28328
28358
  return Math.max(0, (widthEm - BAR_CELL_PADDING_EM) * emPx);
28329
28359
  }
28330
- /** Sides the axis serves. Clamped to 1 so a hypothetical all-zero column never divides by 0. */
28331
- function sideCount(hasNeg, hasPos) {
28332
- return Math.max(1, (hasNeg ? 1 : 0) + (hasPos ? 1 : 0));
28360
+ /** `outerReserve` covers what sticks out beyond the bar or pin end, e.g. the half head. */
28361
+ function sideNeeds(labels, outerReserve, labelGap) {
28362
+ const hasNeg = labels.neg !== undefined;
28363
+ const hasPos = labels.pos !== undefined || !hasNeg;
28364
+ const need = (has, width) => (has ? outerReserve + labelGap + (width !== null && width !== void 0 ? width : 0) : 0);
28365
+ return { sides: (hasNeg ? 1 : 0) + (hasPos ? 1 : 0), neg: need(hasNeg, labels.neg), pos: need(hasPos, labels.pos) };
28366
+ }
28367
+ /** Bar or pin extent per side that a column keeps as long as its labels allow. */
28368
+ function minExtent(totalWidth, emPx, sides) {
28369
+ return Math.min(MIN_EXTENT_EM * emPx, totalWidth / (2 * sides));
28370
+ }
28371
+ /**
28372
+ * Reserves per side only what its widest label needs and gives the rest to the bars or
28373
+ * pins. A column too narrow for that shortens the bars or pins first, down to zero, so a
28374
+ * label keeps its sign. Only a column narrower than its labels alone shrinks the
28375
+ * reserves, and then the labels overflow. wN stays wN in every case.
28376
+ */
28377
+ function splitSides(totalWidth, labels, outerReserve, labelGap) {
28378
+ const { sides, neg, pos } = sideNeeds(labels, outerReserve, labelGap);
28379
+ const shrink = neg + pos > 0 ? Math.min(1, totalWidth / (neg + pos)) : 1;
28380
+ const extent = Math.max(0, (totalWidth - (neg + pos) * shrink) / sides);
28381
+ return { extent, axisX: labels.neg !== undefined ? neg * shrink + extent : 0 };
28382
+ }
28383
+ /** True when the labels fit beside pins of at least the minimum extent; else the caller drops digits. */
28384
+ function pinLabelsFit(widthEm, emPx, labels, headSize) {
28385
+ const totalWidth = contentWidth(widthEm, emPx);
28386
+ const { sides, neg, pos } = sideNeeds(labels, headSize / 2, LABEL_GAP_EM * emPx);
28387
+ return neg + pos + sides * minExtent(totalWidth, emPx, sides) <= totalWidth + 1e-6;
28333
28388
  }
28334
- function absoluteBarGeometry(widthEm, emPx, hasNeg, hasPos) {
28389
+ function absoluteBarGeometry(widthEm, emPx, labels) {
28335
28390
  const totalWidth = contentWidth(widthEm, emPx);
28336
- const sides = sideCount(hasNeg, hasPos);
28337
- // Narrow columns shrink the gutters rather than the total, so wN stays wN.
28338
- const labelAreaWidth = Math.min(BAR_LABEL_EM * emPx, totalWidth / (2 * sides));
28339
- const maxBarWidth = (totalWidth - labelAreaWidth * sides) / sides;
28391
+ const labelGap = LABEL_GAP_EM * emPx;
28392
+ // a variance bar starts at the outer edge of the thick axis
28393
+ const { extent: maxBarWidth, axisX } = splitSides(totalWidth, labels, (VARIANCE_AXIS_EM * emPx) / 2, labelGap);
28340
28394
  const breakGap = Math.min(BREAK_GAP_EM * emPx, maxBarWidth * 0.2);
28341
28395
  const overdriveZoneWidth = maxBarWidth * OVERDRIVE_FRACTION;
28342
28396
  return {
28343
28397
  totalWidth,
28344
- labelAreaWidth,
28345
28398
  maxBarWidth,
28346
- axisX: hasNeg ? labelAreaWidth + maxBarWidth : 0,
28399
+ axisX,
28347
28400
  breakGap,
28348
28401
  overdriveZoneWidth,
28349
28402
  cutWidth: Math.max(0, maxBarWidth - breakGap - overdriveZoneWidth),
28350
- labelGap: LABEL_GAP_EM * emPx,
28403
+ labelGap,
28351
28404
  };
28352
28405
  }
28353
- function relativePinGeometry(widthEm, emPx, hasNeg, hasPos) {
28406
+ function relativePinGeometry(widthEm, emPx, labels, headSize) {
28354
28407
  const totalWidth = contentWidth(widthEm, emPx);
28355
- const sides = sideCount(hasNeg, hasPos);
28356
- const labelAreaWidth = Math.min(PIN_LABEL_EM * emPx, totalWidth / (2 * sides));
28357
- const sideWidth = (totalWidth - labelAreaWidth * sides) / sides;
28358
- // also leaves room for the outer half of the pin head
28359
- const pinPadding = Math.min(PIN_PADDING_EM * emPx, sideWidth * 0.2);
28360
- return {
28361
- totalWidth,
28362
- labelAreaWidth,
28363
- maxPinLength: Math.max(0, sideWidth - pinPadding),
28364
- pinPadding,
28365
- axisX: hasNeg ? labelAreaWidth + sideWidth : 0,
28366
- labelGap: LABEL_GAP_EM * emPx,
28367
- };
28408
+ const labelGap = LABEL_GAP_EM * emPx;
28409
+ const { extent, axisX } = splitSides(totalWidth, labels, headSize / 2, labelGap);
28410
+ return { totalWidth, maxPinLength: extent, axisX, labelGap };
28368
28411
  }
28369
28412
  function waterfallGeometry(widthEm, emPx) {
28370
28413
  const totalWidth = contentWidth(widthEm, emPx);
@@ -28399,13 +28442,8 @@ const DEFAULT_STYLES = {
28399
28442
  scaleFactor: 1,
28400
28443
  };
28401
28444
  const AXIS_COLOR = "#666666";
28402
- /** Width of the axis of a variance bar or pin, in em: near the 0.3em scenario marker in the header. */
28403
- const VARIANCE_AXIS_EM = 0.25;
28404
- /** Side of the square pin head and width of the pin line, in px before scaleFactor. */
28405
- const PIN_HEAD_SIZE = 10;
28445
+ /** Width of the pin line, in px before scaleFactor. */
28406
28446
  const PIN_LINE_WIDTH = 3;
28407
- /** Rough average glyph width, in label font sizes, to tell if an outlier label fits inside its pin. */
28408
- const LABEL_CHAR_WIDTH = 0.6;
28409
28447
  /**
28410
28448
  * Creates a unique pattern ID for the SVG defs
28411
28449
  */
@@ -28430,16 +28468,20 @@ const PatternDefs = ({ styles }) => {
28430
28468
  ];
28431
28469
  return (index.h("defs", null, patterns.map((p) => (index.h("pattern", { id: p.id, patternUnits: "userSpaceOnUse", width: patternSize, height: patternSize }, index.h("rect", { width: patternSize, height: patternSize, fill: "#ffffff" }), index.h("line", { x1: "0", y1: patternSize, x2: patternSize, y2: "0", stroke: p.stroke, "stroke-width": 1.5 * s }))))));
28432
28470
  };
28433
- /** Characters that fit the pin label gutter (PIN_LABEL_EM), the cell cuts off the rest. */
28471
+ /** Longest relative label that keeps all percent digits; a longer one drops digits so its gutter does not eat the pins. */
28434
28472
  const PIN_LABEL_CHARS = 7;
28435
28473
  /**
28436
28474
  * Formats a number for display with appropriate SI prefix.
28437
28475
  */
28438
- function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0) {
28439
- const prefix = value >= 0 ? "+" : "";
28476
+ function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0, percentSign = true) {
28477
+ // IBCS prints a value that rounds to zero without a sign
28478
+ const signed = (text, zero) => (value > 0 && text !== zero ? "+" : "") + text;
28440
28479
  if (isRelative) {
28441
28480
  // Relative variance is stored as decimal (e.g., -2.0 = -200%)
28442
- const format = (digits) => prefix + Formatter.number(value, "percent", digits, digits, 1, locale);
28481
+ const number = (v, digits) => percentSign
28482
+ ? Formatter.number(v, "percent", digits, digits, 1, locale)
28483
+ : Formatter.number(v * 100, "decimal", digits, digits, 1, locale);
28484
+ const format = (digits) => signed(number(value, digits), number(0, digits));
28443
28485
  // Intl throws on a digit count outside 0 to 20
28444
28486
  let digits = Math.min(20, Math.max(0, Math.trunc(percentDigits) || 0));
28445
28487
  while (digits > 0 && format(digits).length > PIN_LABEL_CHARS) {
@@ -28466,7 +28508,7 @@ function formatValue(value, isRelative, measureScale = 1, locale, percentDigits
28466
28508
  suffix = "K";
28467
28509
  }
28468
28510
  const digits = absValue >= unit * 10 ? 0 : 1;
28469
- return prefix + Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale) + suffix;
28511
+ return (signed(Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale), Formatter.number(0, "decimal", digits, 0, 1, locale)) + suffix);
28470
28512
  }
28471
28513
  /**
28472
28514
  * Checks if the column is a scenario column (ac, pp, fc, pl) vs variance (d_, dr_).
@@ -28532,6 +28574,17 @@ const VarianceAxis = ({ x, height, look, styles }) => {
28532
28574
  }
28533
28575
  return index.h("g", { class: `variance-axis axis-${referenceSlot}` }, lines);
28534
28576
  };
28577
+ /** A zero value is drawn on the positive side, unless the column only has negative values. */
28578
+ function zeroOnPositiveSide(labels) {
28579
+ return labels.pos !== undefined || labels.neg === undefined;
28580
+ }
28581
+ /** Side coverage from the column statistics, with only this cell's label as the width. */
28582
+ function ownLabelWidths(columnStats, width) {
28583
+ return {
28584
+ neg: columnStats.min < 0 ? width : undefined,
28585
+ pos: columnStats.max > 0 ? width : undefined,
28586
+ };
28587
+ }
28535
28588
  /**
28536
28589
  * Gets the variance style based on value and sentiment direction.
28537
28590
  * pos suffix = positive values are good (green), negative are bad (red)
@@ -28620,13 +28673,12 @@ function renderZigzagBreak(breakX, breakGap, yTop, barHeight, s) {
28620
28673
  * exactly `columnWidthEm` em - see barColumnGeometry.ts.
28621
28674
  */
28622
28675
  const AbsoluteVarianceBar = (props) => {
28623
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
28676
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
28624
28677
  const s = styles.scaleFactor;
28625
28678
  const fs = styles.fontSize;
28626
- // Adaptive axis placement based on data range
28627
- const hasNeg = columnStats.min < 0;
28628
- const hasPos = columnStats.max > 0;
28629
- const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, hasNeg, hasPos);
28679
+ const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
28680
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
28681
+ const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, sides);
28630
28682
  // Direct unit scaling via fontSize
28631
28683
  const scaledWidth = Math.abs(value) * styles.unitScaling * fs;
28632
28684
  // auto scaling puts the largest bar on the cut, float rounding must not turn it into an overdrive bar
@@ -28639,7 +28691,7 @@ const AbsoluteVarianceBar = (props) => {
28639
28691
  const padding = 2 * s;
28640
28692
  const barHeight = height - padding * 2;
28641
28693
  const barY = padding;
28642
- const isPositive = value >= 0;
28694
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28643
28695
  // Get style based on column type (scenario vs variance)
28644
28696
  const look = getVarianceLook(scenario, styles);
28645
28697
  const sentimentStyle = getBarStyle(scenario, value, isPositiveGood, styles);
@@ -28659,7 +28711,6 @@ const AbsoluteVarianceBar = (props) => {
28659
28711
  const fillValue = getBarFill(barStyle, patternPrefix);
28660
28712
  // a hatched variance bar keeps an outline, the shared getBarStroke also serves the waterfall
28661
28713
  const { stroke: barStroke, strokeWidth: barStrokeWidth } = look && barStyle.fillpattern === "hatched" ? { stroke: barStyle.stroke, strokeWidth: 1 } : getBarStroke(barStyle);
28662
- const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
28663
28714
  // bars start at the outer edge of the thick variance axis
28664
28715
  const axisOffset = look ? (VARIANCE_AXIS_EM * styles.emPx) / 2 : 0;
28665
28716
  const rootX = isPositive ? axisX + axisOffset : axisX - axisOffset;
@@ -28676,7 +28727,7 @@ const AbsoluteVarianceBar = (props) => {
28676
28727
  * Renders a relative variance pin chart (dr_ prefix).
28677
28728
  * Line from the axis in the sentiment colour, ending in a square head in the look of the
28678
28729
  * minuend, with the value label beside the head like an absolute bar label. A pin beyond
28679
- * the pin area is cut and ends in an arrow, its label inside the pin before the arrow.
28730
+ * the pin area is cut and ends in an arrow, its label after the arrow tip.
28680
28731
  *
28681
28732
  * Layout: [left label area][pin area with axis][right label area], summing to exactly
28682
28733
  * `columnWidthEm` em - see barColumnGeometry.ts.
@@ -28684,40 +28735,32 @@ const AbsoluteVarianceBar = (props) => {
28684
28735
  */
28685
28736
  const RelativeVariancePin = (props) => {
28686
28737
  var _a, _b;
28687
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
28738
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
28688
28739
  const s = styles.scaleFactor;
28689
28740
  const fs = styles.fontSize;
28690
- // Adaptive axis placement based on data range
28691
- const hasNeg = columnStats.min < 0;
28692
- const hasPos = columnStats.max > 0;
28693
- const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, hasNeg, hasPos);
28741
+ const headSize = PIN_HEAD_SIZE * s;
28742
+ const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits, percentSign);
28743
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
28744
+ const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize);
28694
28745
  const centerY = height / 2;
28695
28746
  // Direct unit scaling via fontSize
28696
28747
  const percentValue = Math.abs(value) * 100;
28697
28748
  const scaledLength = percentValue * styles.percentageScaling * fs;
28698
- const isExtreme = scaledLength > maxPinLength;
28749
+ // auto scaling puts the longest pin on the limit, float rounding must not cut it
28750
+ const isExtreme = scaledLength - maxPinLength > 1e-6;
28699
28751
  const pinLength = Math.min(scaledLength, maxPinLength);
28700
- // a zero label goes to the side that has a label gutter
28701
- const isPositive = value > 0 || (value === 0 && (hasPos || !hasNeg));
28752
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28702
28753
  const direction = isPositive ? 1 : -1;
28703
28754
  const pinEndX = axisX + direction * pinLength;
28704
28755
  // Get style based on column type (scenario vs variance)
28705
28756
  const barStyle = getBarStyle(scenario, value, isPositiveGood, styles);
28706
28757
  const look = getVarianceLook(scenario, styles);
28707
28758
  const headStyle = (_a = look === null || look === void 0 ? void 0 : look.minuend) !== null && _a !== void 0 ? _a : styles.scenarios.ac;
28708
- const headSize = PIN_HEAD_SIZE * s;
28709
- const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits);
28710
- // the arrow tip lands on the outer edge of a full-length head
28759
+ // the arrow tip lands on the outer edge of a full-length head, so the label after it stays in the cell
28711
28760
  const tipX = axisX + direction * (maxPinLength + headSize / 2);
28712
28761
  const arrowBaseX = tipX - direction * headSize;
28713
- const labelFitsInside = maxPinLength - headSize / 2 - labelGap >= displayValue.length * LABEL_CHAR_WIDTH * fs;
28714
- const labelInside = isExtreme && labelFitsInside;
28715
- let labelX = pinEndX + direction * (headSize / 2 + labelGap);
28716
- if (isExtreme) {
28717
- labelX = labelInside ? arrowBaseX - direction * labelGap : tipX + direction * labelGap;
28718
- }
28719
- const labelAtStart = isPositive !== labelInside;
28720
- return (index.h("svg", { width: totalWidth, height: height, class: "cell-bar-chart relative-variance", style: { overflow: "visible" } }, index.h(PatternDefs, { styles: styles }), look ? (index.h(VarianceAxis, { x: axisX, height: height, look: look, styles: styles })) : (index.h("line", { x1: axisX, y1: 0, x2: axisX, y2: height, stroke: AXIS_COLOR, "stroke-width": 1 * s })), index.h("line", { x1: axisX, y1: centerY, x2: isExtreme ? arrowBaseX : pinEndX, y2: centerY, stroke: barStyle.fill, "stroke-width": PIN_LINE_WIDTH * s }), isExtreme ? (index.h("polygon", { points: `${tipX},${centerY} ${arrowBaseX},${centerY - headSize / 2} ${arrowBaseX},${centerY + headSize / 2}`, fill: barStyle.fill })) : (index.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 })), index.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)));
28762
+ const labelX = (isExtreme ? tipX : pinEndX + direction * (headSize / 2)) + direction * labelGap;
28763
+ return (index.h("svg", { width: totalWidth, height: height, class: "cell-bar-chart relative-variance", style: { overflow: "visible" } }, index.h(PatternDefs, { styles: styles }), look ? (index.h(VarianceAxis, { x: axisX, height: height, look: look, styles: styles })) : (index.h("line", { x1: axisX, y1: 0, x2: axisX, y2: height, stroke: AXIS_COLOR, "stroke-width": 1 * s })), index.h("line", { x1: axisX, y1: centerY, x2: isExtreme ? arrowBaseX : pinEndX, y2: centerY, stroke: barStyle.fill, "stroke-width": PIN_LINE_WIDTH * s }), isExtreme ? (index.h("polygon", { points: `${tipX},${centerY} ${arrowBaseX},${centerY - headSize / 2} ${arrowBaseX},${centerY + headSize / 2}`, fill: barStyle.fill })) : (index.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 })), index.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)));
28721
28764
  };
28722
28765
  /**
28723
28766
  * CellBarChart - Renders IBCS-compliant variance bar charts.
@@ -31221,6 +31264,7 @@ const BnTableRenderer = class {
31221
31264
  /** @internal */
31222
31265
  this.scaleFactor = 1;
31223
31266
  this.componentDataFormatDigits = new DataFormatDigits();
31267
+ this.barColumnLayouts = new Map();
31224
31268
  withHasError.withComponentStatus(this);
31225
31269
  this.componentStyle = withStyled.withComponentStyle(this);
31226
31270
  }
@@ -31341,14 +31385,15 @@ const BnTableRenderer = class {
31341
31385
  return index.h("div", { class: "empty-state" }, errorMsg);
31342
31386
  }
31343
31387
  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;
31344
- return (index.h("table", null, index.h("thead", null, this.table.getHeader().map((row) => (index.h("tr", { class: row.cssStyle }, row.getCells().map((cell) => (index.h("th", { colSpan: cell.colSpan, class: cell.cssStyle }, cell.getAsHeaderList().map((line, i, arr) => (index.h("span", null, i === arr.length - 1 ? (index.h("span", { class: line === " " ? "table-footer" : "" }, line, cell.drawPrefix(), " ")) : (index.h("span", null, line, cell.drawPrefix(), index.h("br", null))))))))))))), index.h("tbody", null, this.table.getBody().map((row) => (index.h("tr", Object.assign({ class: row.cssStyle }, dataPointAttributes(row.key, "element")), row === null || row === void 0 ? void 0 : row.getCells().map((cell) => (index.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 !== "-"
31345
- ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
31346
- : {})), cell.prefix != null ? (index.h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
31347
- cell.columnStats &&
31348
- cell.value !== withStyled.NOT_AVAILABLE &&
31349
- (cell.waterfall !== undefined || row.type !== TableRowType.SUM) ? (cell.waterfall !== undefined ? (index.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 })) : (index.h(CellBarChart, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: cell.key.startsWith("dr"), isPositiveGood: cell.direction === "pos",
31350
- // percentage points: the fraction times 100, no measure scale
31351
- 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)))))))))));
31388
+ return (index.h("table", null, index.h("thead", null, this.table.getHeader().map((row) => (index.h("tr", { class: row.cssStyle }, row.getCells().map((cell) => (index.h("th", { colSpan: cell.colSpan, class: cell.cssStyle }, cell.getAsHeaderList().map((line, i, arr) => (index.h("span", null, i === arr.length - 1 ? (index.h("span", { class: line === " " ? "table-footer" : "" }, line, cell.drawPrefix(), " ")) : (index.h("span", null, line, cell.drawPrefix(), index.h("br", null))))))))))))), index.h("tbody", null, this.table.getBody().map((row) => (index.h("tr", Object.assign({ class: row.cssStyle }, dataPointAttributes(row.key, "element")), row === null || row === void 0 ? void 0 : row.getCells().map((cell) => {
31389
+ var _a, _b, _c, _d;
31390
+ return (index.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 !== "-"
31391
+ ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
31392
+ : {})), cell.prefix != null ? (index.h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
31393
+ cell.columnStats &&
31394
+ cell.value !== withStyled.NOT_AVAILABLE &&
31395
+ (cell.waterfall !== undefined || row.type !== TableRowType.SUM) ? (cell.waterfall !== undefined ? (index.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 })) : (index.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, styles: this.barChartStyles }))) : (cell.draw(this.componentDataFormatDigits, locale))));
31396
+ })))))));
31352
31397
  };
31353
31398
  return index.h(index.Host, { class: { "no-data": ((_a = this.table) === null || _a === void 0 ? void 0 : _a.hasNoData()) === true } }, helper());
31354
31399
  }
@@ -31393,6 +31438,7 @@ const BnTableRenderer = class {
31393
31438
  else {
31394
31439
  (_h = this.errors) === null || _h === void 0 ? void 0 : _h.remove("WARN_orderBucket");
31395
31440
  }
31441
+ this.barColumnLayouts = this.collectBarColumnLayouts();
31396
31442
  this.barChartStyles = this.prepareBarChartStyles();
31397
31443
  }
31398
31444
  prepareConfig(db) {
@@ -31515,67 +31561,106 @@ const BnTableRenderer = class {
31515
31561
  /**
31516
31562
  * Auto-calculate unitScaling so the largest absolute variance bar
31517
31563
  * fills ~80% of its side of the bar area, without tripping the overdrive break.
31564
+ * One scaling for all columns, so it is the smallest that fits every column.
31518
31565
  */
31519
31566
  autoUnitScaling() {
31520
31567
  const emPx = this.emPx();
31521
31568
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31522
- const { absMax, hasNeg, hasPos } = this.getAbsBarColumnStats();
31523
- if (absMax <= 0 || fs <= 0) {
31524
- return 0.01;
31569
+ let scaling = Infinity;
31570
+ for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
31571
+ if (isRelative || absMax <= 0)
31572
+ continue;
31573
+ const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, labels);
31574
+ scaling = Math.min(scaling, Math.min(maxBarWidth * 0.8, cutWidth) / (absMax * fs));
31525
31575
  }
31526
- const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
31527
- const targetWidth = Math.min(maxBarWidth * 0.8, cutWidth);
31528
- return targetWidth / (absMax * fs);
31576
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.01;
31529
31577
  }
31530
31578
  /**
31531
- * Auto-calculate percentageScaling so the largest relative variance pin
31532
- * fills ~80% of the pin area.
31579
+ * Auto-calculate percentageScaling so the longest relative variance pin, its head and
31580
+ * its label exactly fill the column. One scaling for all columns, the smallest that fits.
31533
31581
  */
31534
31582
  autoPercentageScaling() {
31535
31583
  const emPx = this.emPx();
31536
31584
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31537
- const { absMax: maxPct, hasNeg, hasPos } = this.getRelativeBarColumnStats();
31538
- if (maxPct <= 0 || fs <= 0) {
31539
- return 0.5;
31585
+ let scaling = Infinity;
31586
+ for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
31587
+ if (!isRelative || absMax <= 0)
31588
+ continue;
31589
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31590
+ const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
31591
+ scaling = Math.min(scaling, maxPinLength / (absMax * fs));
31540
31592
  }
31541
- const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
31542
- return (maxPinLength * 0.8) / (maxPct * fs);
31543
- }
31544
- /**
31545
- * Maximum value and sign coverage across all absolute variance columns (d_*) in the table body.
31546
- * Sign coverage decides how much of the column the bar area gets (see barColumnGeometry.ts);
31547
- * OR-ing it across columns keeps the single shared scaling inside the narrowest bar area.
31548
- */
31549
- getAbsBarColumnStats() {
31550
- // waterfall cells scale independently and must not skew variance auto-scaling
31551
- return this.collectBarColumnStats((cell) => !cell.key.startsWith("dr") && !cell.waterfall, 1);
31593
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.5;
31552
31594
  }
31553
31595
  /**
31554
- * Same for the relative variance columns (dr_*). Values are stored as decimals
31555
- * (e.g. 0.2 = 20%), returned as percentage (20).
31596
+ * Largest value and widest label per side of every bar and pin column, from the cells
31597
+ * that draw one: total rows and n/a draw none. Interval buckets share the column key,
31598
+ * so they share the layout and the axis.
31556
31599
  */
31557
- getRelativeBarColumnStats() {
31558
- return this.collectBarColumnStats((cell) => cell.key.startsWith("dr"), 100);
31559
- }
31560
- collectBarColumnStats(accept, valueFactor) {
31561
- const stats = { absMax: 0, hasNeg: false, hasPos: false };
31562
- if (!this.table)
31563
- return stats;
31600
+ collectBarColumnLayouts() {
31601
+ var _a, _b, _c, _d;
31602
+ const layouts = new Map();
31603
+ if (!this.table || this.table.hasNoData())
31604
+ return layouts;
31605
+ const cellsByKey = new Map();
31564
31606
  for (const row of this.table.getBody()) {
31565
31607
  if (row.type === TableRowType.SUM)
31566
31608
  continue;
31567
31609
  for (const cell of row.getCells()) {
31568
- if (!cell.isBarChart || !cell.columnStats || !accept(cell))
31610
+ // waterfall cells scale independently
31611
+ if (!cell.isBarChart || !cell.columnStats || cell.waterfall !== undefined)
31569
31612
  continue;
31570
- stats.absMax = Math.max(stats.absMax, cell.columnStats.absMax * valueFactor);
31571
- stats.hasNeg = stats.hasNeg || cell.columnStats.min < 0;
31572
- stats.hasPos = stats.hasPos || cell.columnStats.max > 0;
31613
+ if (typeof cell.value !== "number" || isNaN(cell.value))
31614
+ continue;
31615
+ cellsByKey.set(cell.key, [...((_a = cellsByKey.get(cell.key)) !== null && _a !== void 0 ? _a : []), cell]);
31573
31616
  }
31574
31617
  }
31575
- return stats;
31618
+ const emPx = this.emPx();
31619
+ const fs = emPx * BAR_LABEL_FONT_RATIO;
31620
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31621
+ 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;
31622
+ for (const [key, cells] of cellsByKey) {
31623
+ const isRelative = key.startsWith("dr");
31624
+ const values = cells.map((cell) => cell.value);
31625
+ const measure = (percentDigits, percentSign) => {
31626
+ var _a;
31627
+ const labels = {
31628
+ neg: values.some((value) => value < 0) ? 0 : undefined,
31629
+ pos: values.some((value) => value > 0) ? 0 : undefined,
31630
+ };
31631
+ const zeroPositive = zeroOnPositiveSide(labels);
31632
+ for (const cell of cells) {
31633
+ const value = cell.value;
31634
+ const side = value > 0 || (value === 0 && zeroPositive) ? "pos" : "neg";
31635
+ const text = formatValue(value, isRelative, barLabelMeasureScale(cell), locale, percentDigits, percentSign);
31636
+ labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, estimateLabelWidth(text, fs));
31637
+ }
31638
+ return labels;
31639
+ };
31640
+ // a narrow pin column drops percent digits, then the %, so every label keeps its sign
31641
+ let percentDigits = Math.min(20, Math.max(0, Math.trunc(this.componentDataFormatDigits.percent) || 0));
31642
+ let percentSign = true;
31643
+ let labels = measure(percentDigits, percentSign);
31644
+ while (isRelative && (percentDigits > 0 || percentSign)) {
31645
+ if (pinLabelsFit(this.barColumnMaxWidth, emPx, labels, headSize))
31646
+ break;
31647
+ if (percentDigits > 0)
31648
+ percentDigits--;
31649
+ else
31650
+ percentSign = false;
31651
+ labels = measure(percentDigits, percentSign);
31652
+ }
31653
+ const absMax = Math.max(...values.map(Math.abs)) * (isRelative ? 100 : 1);
31654
+ layouts.set(key, { isRelative, absMax, labels, percentDigits, percentSign });
31655
+ }
31656
+ return layouts;
31576
31657
  }
31577
31658
  get element() { return index.getElement(this); }
31578
31659
  };
31660
+ /** Percentage points: the fraction times 100, no measure scale. */
31661
+ function barLabelMeasureScale(cell) {
31662
+ return cell.type === "percentpoint" ? 0.01 : cell.measureScale;
31663
+ }
31579
31664
  BnTableRenderer.style = bnTableRendererCss();
31580
31665
 
31581
31666
  const bnTitleCss = () => `:host{padding:0.5em;margin-bottom:0.5em;box-sizing:border-box;-webkit-box-sizing:border-box;width:100%;display:block;font-family:var(--bn-font-family, "Noto Sans", sans-serif);font-size:var(--bn-font-size, 13.3333px);color:var(--bn-font-color, #000000)}`;