@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.
@@ -28309,60 +28309,103 @@ 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
+ }
28324
28354
  /** The SVG fills the cell's content box: the requested column width minus the cell padding. */
28325
28355
  function contentWidth(widthEm, emPx) {
28326
28356
  return Math.max(0, (widthEm - BAR_CELL_PADDING_EM) * emPx);
28327
28357
  }
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));
28358
+ /** `outerReserve` covers what sticks out beyond the bar or pin end, e.g. the half head. */
28359
+ function sideNeeds(labels, outerReserve, labelGap) {
28360
+ const hasNeg = labels.neg !== undefined;
28361
+ const hasPos = labels.pos !== undefined || !hasNeg;
28362
+ const need = (has, width) => (has ? outerReserve + labelGap + (width !== null && width !== void 0 ? width : 0) : 0);
28363
+ return { sides: (hasNeg ? 1 : 0) + (hasPos ? 1 : 0), neg: need(hasNeg, labels.neg), pos: need(hasPos, labels.pos) };
28364
+ }
28365
+ /** Bar or pin extent per side that a column keeps as long as its labels allow. */
28366
+ function minExtent(totalWidth, emPx, sides) {
28367
+ return Math.min(MIN_EXTENT_EM * emPx, totalWidth / (2 * sides));
28368
+ }
28369
+ /**
28370
+ * Reserves per side only what its widest label needs and gives the rest to the bars or
28371
+ * pins. A column too narrow for that shortens the bars or pins first, down to zero, so a
28372
+ * label keeps its sign. Only a column narrower than its labels alone shrinks the
28373
+ * reserves, and then the labels overflow. wN stays wN in every case.
28374
+ */
28375
+ function splitSides(totalWidth, labels, outerReserve, labelGap) {
28376
+ const { sides, neg, pos } = sideNeeds(labels, outerReserve, labelGap);
28377
+ const shrink = neg + pos > 0 ? Math.min(1, totalWidth / (neg + pos)) : 1;
28378
+ const extent = Math.max(0, (totalWidth - (neg + pos) * shrink) / sides);
28379
+ return { extent, axisX: labels.neg !== undefined ? neg * shrink + extent : 0 };
28380
+ }
28381
+ /** True when the labels fit beside pins of at least the minimum extent; else the caller drops digits. */
28382
+ function pinLabelsFit(widthEm, emPx, labels, headSize) {
28383
+ const totalWidth = contentWidth(widthEm, emPx);
28384
+ const { sides, neg, pos } = sideNeeds(labels, headSize / 2, LABEL_GAP_EM * emPx);
28385
+ return neg + pos + sides * minExtent(totalWidth, emPx, sides) <= totalWidth + 1e-6;
28331
28386
  }
28332
- function absoluteBarGeometry(widthEm, emPx, hasNeg, hasPos) {
28387
+ function absoluteBarGeometry(widthEm, emPx, labels) {
28333
28388
  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;
28389
+ const labelGap = LABEL_GAP_EM * emPx;
28390
+ // a variance bar starts at the outer edge of the thick axis
28391
+ const { extent: maxBarWidth, axisX } = splitSides(totalWidth, labels, (VARIANCE_AXIS_EM * emPx) / 2, labelGap);
28338
28392
  const breakGap = Math.min(BREAK_GAP_EM * emPx, maxBarWidth * 0.2);
28339
28393
  const overdriveZoneWidth = maxBarWidth * OVERDRIVE_FRACTION;
28340
28394
  return {
28341
28395
  totalWidth,
28342
- labelAreaWidth,
28343
28396
  maxBarWidth,
28344
- axisX: hasNeg ? labelAreaWidth + maxBarWidth : 0,
28397
+ axisX,
28345
28398
  breakGap,
28346
28399
  overdriveZoneWidth,
28347
28400
  cutWidth: Math.max(0, maxBarWidth - breakGap - overdriveZoneWidth),
28348
- labelGap: LABEL_GAP_EM * emPx,
28401
+ labelGap,
28349
28402
  };
28350
28403
  }
28351
- function relativePinGeometry(widthEm, emPx, hasNeg, hasPos) {
28404
+ function relativePinGeometry(widthEm, emPx, labels, headSize) {
28352
28405
  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);
28358
- return {
28359
- totalWidth,
28360
- labelAreaWidth,
28361
- maxPinLength: Math.max(0, sideWidth - pinPadding),
28362
- pinPadding,
28363
- axisX: hasNeg ? labelAreaWidth + sideWidth : 0,
28364
- labelGap: LABEL_GAP_EM * emPx,
28365
- };
28406
+ const labelGap = LABEL_GAP_EM * emPx;
28407
+ const { extent, axisX } = splitSides(totalWidth, labels, headSize / 2, labelGap);
28408
+ return { totalWidth, maxPinLength: extent, axisX, labelGap };
28366
28409
  }
28367
28410
  function waterfallGeometry(widthEm, emPx) {
28368
28411
  const totalWidth = contentWidth(widthEm, emPx);
@@ -28397,13 +28440,8 @@ const DEFAULT_STYLES = {
28397
28440
  scaleFactor: 1,
28398
28441
  };
28399
28442
  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;
28443
+ /** Width of the pin line, in px before scaleFactor. */
28404
28444
  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
28445
  /**
28408
28446
  * Creates a unique pattern ID for the SVG defs
28409
28447
  */
@@ -28428,16 +28466,20 @@ const PatternDefs = ({ styles }) => {
28428
28466
  ];
28429
28467
  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
28468
  };
28431
- /** Characters that fit the pin label gutter (PIN_LABEL_EM), the cell cuts off the rest. */
28469
+ /** Longest relative label that keeps all percent digits; a longer one drops digits so its gutter does not eat the pins. */
28432
28470
  const PIN_LABEL_CHARS = 7;
28433
28471
  /**
28434
28472
  * Formats a number for display with appropriate SI prefix.
28435
28473
  */
28436
- function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0) {
28437
- const prefix = value >= 0 ? "+" : "";
28474
+ function formatValue(value, isRelative, measureScale = 1, locale, percentDigits = 0, percentSign = true) {
28475
+ // IBCS prints a value that rounds to zero without a sign
28476
+ const signed = (text, zero) => (value > 0 && text !== zero ? "+" : "") + text;
28438
28477
  if (isRelative) {
28439
28478
  // Relative variance is stored as decimal (e.g., -2.0 = -200%)
28440
- const format = (digits) => prefix + Formatter.number(value, "percent", digits, digits, 1, locale);
28479
+ const number = (v, digits) => percentSign
28480
+ ? Formatter.number(v, "percent", digits, digits, 1, locale)
28481
+ : Formatter.number(v * 100, "decimal", digits, digits, 1, locale);
28482
+ const format = (digits) => signed(number(value, digits), number(0, digits));
28441
28483
  // Intl throws on a digit count outside 0 to 20
28442
28484
  let digits = Math.min(20, Math.max(0, Math.trunc(percentDigits) || 0));
28443
28485
  while (digits > 0 && format(digits).length > PIN_LABEL_CHARS) {
@@ -28464,7 +28506,7 @@ function formatValue(value, isRelative, measureScale = 1, locale, percentDigits
28464
28506
  suffix = "K";
28465
28507
  }
28466
28508
  const digits = absValue >= unit * 10 ? 0 : 1;
28467
- return prefix + Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale) + suffix;
28509
+ return (signed(Formatter.number(scaledValue / unit, "decimal", digits, 0, 1, locale), Formatter.number(0, "decimal", digits, 0, 1, locale)) + suffix);
28468
28510
  }
28469
28511
  /**
28470
28512
  * Checks if the column is a scenario column (ac, pp, fc, pl) vs variance (d_, dr_).
@@ -28530,6 +28572,17 @@ const VarianceAxis = ({ x, height, look, styles }) => {
28530
28572
  }
28531
28573
  return h("g", { class: `variance-axis axis-${referenceSlot}` }, lines);
28532
28574
  };
28575
+ /** A zero value is drawn on the positive side, unless the column only has negative values. */
28576
+ function zeroOnPositiveSide(labels) {
28577
+ return labels.pos !== undefined || labels.neg === undefined;
28578
+ }
28579
+ /** Side coverage from the column statistics, with only this cell's label as the width. */
28580
+ function ownLabelWidths(columnStats, width) {
28581
+ return {
28582
+ neg: columnStats.min < 0 ? width : undefined,
28583
+ pos: columnStats.max > 0 ? width : undefined,
28584
+ };
28585
+ }
28533
28586
  /**
28534
28587
  * Gets the variance style based on value and sentiment direction.
28535
28588
  * pos suffix = positive values are good (green), negative are bad (red)
@@ -28618,13 +28671,12 @@ function renderZigzagBreak(breakX, breakGap, yTop, barHeight, s) {
28618
28671
  * exactly `columnWidthEm` em - see barColumnGeometry.ts.
28619
28672
  */
28620
28673
  const AbsoluteVarianceBar = (props) => {
28621
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
28674
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
28622
28675
  const s = styles.scaleFactor;
28623
28676
  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);
28677
+ const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
28678
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
28679
+ const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, sides);
28628
28680
  // Direct unit scaling via fontSize
28629
28681
  const scaledWidth = Math.abs(value) * styles.unitScaling * fs;
28630
28682
  // auto scaling puts the largest bar on the cut, float rounding must not turn it into an overdrive bar
@@ -28637,7 +28689,7 @@ const AbsoluteVarianceBar = (props) => {
28637
28689
  const padding = 2 * s;
28638
28690
  const barHeight = height - padding * 2;
28639
28691
  const barY = padding;
28640
- const isPositive = value >= 0;
28692
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28641
28693
  // Get style based on column type (scenario vs variance)
28642
28694
  const look = getVarianceLook(scenario, styles);
28643
28695
  const sentimentStyle = getBarStyle(scenario, value, isPositiveGood, styles);
@@ -28657,7 +28709,6 @@ const AbsoluteVarianceBar = (props) => {
28657
28709
  const fillValue = getBarFill(barStyle, patternPrefix);
28658
28710
  // a hatched variance bar keeps an outline, the shared getBarStroke also serves the waterfall
28659
28711
  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
28712
  // bars start at the outer edge of the thick variance axis
28662
28713
  const axisOffset = look ? (VARIANCE_AXIS_EM * styles.emPx) / 2 : 0;
28663
28714
  const rootX = isPositive ? axisX + axisOffset : axisX - axisOffset;
@@ -28674,7 +28725,7 @@ const AbsoluteVarianceBar = (props) => {
28674
28725
  * Renders a relative variance pin chart (dr_ prefix).
28675
28726
  * Line from the axis in the sentiment colour, ending in a square head in the look of the
28676
28727
  * 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.
28728
+ * the pin area is cut and ends in an arrow, its label after the arrow tip.
28678
28729
  *
28679
28730
  * Layout: [left label area][pin area with axis][right label area], summing to exactly
28680
28731
  * `columnWidthEm` em - see barColumnGeometry.ts.
@@ -28682,40 +28733,32 @@ const AbsoluteVarianceBar = (props) => {
28682
28733
  */
28683
28734
  const RelativeVariancePin = (props) => {
28684
28735
  var _a, _b;
28685
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, columnWidthEm = 10, styles = DEFAULT_STYLES, } = props;
28736
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
28686
28737
  const s = styles.scaleFactor;
28687
28738
  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);
28739
+ const headSize = PIN_HEAD_SIZE * s;
28740
+ const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits, percentSign);
28741
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
28742
+ const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize);
28692
28743
  const centerY = height / 2;
28693
28744
  // Direct unit scaling via fontSize
28694
28745
  const percentValue = Math.abs(value) * 100;
28695
28746
  const scaledLength = percentValue * styles.percentageScaling * fs;
28696
- const isExtreme = scaledLength > maxPinLength;
28747
+ // auto scaling puts the longest pin on the limit, float rounding must not cut it
28748
+ const isExtreme = scaledLength - maxPinLength > 1e-6;
28697
28749
  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));
28750
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28700
28751
  const direction = isPositive ? 1 : -1;
28701
28752
  const pinEndX = axisX + direction * pinLength;
28702
28753
  // Get style based on column type (scenario vs variance)
28703
28754
  const barStyle = getBarStyle(scenario, value, isPositiveGood, styles);
28704
28755
  const look = getVarianceLook(scenario, styles);
28705
28756
  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
28757
+ // the arrow tip lands on the outer edge of a full-length head, so the label after it stays in the cell
28709
28758
  const tipX = axisX + direction * (maxPinLength + headSize / 2);
28710
28759
  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)));
28760
+ const labelX = (isExtreme ? tipX : pinEndX + direction * (headSize / 2)) + direction * labelGap;
28761
+ 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
28762
  };
28720
28763
  /**
28721
28764
  * CellBarChart - Renders IBCS-compliant variance bar charts.
@@ -31219,6 +31262,7 @@ const BnTableRenderer = class {
31219
31262
  /** @internal */
31220
31263
  this.scaleFactor = 1;
31221
31264
  this.componentDataFormatDigits = new DataFormatDigits();
31265
+ this.barColumnLayouts = new Map();
31222
31266
  withComponentStatus(this);
31223
31267
  this.componentStyle = withComponentStyle(this);
31224
31268
  }
@@ -31339,14 +31383,15 @@ const BnTableRenderer = class {
31339
31383
  return h("div", { class: "empty-state" }, errorMsg);
31340
31384
  }
31341
31385
  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)))))))))));
31386
+ 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) => {
31387
+ var _a, _b, _c, _d;
31388
+ 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 !== "-"
31389
+ ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
31390
+ : {})), cell.prefix != null ? (h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
31391
+ cell.columnStats &&
31392
+ cell.value !== NOT_AVAILABLE &&
31393
+ (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, styles: this.barChartStyles }))) : (cell.draw(this.componentDataFormatDigits, locale))));
31394
+ })))))));
31350
31395
  };
31351
31396
  return h(Host, { class: { "no-data": ((_a = this.table) === null || _a === void 0 ? void 0 : _a.hasNoData()) === true } }, helper());
31352
31397
  }
@@ -31391,6 +31436,7 @@ const BnTableRenderer = class {
31391
31436
  else {
31392
31437
  (_h = this.errors) === null || _h === void 0 ? void 0 : _h.remove("WARN_orderBucket");
31393
31438
  }
31439
+ this.barColumnLayouts = this.collectBarColumnLayouts();
31394
31440
  this.barChartStyles = this.prepareBarChartStyles();
31395
31441
  }
31396
31442
  prepareConfig(db) {
@@ -31513,67 +31559,106 @@ const BnTableRenderer = class {
31513
31559
  /**
31514
31560
  * Auto-calculate unitScaling so the largest absolute variance bar
31515
31561
  * fills ~80% of its side of the bar area, without tripping the overdrive break.
31562
+ * One scaling for all columns, so it is the smallest that fits every column.
31516
31563
  */
31517
31564
  autoUnitScaling() {
31518
31565
  const emPx = this.emPx();
31519
31566
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31520
- const { absMax, hasNeg, hasPos } = this.getAbsBarColumnStats();
31521
- if (absMax <= 0 || fs <= 0) {
31522
- return 0.01;
31567
+ let scaling = Infinity;
31568
+ for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
31569
+ if (isRelative || absMax <= 0)
31570
+ continue;
31571
+ const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, labels);
31572
+ scaling = Math.min(scaling, Math.min(maxBarWidth * 0.8, cutWidth) / (absMax * fs));
31523
31573
  }
31524
- const { maxBarWidth, cutWidth } = absoluteBarGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
31525
- const targetWidth = Math.min(maxBarWidth * 0.8, cutWidth);
31526
- return targetWidth / (absMax * fs);
31574
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.01;
31527
31575
  }
31528
31576
  /**
31529
- * Auto-calculate percentageScaling so the largest relative variance pin
31530
- * fills ~80% of the pin area.
31577
+ * Auto-calculate percentageScaling so the longest relative variance pin, its head and
31578
+ * its label exactly fill the column. One scaling for all columns, the smallest that fits.
31531
31579
  */
31532
31580
  autoPercentageScaling() {
31533
31581
  const emPx = this.emPx();
31534
31582
  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;
31583
+ let scaling = Infinity;
31584
+ for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
31585
+ if (!isRelative || absMax <= 0)
31586
+ continue;
31587
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31588
+ const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
31589
+ scaling = Math.min(scaling, maxPinLength / (absMax * fs));
31538
31590
  }
31539
- const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, hasNeg, hasPos);
31540
- return (maxPinLength * 0.8) / (maxPct * fs);
31541
- }
31542
- /**
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.
31546
- */
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);
31591
+ return Number.isFinite(scaling) && fs > 0 ? scaling : 0.5;
31550
31592
  }
31551
31593
  /**
31552
- * Same for the relative variance columns (dr_*). Values are stored as decimals
31553
- * (e.g. 0.2 = 20%), returned as percentage (20).
31594
+ * Largest value and widest label per side of every bar and pin column, from the cells
31595
+ * that draw one: total rows and n/a draw none. Interval buckets share the column key,
31596
+ * so they share the layout and the axis.
31554
31597
  */
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;
31598
+ collectBarColumnLayouts() {
31599
+ var _a, _b, _c, _d;
31600
+ const layouts = new Map();
31601
+ if (!this.table || this.table.hasNoData())
31602
+ return layouts;
31603
+ const cellsByKey = new Map();
31562
31604
  for (const row of this.table.getBody()) {
31563
31605
  if (row.type === TableRowType.SUM)
31564
31606
  continue;
31565
31607
  for (const cell of row.getCells()) {
31566
- if (!cell.isBarChart || !cell.columnStats || !accept(cell))
31608
+ // waterfall cells scale independently
31609
+ if (!cell.isBarChart || !cell.columnStats || cell.waterfall !== undefined)
31567
31610
  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;
31611
+ if (typeof cell.value !== "number" || isNaN(cell.value))
31612
+ continue;
31613
+ cellsByKey.set(cell.key, [...((_a = cellsByKey.get(cell.key)) !== null && _a !== void 0 ? _a : []), cell]);
31571
31614
  }
31572
31615
  }
31573
- return stats;
31616
+ const emPx = this.emPx();
31617
+ const fs = emPx * BAR_LABEL_FONT_RATIO;
31618
+ const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31619
+ 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;
31620
+ for (const [key, cells] of cellsByKey) {
31621
+ const isRelative = key.startsWith("dr");
31622
+ const values = cells.map((cell) => cell.value);
31623
+ const measure = (percentDigits, percentSign) => {
31624
+ var _a;
31625
+ const labels = {
31626
+ neg: values.some((value) => value < 0) ? 0 : undefined,
31627
+ pos: values.some((value) => value > 0) ? 0 : undefined,
31628
+ };
31629
+ const zeroPositive = zeroOnPositiveSide(labels);
31630
+ for (const cell of cells) {
31631
+ const value = cell.value;
31632
+ const side = value > 0 || (value === 0 && zeroPositive) ? "pos" : "neg";
31633
+ const text = formatValue(value, isRelative, barLabelMeasureScale(cell), locale, percentDigits, percentSign);
31634
+ labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, estimateLabelWidth(text, fs));
31635
+ }
31636
+ return labels;
31637
+ };
31638
+ // a narrow pin column drops percent digits, then the %, so every label keeps its sign
31639
+ let percentDigits = Math.min(20, Math.max(0, Math.trunc(this.componentDataFormatDigits.percent) || 0));
31640
+ let percentSign = true;
31641
+ let labels = measure(percentDigits, percentSign);
31642
+ while (isRelative && (percentDigits > 0 || percentSign)) {
31643
+ if (pinLabelsFit(this.barColumnMaxWidth, emPx, labels, headSize))
31644
+ break;
31645
+ if (percentDigits > 0)
31646
+ percentDigits--;
31647
+ else
31648
+ percentSign = false;
31649
+ labels = measure(percentDigits, percentSign);
31650
+ }
31651
+ const absMax = Math.max(...values.map(Math.abs)) * (isRelative ? 100 : 1);
31652
+ layouts.set(key, { isRelative, absMax, labels, percentDigits, percentSign });
31653
+ }
31654
+ return layouts;
31574
31655
  }
31575
31656
  get element() { return getElement(this); }
31576
31657
  };
31658
+ /** Percentage points: the fraction times 100, no measure scale. */
31659
+ function barLabelMeasureScale(cell) {
31660
+ return cell.type === "percentpoint" ? 0.01 : cell.measureScale;
31661
+ }
31577
31662
  BnTableRenderer.style = bnTableRendererCss();
31578
31663
 
31579
31664
  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)}`;
@@ -128,6 +128,7 @@ export declare class BnTableRenderer {
128
128
  private table;
129
129
  private componentDataFormatDigits;
130
130
  private barChartStyles;
131
+ private barColumnLayouts;
131
132
  private stores;
132
133
  constructor();
133
134
  /**
@@ -153,23 +154,18 @@ export declare class BnTableRenderer {
153
154
  /**
154
155
  * Auto-calculate unitScaling so the largest absolute variance bar
155
156
  * fills ~80% of its side of the bar area, without tripping the overdrive break.
157
+ * One scaling for all columns, so it is the smallest that fits every column.
156
158
  */
157
159
  private autoUnitScaling;
158
160
  /**
159
- * Auto-calculate percentageScaling so the largest relative variance pin
160
- * fills ~80% of the pin area.
161
+ * Auto-calculate percentageScaling so the longest relative variance pin, its head and
162
+ * its label exactly fill the column. One scaling for all columns, the smallest that fits.
161
163
  */
162
164
  private autoPercentageScaling;
163
165
  /**
164
- * Maximum value and sign coverage across all absolute variance columns (d_*) in the table body.
165
- * Sign coverage decides how much of the column the bar area gets (see barColumnGeometry.ts);
166
- * OR-ing it across columns keeps the single shared scaling inside the narrowest bar area.
166
+ * Largest value and widest label per side of every bar and pin column, from the cells
167
+ * that draw one: total rows and n/a draw none. Interval buckets share the column key,
168
+ * so they share the layout and the axis.
167
169
  */
168
- private getAbsBarColumnStats;
169
- /**
170
- * Same for the relative variance columns (dr_*). Values are stored as decimals
171
- * (e.g. 0.2 = 20%), returned as percentage (20).
172
- */
173
- private getRelativeBarColumnStats;
174
- private collectBarColumnStats;
170
+ private collectBarColumnLayouts;
175
171
  }
@@ -1,6 +1,7 @@
1
1
  import { FunctionalComponent } from "../../../../stencil-public-runtime";
2
2
  import { ColumnStats } from "./Cell";
3
3
  import { FillPattern } from "../../../../utils/model/styles/SVGStyle";
4
+ import { SideLabels } from "../helper/barColumnGeometry";
4
5
  export interface VarianceStyle {
5
6
  stroke: string;
6
7
  fill: string;
@@ -42,8 +43,12 @@ export interface CellBarChartProps {
42
43
  measureScale?: number;
43
44
  locale?: string;
44
45
  percentDigits?: number;
46
+ /** False drops the % of a relative label, for a column too narrow for it. */
47
+ percentSign?: boolean;
45
48
  /** Total column width in table em - see barColumnGeometry.ts. */
46
49
  columnWidthEm?: number;
50
+ /** Widest label per side over the drawn cells of the column; without it only this cell's label counts. */
51
+ labelWidths?: SideLabels;
47
52
  styles?: CellBarChartStyles;
48
53
  }
49
54
  export declare const DEFAULT_STYLES: CellBarChartStyles;
@@ -57,11 +62,13 @@ export declare const PatternDefs: FunctionalComponent<{
57
62
  /**
58
63
  * Formats a number for display with appropriate SI prefix.
59
64
  */
60
- export declare function formatValue(value: number, isRelative: boolean, measureScale?: number, locale?: string, percentDigits?: number): string;
65
+ export declare function formatValue(value: number, isRelative: boolean, measureScale?: number, locale?: string, percentDigits?: number, percentSign?: boolean): string;
61
66
  /**
62
67
  * Gets the scenario type from the column name (ac, pp, fc, pl).
63
68
  */
64
69
  export declare function getScenarioType(scenario: string): "ac" | "pp" | "fc" | "pl";
70
+ /** A zero value is drawn on the positive side, unless the column only has negative values. */
71
+ export declare function zeroOnPositiveSide(labels: SideLabels): boolean;
65
72
  /**
66
73
  * Gets the appropriate style for a bar based on column type.
67
74
  * Scenarios (ac, pp, fc, pl) use scenario styles.
@@ -14,11 +14,22 @@
14
14
  * unitScaling / percentageScaling math, so changing it rescales every user-set value.
15
15
  */
16
16
  export declare const BAR_LABEL_FONT_RATIO = 0.85;
17
+ /** Width of the axis of a variance bar or pin, in em: near the 0.3em scenario marker in the header. */
18
+ export declare const VARIANCE_AXIS_EM = 0.25;
19
+ /** Side of the square pin head, in px before scaleFactor. */
20
+ export declare const PIN_HEAD_SIZE = 10;
21
+ /** Estimated rendered width of a value label in px, with a margin for wider fonts. */
22
+ export declare function estimateLabelWidth(text: string, fontSize: number): number;
23
+ /** Widest value label in px per side of the axis; a side without drawn values is undefined. */
24
+ export interface SideLabels {
25
+ neg?: number;
26
+ pos?: number;
27
+ }
28
+ /** True when the labels fit beside pins of at least the minimum extent; else the caller drops digits. */
29
+ export declare function pinLabelsFit(widthEm: number, emPx: number, labels: SideLabels, headSize: number): boolean;
17
30
  export interface AbsoluteBarGeometry {
18
31
  /** SVG width - the cell's content box. */
19
32
  totalWidth: number;
20
- /** Width of one value-label gutter; one is drawn per side in use. */
21
- labelAreaWidth: number;
22
33
  /** Max bar extent on one side of the axis. */
23
34
  maxBarWidth: number;
24
35
  axisX: number;
@@ -29,19 +40,16 @@ export interface AbsoluteBarGeometry {
29
40
  /** Gap between the bar end and its value label. */
30
41
  labelGap: number;
31
42
  }
32
- export declare function absoluteBarGeometry(widthEm: number, emPx: number, hasNeg: boolean, hasPos: boolean): AbsoluteBarGeometry;
43
+ export declare function absoluteBarGeometry(widthEm: number, emPx: number, labels: SideLabels): AbsoluteBarGeometry;
33
44
  export interface RelativePinGeometry {
34
45
  totalWidth: number;
35
- /** Width of one value-label gutter; one is drawn per side in use. */
36
- labelAreaWidth: number;
37
- /** Max pin extent on one side of the axis. */
46
+ /** Max pin extent on one side of the axis, to the centre of the head or the base of the arrow tip. */
38
47
  maxPinLength: number;
39
- pinPadding: number;
40
48
  axisX: number;
41
49
  /** Gap between the pin head and its value label. */
42
50
  labelGap: number;
43
51
  }
44
- export declare function relativePinGeometry(widthEm: number, emPx: number, hasNeg: boolean, hasPos: boolean): RelativePinGeometry;
52
+ export declare function relativePinGeometry(widthEm: number, emPx: number, labels: SideLabels, headSize: number): RelativePinGeometry;
45
53
  export interface WaterfallGeometry {
46
54
  totalWidth: number;
47
55
  /** One gutter on each side; a waterfall always spans its full range. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bino-bi/bn-template-engine",
3
- "version": "1.0.0-next.35",
3
+ "version": "1.0.0-next.36",
4
4
  "description": "IBCS-compliant web components for business intelligence tables and charts",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.js",