@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.
@@ -28309,59 +28309,174 @@ const BAR_LABEL_FONT_RATIO = 0.85;
28309
28309
  * `.category-wN` (bn-table-renderer.scss:50-83) produces.
28310
28310
  */
28311
28311
  const BAR_CELL_PADDING_EM = 0.5;
28312
- /** Value-label gutter beside a bar, in em (was a hardcoded 38 px at the 13.3333 px em). */
28312
+ /** Value-label gutter beside a waterfall bar, in em (was a hardcoded 38 px at the 13.3333 px em). */
28313
28313
  const BAR_LABEL_EM = 2.85;
28314
- /** Value-label gutter beside a pin, in em (was 45 px). */
28315
- const PIN_LABEL_EM = 3.375;
28316
- /** Gap between the axis and the start of a pin's reserved side, in em (was 5 px). */
28317
- const PIN_PADDING_EM = 0.375;
28318
28314
  /** Zigzag break gap of an overdrive bar, in em (was 4 px). */
28319
28315
  const BREAK_GAP_EM = 0.3;
28320
28316
  /** Gap between a bar/pin end and its value label, in em (was 4 px). */
28321
28317
  const LABEL_GAP_EM = 0.3;
28318
+ /** Pin extent per side kept before a pin column drops label digits, in em. */
28319
+ const MIN_EXTENT_EM = 1;
28322
28320
  /** Share of the per-side bar extent reserved for the overdrive stub. */
28323
28321
  const OVERDRIVE_FRACTION = 0.15;
28322
+ /** Width of the axis of a variance bar or pin, in em: near the 0.3em scenario marker in the header. */
28323
+ const VARIANCE_AXIS_EM = 0.25;
28324
+ /** Side of the square pin head, in px before scaleFactor. */
28325
+ const PIN_HEAD_SIZE = 10;
28326
+ /**
28327
+ * Advance widths in em, the larger of Noto Sans and Arial. The engine ships no font, so a
28328
+ * wider fallback (DejaVu Sans, Verdana) is possible: LABEL_WIDTH_MARGIN and the cell
28329
+ * padding cover it. Any glyph not listed counts as a wide letter.
28330
+ */
28331
+ const GLYPH_EM = {
28332
+ ".": 0.28,
28333
+ ",": 0.28,
28334
+ "-": 0.34,
28335
+ "%": 0.89,
28336
+ " ": 0.28,
28337
+ "\u00a0": 0.28,
28338
+ "\u202f": 0.28,
28339
+ M: 0.9,
28340
+ };
28341
+ const DIGIT_EM = 0.59;
28342
+ const OTHER_EM = 0.72;
28343
+ /** Labels fill the cell to the edge, so the estimate keeps room for a wider fallback font. */
28344
+ const LABEL_WIDTH_MARGIN = 1.1;
28345
+ /** Estimated rendered width of a value label in px, with a margin for wider fonts. */
28346
+ function estimateLabelWidth(text, fontSize) {
28347
+ var _a;
28348
+ let em = 0;
28349
+ for (const char of text) {
28350
+ em += (_a = GLYPH_EM[char]) !== null && _a !== void 0 ? _a : (/[0-9+]/.test(char) ? DIGIT_EM : OTHER_EM);
28351
+ }
28352
+ return em * fontSize * LABEL_WIDTH_MARGIN;
28353
+ }
28354
+ /** Safety pad on a measured label, in label font size: covers subpixel rounding only. */
28355
+ const MEASURE_PAD = 0.15;
28356
+ const FONT_SENTINEL = "1px __bn_font_sentinel__";
28357
+ let measureContext;
28358
+ /** One shared 2D context; null where the document has no canvas (spec tests, mock-doc). */
28359
+ function labelMeasureContext() {
28360
+ var _a, _b, _c;
28361
+ if (measureContext === undefined) {
28362
+ try {
28363
+ measureContext =
28364
+ typeof document === "undefined" ? null : ((_c = (_b = (_a = document.createElement("canvas")).getContext) === null || _b === void 0 ? void 0 : _b.call(_a, "2d")) !== null && _c !== void 0 ? _c : null);
28365
+ }
28366
+ catch (_d) {
28367
+ measureContext = null;
28368
+ }
28369
+ }
28370
+ return measureContext;
28371
+ }
28372
+ /**
28373
+ * Rendered width of a value label in px, measured with the font the SVG text uses. Falls
28374
+ * back to estimateLabelWidth where no canvas can measure.
28375
+ */
28376
+ function labelWidth(text, fontSize, fontFamily, italic) {
28377
+ const ctx = labelMeasureContext();
28378
+ if (ctx) {
28379
+ // the canvas ignores a font it cannot parse and keeps the last one: the sentinel shows that
28380
+ ctx.font = FONT_SENTINEL;
28381
+ const sentinel = ctx.font;
28382
+ ctx.font = `${italic ? "italic " : ""}${fontSize}px ${fontFamily}`;
28383
+ if (ctx.font === sentinel)
28384
+ return estimateLabelWidth(text, fontSize);
28385
+ const width = ctx.measureText(text).width;
28386
+ if (Number.isFinite(width) && (width > 0 || text === ""))
28387
+ return width + MEASURE_PAD * fontSize;
28388
+ }
28389
+ return estimateLabelWidth(text, fontSize);
28390
+ }
28324
28391
  /** The SVG fills the cell's content box: the requested column width minus the cell padding. */
28325
28392
  function contentWidth(widthEm, emPx) {
28326
28393
  return Math.max(0, (widthEm - BAR_CELL_PADDING_EM) * emPx);
28327
28394
  }
28328
- /** Sides the axis serves. Clamped to 1 so a hypothetical all-zero column never divides by 0. */
28329
- function sideCount(hasNeg, hasPos) {
28330
- return Math.max(1, (hasNeg ? 1 : 0) + (hasPos ? 1 : 0));
28395
+ /** `outerReserve` covers what sticks out beyond the bar or pin end, e.g. the half head. */
28396
+ function sideNeeds(labels, outerReserve, labelGap) {
28397
+ const hasNeg = labels.neg !== undefined;
28398
+ const hasPos = labels.pos !== undefined || !hasNeg;
28399
+ const need = (has, width) => (has ? outerReserve + labelGap + (width !== null && width !== void 0 ? width : 0) : 0);
28400
+ return { sides: (hasNeg ? 1 : 0) + (hasPos ? 1 : 0), neg: need(hasNeg, labels.neg), pos: need(hasPos, labels.pos) };
28331
28401
  }
28332
- function absoluteBarGeometry(widthEm, emPx, hasNeg, hasPos) {
28402
+ /** Bar or pin extent per side that a column keeps as long as its labels allow. */
28403
+ function minExtent(totalWidth, emPx, sides) {
28404
+ return Math.min(MIN_EXTENT_EM * emPx, totalWidth / (2 * sides));
28405
+ }
28406
+ /**
28407
+ * Reserves per side only what its widest label needs and gives the rest to the bars or
28408
+ * pins. A column too narrow for that shortens the bars or pins first, down to zero, so a
28409
+ * label keeps its sign. Only a column narrower than its labels alone shrinks the
28410
+ * reserves, and then the labels overflow. wN stays wN in every case.
28411
+ */
28412
+ function splitSides(totalWidth, labels, outerReserve, labelGap) {
28413
+ const { sides, neg, pos } = sideNeeds(labels, outerReserve, labelGap);
28414
+ const shrink = neg + pos > 0 ? Math.min(1, totalWidth / (neg + pos)) : 1;
28415
+ const extent = Math.max(0, (totalWidth - (neg + pos) * shrink) / sides);
28416
+ return { extent, axisX: labels.neg !== undefined ? neg * shrink + extent : 0 };
28417
+ }
28418
+ /** True when the labels fit beside pins of at least the minimum extent; else the caller drops digits. */
28419
+ function pinLabelsFit(widthEm, emPx, labels, headSize) {
28420
+ const totalWidth = contentWidth(widthEm, emPx);
28421
+ const { sides, neg, pos } = sideNeeds(labels, headSize / 2, LABEL_GAP_EM * emPx);
28422
+ return neg + pos + sides * minExtent(totalWidth, emPx, sides) <= totalWidth + 1e-6;
28423
+ }
28424
+ function absoluteBarGeometry(widthEm, emPx, labels) {
28333
28425
  const totalWidth = contentWidth(widthEm, emPx);
28334
- const sides = sideCount(hasNeg, hasPos);
28335
- // Narrow columns shrink the gutters rather than the total, so wN stays wN.
28336
- const labelAreaWidth = Math.min(BAR_LABEL_EM * emPx, totalWidth / (2 * sides));
28337
- const maxBarWidth = (totalWidth - labelAreaWidth * sides) / sides;
28426
+ const labelGap = LABEL_GAP_EM * emPx;
28427
+ // a variance bar starts at the outer edge of the thick axis
28428
+ const { extent: maxBarWidth, axisX } = splitSides(totalWidth, labels, (VARIANCE_AXIS_EM * emPx) / 2, labelGap);
28338
28429
  const breakGap = Math.min(BREAK_GAP_EM * emPx, maxBarWidth * 0.2);
28339
28430
  const overdriveZoneWidth = maxBarWidth * OVERDRIVE_FRACTION;
28340
28431
  return {
28341
28432
  totalWidth,
28342
- labelAreaWidth,
28343
28433
  maxBarWidth,
28344
- axisX: hasNeg ? labelAreaWidth + maxBarWidth : 0,
28434
+ axisX,
28345
28435
  breakGap,
28346
28436
  overdriveZoneWidth,
28347
28437
  cutWidth: Math.max(0, maxBarWidth - breakGap - overdriveZoneWidth),
28348
- labelGap: LABEL_GAP_EM * emPx,
28438
+ labelGap,
28349
28439
  };
28350
28440
  }
28351
- function relativePinGeometry(widthEm, emPx, hasNeg, hasPos) {
28441
+ /**
28442
+ * `reach` is the length in px the longest pin of each side wants at the column's scale.
28443
+ * With it, the pin room is split by what each side needs, so a side with shorter pins
28444
+ * gives its room to the other; room left after both is shared equally. Without it, both
28445
+ * sides get the same extent.
28446
+ */
28447
+ function relativePinGeometry(widthEm, emPx, labels, headSize, reach) {
28448
+ var _a, _b;
28352
28449
  const totalWidth = contentWidth(widthEm, emPx);
28353
- const sides = sideCount(hasNeg, hasPos);
28354
- const labelAreaWidth = Math.min(PIN_LABEL_EM * emPx, totalWidth / (2 * sides));
28355
- const sideWidth = (totalWidth - labelAreaWidth * sides) / sides;
28356
- // also leaves room for the outer half of the pin head
28357
- const pinPadding = Math.min(PIN_PADDING_EM * emPx, sideWidth * 0.2);
28450
+ const labelGap = LABEL_GAP_EM * emPx;
28451
+ const { extent, axisX } = splitSides(totalWidth, labels, headSize / 2, labelGap);
28452
+ const hasNeg = labels.neg !== undefined;
28453
+ const hasPos = labels.pos !== undefined || !hasNeg;
28454
+ let neg = hasNeg ? extent : 0;
28455
+ let pos = hasPos ? extent : 0;
28456
+ if (reach && hasNeg && hasPos) {
28457
+ const room = 2 * extent;
28458
+ const wantNeg = Math.max(0, (_a = reach.neg) !== null && _a !== void 0 ? _a : 0);
28459
+ const wantPos = Math.max(0, (_b = reach.pos) !== null && _b !== void 0 ? _b : 0);
28460
+ if (wantNeg + wantPos <= room + 1e-6) {
28461
+ neg = wantNeg + (room - wantNeg - wantPos) / 2;
28462
+ pos = room - neg;
28463
+ }
28464
+ else if (wantNeg < extent) {
28465
+ neg = wantNeg;
28466
+ pos = room - neg;
28467
+ }
28468
+ else if (wantPos < extent) {
28469
+ pos = wantPos;
28470
+ neg = room - pos;
28471
+ }
28472
+ }
28358
28473
  return {
28359
28474
  totalWidth,
28360
- labelAreaWidth,
28361
- maxPinLength: Math.max(0, sideWidth - pinPadding),
28362
- pinPadding,
28363
- axisX: hasNeg ? labelAreaWidth + sideWidth : 0,
28364
- labelGap: LABEL_GAP_EM * emPx,
28475
+ maxPinLength: Math.max(neg, pos),
28476
+ negPinLength: neg,
28477
+ posPinLength: pos,
28478
+ axisX: hasNeg ? axisX - extent + neg : 0,
28479
+ labelGap,
28365
28480
  };
28366
28481
  }
28367
28482
  function waterfallGeometry(widthEm, emPx) {
@@ -28397,13 +28512,8 @@ const DEFAULT_STYLES = {
28397
28512
  scaleFactor: 1,
28398
28513
  };
28399
28514
  const AXIS_COLOR = "#666666";
28400
- /** Width of the axis of a variance bar or pin, in em: near the 0.3em scenario marker in the header. */
28401
- const VARIANCE_AXIS_EM = 0.25;
28402
- /** Side of the square pin head and width of the pin line, in px before scaleFactor. */
28403
- const PIN_HEAD_SIZE = 10;
28515
+ /** Width of the pin line, in px before scaleFactor. */
28404
28516
  const PIN_LINE_WIDTH = 3;
28405
- /** Rough average glyph width, in label font sizes, to tell if an outlier label fits inside its pin. */
28406
- const LABEL_CHAR_WIDTH = 0.6;
28407
28517
  /**
28408
28518
  * Creates a unique pattern ID for the SVG defs
28409
28519
  */
@@ -28428,16 +28538,20 @@ const PatternDefs = ({ styles }) => {
28428
28538
  ];
28429
28539
  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 }))))));
28430
28540
  };
28431
- /** Characters that fit the pin label gutter (PIN_LABEL_EM), the cell cuts off the rest. */
28541
+ /** Longest relative label that keeps all percent digits; a longer one drops digits so its gutter does not eat the pins. */
28432
28542
  const PIN_LABEL_CHARS = 7;
28433
28543
  /**
28434
28544
  * Formats a number for display with appropriate SI prefix.
28435
28545
  */
28436
- function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0) {
28437
- const prefix = value >= 0 ? "+" : "";
28546
+ function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0, percentSign = true) {
28547
+ // IBCS prints a value that rounds to zero without a sign
28548
+ const signed = (text, zero) => (value > 0 && text !== zero ? "+" : "") + text;
28438
28549
  if (isRelative) {
28439
28550
  // Relative variance is stored as decimal (e.g., -2.0 = -200%)
28440
- const format = (digits) => prefix + Formatter.number(value, "percent", digits, digits, 1, locale);
28551
+ const number = (v, digits) => percentSign
28552
+ ? Formatter.number(v, "percent", digits, digits, 1, locale)
28553
+ : Formatter.number(v * 100, "decimal", digits, digits, 1, locale);
28554
+ const format = (digits) => signed(number(value, digits), number(0, digits));
28441
28555
  // Intl throws on a digit count outside 0 to 20
28442
28556
  let digits = Math.min(20, Math.max(0, Math.trunc(percentDigits) || 0));
28443
28557
  while (digits > 0 && format(digits).length > PIN_LABEL_CHARS) {
@@ -28464,7 +28578,7 @@ function formatValue(value, isRelative, measureScale = 1, locale, percentDigits
28464
28578
  suffix = "K";
28465
28579
  }
28466
28580
  const digits = absValue >= unit * 10 ? 0 : 1;
28467
- return prefix + Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale) + suffix;
28581
+ return (signed(Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale), Formatter.number(0, "decimal", digits, 0, 1, locale)) + suffix);
28468
28582
  }
28469
28583
  /**
28470
28584
  * Checks if the column is a scenario column (ac, pp, fc, pl) vs variance (d_, dr_).
@@ -28530,6 +28644,17 @@ const VarianceAxis = ({ x, height, look, styles }) => {
28530
28644
  }
28531
28645
  return h("g", { class: `variance-axis axis-${referenceSlot}` }, lines);
28532
28646
  };
28647
+ /** A zero value is drawn on the positive side, unless the column only has negative values. */
28648
+ function zeroOnPositiveSide(labels) {
28649
+ return labels.pos !== undefined || labels.neg === undefined;
28650
+ }
28651
+ /** Side coverage from the column statistics, with only this cell's label as the width. */
28652
+ function ownLabelWidths(columnStats, width) {
28653
+ return {
28654
+ neg: columnStats.min < 0 ? width : undefined,
28655
+ pos: columnStats.max > 0 ? width : undefined,
28656
+ };
28657
+ }
28533
28658
  /**
28534
28659
  * Gets the variance style based on value and sentiment direction.
28535
28660
  * pos suffix = positive values are good (green), negative are bad (red)
@@ -28618,13 +28743,12 @@ function renderZigzagBreak(breakX, breakGap, yTop, barHeight, s) {
28618
28743
  * exactly `columnWidthEm` em - see barColumnGeometry.ts.
28619
28744
  */
28620
28745
  const AbsoluteVarianceBar = (props) => {
28621
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
28746
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
28622
28747
  const s = styles.scaleFactor;
28623
28748
  const fs = styles.fontSize;
28624
- // Adaptive axis placement based on data range
28625
- const hasNeg = columnStats.min < 0;
28626
- const hasPos = columnStats.max > 0;
28627
- const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, hasNeg, hasPos);
28749
+ const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
28750
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, false));
28751
+ const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, sides);
28628
28752
  // Direct unit scaling via fontSize
28629
28753
  const scaledWidth = Math.abs(value) * styles.unitScaling * fs;
28630
28754
  // auto scaling puts the largest bar on the cut, float rounding must not turn it into an overdrive bar
@@ -28637,7 +28761,7 @@ const AbsoluteVarianceBar = (props) => {
28637
28761
  const padding = 2 * s;
28638
28762
  const barHeight = height - padding * 2;
28639
28763
  const barY = padding;
28640
- const isPositive = value >= 0;
28764
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28641
28765
  // Get style based on column type (scenario vs variance)
28642
28766
  const look = getVarianceLook(scenario, styles);
28643
28767
  const sentimentStyle = getBarStyle(scenario, value, isPositiveGood, styles);
@@ -28657,7 +28781,6 @@ const AbsoluteVarianceBar = (props) => {
28657
28781
  const fillValue = getBarFill(barStyle, patternPrefix);
28658
28782
  // a hatched variance bar keeps an outline, the shared getBarStroke also serves the waterfall
28659
28783
  const { stroke: barStroke, strokeWidth: barStrokeWidth } = look && barStyle.fillpattern === "hatched" ? { stroke: barStyle.stroke, strokeWidth: 1 } : getBarStroke(barStyle);
28660
- const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
28661
28784
  // bars start at the outer edge of the thick variance axis
28662
28785
  const axisOffset = look ? (VARIANCE_AXIS_EM * styles.emPx) / 2 : 0;
28663
28786
  const rootX = isPositive ? axisX + axisOffset : axisX - axisOffset;
@@ -28674,7 +28797,7 @@ const AbsoluteVarianceBar = (props) => {
28674
28797
  * Renders a relative variance pin chart (dr_ prefix).
28675
28798
  * Line from the axis in the sentiment colour, ending in a square head in the look of the
28676
28799
  * minuend, with the value label beside the head like an absolute bar label. A pin beyond
28677
- * the pin area is cut and ends in an arrow, its label inside the pin before the arrow.
28800
+ * the pin area is cut and ends in an arrow, its label after the arrow tip.
28678
28801
  *
28679
28802
  * Layout: [left label area][pin area with axis][right label area], summing to exactly
28680
28803
  * `columnWidthEm` em - see barColumnGeometry.ts.
@@ -28682,40 +28805,34 @@ const AbsoluteVarianceBar = (props) => {
28682
28805
  */
28683
28806
  const RelativeVariancePin = (props) => {
28684
28807
  var _a, _b;
28685
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
28808
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, sideMax, styles = DEFAULT_STYLES, } = props;
28686
28809
  const s = styles.scaleFactor;
28687
28810
  const fs = styles.fontSize;
28688
- // Adaptive axis placement based on data range
28689
- const hasNeg = columnStats.min < 0;
28690
- const hasPos = columnStats.max > 0;
28691
- const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, hasNeg, hasPos);
28811
+ const headSize = PIN_HEAD_SIZE * s;
28812
+ const pinPx = 100 * styles.percentageScaling * fs;
28813
+ const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits, percentSign);
28814
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, true));
28815
+ const { neg = Math.max(0, -columnStats.min), pos = Math.max(0, columnStats.max) } = sideMax !== null && sideMax !== void 0 ? sideMax : {};
28816
+ const { totalWidth, negPinLength, posPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize, { neg: neg * pinPx, pos: pos * pinPx });
28692
28817
  const centerY = height / 2;
28693
28818
  // Direct unit scaling via fontSize
28694
- const percentValue = Math.abs(value) * 100;
28695
- const scaledLength = percentValue * styles.percentageScaling * fs;
28696
- const isExtreme = scaledLength > maxPinLength;
28819
+ const scaledLength = Math.abs(value) * pinPx;
28820
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28821
+ const maxPinLength = isPositive ? posPinLength : negPinLength;
28822
+ // auto scaling puts the longest pin on the limit, float rounding must not cut it
28823
+ const isExtreme = scaledLength - maxPinLength > 1e-6;
28697
28824
  const pinLength = Math.min(scaledLength, maxPinLength);
28698
- // a zero label goes to the side that has a label gutter
28699
- const isPositive = value > 0 || (value === 0 && (hasPos || !hasNeg));
28700
28825
  const direction = isPositive ? 1 : -1;
28701
28826
  const pinEndX = axisX + direction * pinLength;
28702
28827
  // Get style based on column type (scenario vs variance)
28703
28828
  const barStyle = getBarStyle(scenario, value, isPositiveGood, styles);
28704
28829
  const look = getVarianceLook(scenario, styles);
28705
28830
  const headStyle = (_a = look === null || look === void 0 ? void 0 : look.minuend) !== null && _a !== void 0 ? _a : styles.scenarios.ac;
28706
- const headSize = PIN_HEAD_SIZE * s;
28707
- const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits);
28708
- // the arrow tip lands on the outer edge of a full-length head
28831
+ // the arrow tip lands on the outer edge of a full-length head, so the label after it stays in the cell
28709
28832
  const tipX = axisX + direction * (maxPinLength + headSize / 2);
28710
28833
  const arrowBaseX = tipX - direction * headSize;
28711
- const labelFitsInside = maxPinLength - headSize / 2 - labelGap >= displayValue.length * LABEL_CHAR_WIDTH * fs;
28712
- const labelInside = isExtreme && labelFitsInside;
28713
- let labelX = pinEndX + direction * (headSize / 2 + labelGap);
28714
- if (isExtreme) {
28715
- labelX = labelInside ? arrowBaseX - direction * labelGap : tipX + direction * labelGap;
28716
- }
28717
- const labelAtStart = isPositive !== labelInside;
28718
- 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)));
28834
+ const labelX = (isExtreme ? tipX : pinEndX + direction * (headSize / 2)) + direction * labelGap;
28835
+ 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)));
28719
28836
  };
28720
28837
  /**
28721
28838
  * CellBarChart - Renders IBCS-compliant variance bar charts.
@@ -31219,9 +31336,23 @@ const BnTableRenderer = class {
31219
31336
  /** @internal */
31220
31337
  this.scaleFactor = 1;
31221
31338
  this.componentDataFormatDigits = new DataFormatDigits();
31339
+ this.barColumnLayouts = new Map();
31340
+ // labels measured in the fallback font are too narrow or too wide once the web font is there: lay out again
31341
+ this.relayout = () => {
31342
+ if (this.barColumnLayouts.size > 0)
31343
+ forceUpdate(this);
31344
+ };
31222
31345
  withComponentStatus(this);
31223
31346
  this.componentStyle = withComponentStyle(this);
31224
31347
  }
31348
+ connectedCallback() {
31349
+ var _a;
31350
+ (_a = document.fonts) === null || _a === void 0 ? void 0 : _a.addEventListener("loadingdone", this.relayout);
31351
+ }
31352
+ disconnectedCallback() {
31353
+ var _a;
31354
+ (_a = document.fonts) === null || _a === void 0 ? void 0 : _a.removeEventListener("loadingdone", this.relayout);
31355
+ }
31225
31356
  /**
31226
31357
  * Collects the table's layout-state contribution: header/body regions,
31227
31358
  * counts, and - with `detail: "full"` - per-cell geometry paired with the
@@ -31339,14 +31470,15 @@ const BnTableRenderer = class {
31339
31470
  return h("div", { class: "empty-state" }, errorMsg);
31340
31471
  }
31341
31472
  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;
31342
- 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 !== "-"
31343
- ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
31344
- : {})), cell.prefix != null ? (h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
31345
- cell.columnStats &&
31346
- cell.value !== NOT_AVAILABLE &&
31347
- (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",
31348
- // percentage points: the fraction times 100, no measure scale
31349
- 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)))))))))));
31473
+ 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) => {
31474
+ var _a, _b, _c, _d, _e;
31475
+ 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 !== "-"
31476
+ ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
31477
+ : {})), cell.prefix != null ? (h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
31478
+ cell.columnStats &&
31479
+ cell.value !== NOT_AVAILABLE &&
31480
+ (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))));
31481
+ })))))));
31350
31482
  };
31351
31483
  return h(Host, { class: { "no-data": ((_a = this.table) === null || _a === void 0 ? void 0 : _a.hasNoData()) === true } }, helper());
31352
31484
  }
@@ -31391,6 +31523,7 @@ const BnTableRenderer = class {
31391
31523
  else {
31392
31524
  (_h = this.errors) === null || _h === void 0 ? void 0 : _h.remove("WARN_orderBucket");
31393
31525
  }
31526
+ this.barColumnLayouts = this.collectBarColumnLayouts();
31394
31527
  this.barChartStyles = this.prepareBarChartStyles();
31395
31528
  }
31396
31529
  prepareConfig(db) {
@@ -31445,7 +31578,7 @@ const BnTableRenderer = class {
31445
31578
  });
31446
31579
  }
31447
31580
  return this.componentStyle(($) => {
31448
- 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;
31581
+ 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;
31449
31582
  const varianceStyles = (_a = $["bn-chart-time"]) === null || _a === void 0 ? void 0 : _a["varianceStyles"];
31450
31583
  const barStyles = (_b = $["bn-chart-time"]) === null || _b === void 0 ? void 0 : _b["barStyles"];
31451
31584
  // Default variance styles with fillpattern
@@ -31499,13 +31632,17 @@ const BnTableRenderer = class {
31499
31632
  fontColor: ((_z = $["global"]) === null || _z === void 0 ? void 0 : _z["fontColor"]) || "#000000",
31500
31633
  fontSize: (((_0 = $["global"]) === null || _0 === void 0 ? void 0 : _0["fontSizePx"]) || 13.3333) * BAR_LABEL_FONT_RATIO * this.scaleFactor,
31501
31634
  emPx: (((_1 = $["global"]) === null || _1 === void 0 ? void 0 : _1["fontSizePx"]) || 13.3333) * this.scaleFactor,
31502
- fontFamily: ((_2 = $["global"]) === null || _2 === void 0 ? void 0 : _2["fontFamily"]) || "'Noto Sans', sans-serif",
31635
+ fontFamily: this.labelFontFamily(),
31503
31636
  unitScaling: resolvedUnit,
31504
31637
  percentageScaling: resolvedPercent,
31505
31638
  scaleFactor: this.scaleFactor,
31506
31639
  };
31507
31640
  });
31508
31641
  }
31642
+ /** Font family of the bar and pin labels, also used to measure them. */
31643
+ labelFontFamily() {
31644
+ return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontFamily"]) || "'Noto Sans', sans-serif"; });
31645
+ }
31509
31646
  /** The table's true em in px - the unit `.category-wN` and the bar columns share. */
31510
31647
  emPx() {
31511
31648
  return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontSizePx"]) || 13.3333; }) * this.scaleFactor;
@@ -31513,67 +31650,114 @@ const BnTableRenderer = class {
31513
31650
  /**
31514
31651
  * Auto-calculate unitScaling so the largest absolute variance bar
31515
31652
  * fills ~80% of its side of the bar area, without tripping the overdrive break.
31653
+ * One scaling for all columns, so it is the smallest that fits every column.
31516
31654
  */
31517
31655
  autoUnitScaling() {
31518
31656
  const emPx = this.emPx();
31519
31657
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31520
- const { absMax, hasNeg, hasPos } = this.getAbsBarColumnStats();
31521
- if (absMax <= 0 || fs <= 0) {
31522
- return 0.01;
31658
+ let scaling = Infinity;
31659
+ for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
31660
+ if (isRelative || absMax <= 0)
31661
+ continue;
31662
+ const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, labels);
31663
+ scaling = Math.min(scaling, Math.min(maxBarWidth * 0.8, cutWidth) / (absMax * fs));
31523
31664
  }
31524
- const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
31525
- const targetWidth = Math.min(maxBarWidth * 0.8, cutWidth);
31526
- return targetWidth / (absMax * fs);
31665
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.01;
31527
31666
  }
31528
31667
  /**
31529
- * Auto-calculate percentageScaling so the largest relative variance pin
31530
- * fills ~80% of the pin area.
31668
+ * Auto-calculate percentageScaling so the longest relative variance pin, its head and
31669
+ * its label exactly fill the column. One scaling for all columns, the smallest that fits.
31531
31670
  */
31532
31671
  autoPercentageScaling() {
31672
+ var _a, _b;
31533
31673
  const emPx = this.emPx();
31534
31674
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31535
- const { absMax: maxPct, hasNeg, hasPos } = this.getRelativeBarColumnStats();
31536
- if (maxPct <= 0 || fs <= 0) {
31537
- return 0.5;
31675
+ let scaling = Infinity;
31676
+ for (const { isRelative, absMax, labels, sideMax } of this.barColumnLayouts.values()) {
31677
+ if (!isRelative || absMax <= 0)
31678
+ continue;
31679
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31680
+ // the longest pins of both sides share the room, each side takes what its pins need
31681
+ const { negPinLength, posPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
31682
+ const percent = (((_a = sideMax.neg) !== null && _a !== void 0 ? _a : 0) + ((_b = sideMax.pos) !== null && _b !== void 0 ? _b : 0)) * 100;
31683
+ scaling = Math.min(scaling, (negPinLength + posPinLength) / (percent * fs));
31538
31684
  }
31539
- const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
31540
- return (maxPinLength * 0.8) / (maxPct * fs);
31685
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.5;
31541
31686
  }
31542
31687
  /**
31543
- * Maximum value and sign coverage across all absolute variance columns (d_*) in the table body.
31544
- * Sign coverage decides how much of the column the bar area gets (see barColumnGeometry.ts);
31545
- * OR-ing it across columns keeps the single shared scaling inside the narrowest bar area.
31688
+ * Largest value and widest label per side of every bar and pin column, from the cells
31689
+ * that draw one: total rows and n/a draw none. Interval buckets share the column key,
31690
+ * so they share the layout and the axis.
31546
31691
  */
31547
- getAbsBarColumnStats() {
31548
- // waterfall cells scale independently and must not skew variance auto-scaling
31549
- return this.collectBarColumnStats((cell) => !cell.key.startsWith("dr") && !cell.waterfall, 1);
31550
- }
31551
- /**
31552
- * Same for the relative variance columns (dr_*). Values are stored as decimals
31553
- * (e.g. 0.2 = 20%), returned as percentage (20).
31554
- */
31555
- getRelativeBarColumnStats() {
31556
- return this.collectBarColumnStats((cell) => cell.key.startsWith("dr"), 100);
31557
- }
31558
- collectBarColumnStats(accept, valueFactor) {
31559
- const stats = { absMax: 0, hasNeg: false, hasPos: false };
31560
- if (!this.table)
31561
- return stats;
31692
+ collectBarColumnLayouts() {
31693
+ var _a, _b, _c, _d;
31694
+ const layouts = new Map();
31695
+ if (!this.table || this.table.hasNoData())
31696
+ return layouts;
31697
+ const cellsByKey = new Map();
31562
31698
  for (const row of this.table.getBody()) {
31563
31699
  if (row.type === TableRowType.SUM)
31564
31700
  continue;
31565
31701
  for (const cell of row.getCells()) {
31566
- if (!cell.isBarChart || !cell.columnStats || !accept(cell))
31702
+ // waterfall cells scale independently
31703
+ if (!cell.isBarChart || !cell.columnStats || cell.waterfall !== undefined)
31704
+ continue;
31705
+ if (typeof cell.value !== "number" || isNaN(cell.value))
31567
31706
  continue;
31568
- stats.absMax = Math.max(stats.absMax, cell.columnStats.absMax * valueFactor);
31569
- stats.hasNeg = stats.hasNeg || cell.columnStats.min < 0;
31570
- stats.hasPos = stats.hasPos || cell.columnStats.max > 0;
31707
+ cellsByKey.set(cell.key, [...((_a = cellsByKey.get(cell.key)) !== null && _a !== void 0 ? _a : []), cell]);
31571
31708
  }
31572
31709
  }
31573
- return stats;
31710
+ const emPx = this.emPx();
31711
+ const fs = emPx * BAR_LABEL_FONT_RATIO;
31712
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31713
+ 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;
31714
+ const fontFamily = this.labelFontFamily();
31715
+ for (const [key, cells] of cellsByKey) {
31716
+ const isRelative = key.startsWith("dr");
31717
+ const values = cells.map((cell) => cell.value);
31718
+ const measure = (percentDigits, percentSign) => {
31719
+ var _a;
31720
+ const labels = {
31721
+ neg: values.some((value) => value < 0) ? 0 : undefined,
31722
+ pos: values.some((value) => value > 0) ? 0 : undefined,
31723
+ };
31724
+ const zeroPositive = zeroOnPositiveSide(labels);
31725
+ for (const cell of cells) {
31726
+ const value = cell.value;
31727
+ const side = value > 0 || (value === 0 && zeroPositive) ? "pos" : "neg";
31728
+ const text = formatValue(value, isRelative, barLabelMeasureScale(cell), locale, percentDigits, percentSign);
31729
+ labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, labelWidth(text, fs, fontFamily, isRelative));
31730
+ }
31731
+ return labels;
31732
+ };
31733
+ // a narrow pin column drops percent digits, then the %, so every label keeps its sign
31734
+ let percentDigits = Math.min(20, Math.max(0, Math.trunc(this.componentDataFormatDigits.percent) || 0));
31735
+ let percentSign = true;
31736
+ let labels = measure(percentDigits, percentSign);
31737
+ while (isRelative && (percentDigits > 0 || percentSign)) {
31738
+ if (pinLabelsFit(this.barColumnMaxWidth, emPx, labels, headSize))
31739
+ break;
31740
+ if (percentDigits > 0)
31741
+ percentDigits--;
31742
+ else
31743
+ percentSign = false;
31744
+ labels = measure(percentDigits, percentSign);
31745
+ }
31746
+ const absMax = Math.max(...values.map(Math.abs)) * (isRelative ? 100 : 1);
31747
+ const sideMax = {
31748
+ neg: labels.neg === undefined ? undefined : Math.max(0, ...values.map((value) => -value)),
31749
+ pos: labels.pos === undefined ? undefined : Math.max(0, ...values),
31750
+ };
31751
+ layouts.set(key, { isRelative, absMax, labels, sideMax, percentDigits, percentSign });
31752
+ }
31753
+ return layouts;
31574
31754
  }
31575
31755
  get element() { return getElement(this); }
31576
31756
  };
31757
+ /** Percentage points: the fraction times 100, no measure scale. */
31758
+ function barLabelMeasureScale(cell) {
31759
+ return cell.type === "percentpoint" ? 0.01 : cell.measureScale;
31760
+ }
31577
31761
  BnTableRenderer.style = bnTableRendererCss();
31578
31762
 
31579
31763
  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)}`;