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