@bino-bi/bn-template-engine 1.0.0-next.36 → 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.
@@ -28353,6 +28353,43 @@ function estimateLabelWidth(text, fontSize) {
28353
28353
  }
28354
28354
  return em * fontSize * LABEL_WIDTH_MARGIN;
28355
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
+ }
28356
28393
  /** The SVG fills the cell's content box: the requested column width minus the cell padding. */
28357
28394
  function contentWidth(widthEm, emPx) {
28358
28395
  return Math.max(0, (widthEm - BAR_CELL_PADDING_EM) * emPx);
@@ -28403,11 +28440,46 @@ function absoluteBarGeometry(widthEm, emPx, labels) {
28403
28440
  labelGap,
28404
28441
  };
28405
28442
  }
28406
- function relativePinGeometry(widthEm, emPx, labels, headSize) {
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;
28407
28451
  const totalWidth = contentWidth(widthEm, emPx);
28408
28452
  const labelGap = LABEL_GAP_EM * emPx;
28409
28453
  const { extent, axisX } = splitSides(totalWidth, labels, headSize / 2, labelGap);
28410
- return { totalWidth, maxPinLength: extent, axisX, 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
+ }
28475
+ return {
28476
+ totalWidth,
28477
+ maxPinLength: Math.max(neg, pos),
28478
+ negPinLength: neg,
28479
+ posPinLength: pos,
28480
+ axisX: hasNeg ? axisX - extent + neg : 0,
28481
+ labelGap,
28482
+ };
28411
28483
  }
28412
28484
  function waterfallGeometry(widthEm, emPx) {
28413
28485
  const totalWidth = contentWidth(widthEm, emPx);
@@ -28677,7 +28749,7 @@ const AbsoluteVarianceBar = (props) => {
28677
28749
  const s = styles.scaleFactor;
28678
28750
  const fs = styles.fontSize;
28679
28751
  const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
28680
- const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
28752
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, false));
28681
28753
  const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, sides);
28682
28754
  // Direct unit scaling via fontSize
28683
28755
  const scaledWidth = Math.abs(value) * styles.unitScaling * fs;
@@ -28735,21 +28807,23 @@ const AbsoluteVarianceBar = (props) => {
28735
28807
  */
28736
28808
  const RelativeVariancePin = (props) => {
28737
28809
  var _a, _b;
28738
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
28810
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, sideMax, styles = DEFAULT_STYLES, } = props;
28739
28811
  const s = styles.scaleFactor;
28740
28812
  const fs = styles.fontSize;
28741
28813
  const headSize = PIN_HEAD_SIZE * s;
28814
+ const pinPx = 100 * styles.percentageScaling * fs;
28742
28815
  const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits, percentSign);
28743
- const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
28744
- const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize);
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 });
28745
28819
  const centerY = height / 2;
28746
28820
  // Direct unit scaling via fontSize
28747
- const percentValue = Math.abs(value) * 100;
28748
- const scaledLength = percentValue * styles.percentageScaling * fs;
28821
+ const scaledLength = Math.abs(value) * pinPx;
28822
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28823
+ const maxPinLength = isPositive ? posPinLength : negPinLength;
28749
28824
  // auto scaling puts the longest pin on the limit, float rounding must not cut it
28750
28825
  const isExtreme = scaledLength - maxPinLength > 1e-6;
28751
28826
  const pinLength = Math.min(scaledLength, maxPinLength);
28752
- const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
28753
28827
  const direction = isPositive ? 1 : -1;
28754
28828
  const pinEndX = axisX + direction * pinLength;
28755
28829
  // Get style based on column type (scenario vs variance)
@@ -31265,9 +31339,22 @@ const BnTableRenderer = class {
31265
31339
  this.scaleFactor = 1;
31266
31340
  this.componentDataFormatDigits = new DataFormatDigits();
31267
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
+ };
31268
31347
  withHasError.withComponentStatus(this);
31269
31348
  this.componentStyle = withStyled.withComponentStyle(this);
31270
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
+ }
31271
31358
  /**
31272
31359
  * Collects the table's layout-state contribution: header/body regions,
31273
31360
  * counts, and - with `detail: "full"` - per-cell geometry paired with the
@@ -31386,13 +31473,13 @@ const BnTableRenderer = class {
31386
31473
  }
31387
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;
31388
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) => {
31389
- var _a, _b, _c, _d;
31476
+ var _a, _b, _c, _d, _e;
31390
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 !== "-"
31391
31478
  ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
31392
31479
  : {})), cell.prefix != null ? (index.h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
31393
31480
  cell.columnStats &&
31394
31481
  cell.value !== withStyled.NOT_AVAILABLE &&
31395
- (cell.waterfall !== undefined || row.type !== TableRowType.SUM) ? (cell.waterfall !== undefined ? (index.h(WaterfallBar, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: false, isPositiveGood: cell.direction === "pos", measureScale: cell.measureScale, locale: locale, columnWidthEm: this.barColumnMaxWidth, styles: this.barChartStyles, waterfall: cell.waterfall })) : (index.h(CellBarChart, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: cell.key.startsWith("dr"), isPositiveGood: cell.direction === "pos", measureScale: barLabelMeasureScale(cell), locale: locale, percentDigits: (_b = (_a = this.barColumnLayouts.get(cell.key)) === null || _a === void 0 ? void 0 : _a.percentDigits) !== null && _b !== void 0 ? _b : this.componentDataFormatDigits.percent, percentSign: (_c = this.barColumnLayouts.get(cell.key)) === null || _c === void 0 ? void 0 : _c.percentSign, columnWidthEm: this.barColumnMaxWidth, labelWidths: (_d = this.barColumnLayouts.get(cell.key)) === null || _d === void 0 ? void 0 : _d.labels, styles: this.barChartStyles }))) : (cell.draw(this.componentDataFormatDigits, locale))));
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))));
31396
31483
  })))))));
31397
31484
  };
31398
31485
  return index.h(index.Host, { class: { "no-data": ((_a = this.table) === null || _a === void 0 ? void 0 : _a.hasNoData()) === true } }, helper());
@@ -31493,7 +31580,7 @@ const BnTableRenderer = class {
31493
31580
  });
31494
31581
  }
31495
31582
  return this.componentStyle(($) => {
31496
- 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;
31497
31584
  const varianceStyles = (_a = $["bn-chart-time"]) === null || _a === void 0 ? void 0 : _a["varianceStyles"];
31498
31585
  const barStyles = (_b = $["bn-chart-time"]) === null || _b === void 0 ? void 0 : _b["barStyles"];
31499
31586
  // Default variance styles with fillpattern
@@ -31547,13 +31634,17 @@ const BnTableRenderer = class {
31547
31634
  fontColor: ((_z = $["global"]) === null || _z === void 0 ? void 0 : _z["fontColor"]) || "#000000",
31548
31635
  fontSize: (((_0 = $["global"]) === null || _0 === void 0 ? void 0 : _0["fontSizePx"]) || 13.3333) * BAR_LABEL_FONT_RATIO * this.scaleFactor,
31549
31636
  emPx: (((_1 = $["global"]) === null || _1 === void 0 ? void 0 : _1["fontSizePx"]) || 13.3333) * this.scaleFactor,
31550
- fontFamily: ((_2 = $["global"]) === null || _2 === void 0 ? void 0 : _2["fontFamily"]) || "'Noto Sans', sans-serif",
31637
+ fontFamily: this.labelFontFamily(),
31551
31638
  unitScaling: resolvedUnit,
31552
31639
  percentageScaling: resolvedPercent,
31553
31640
  scaleFactor: this.scaleFactor,
31554
31641
  };
31555
31642
  });
31556
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
+ }
31557
31648
  /** The table's true em in px - the unit `.category-wN` and the bar columns share. */
31558
31649
  emPx() {
31559
31650
  return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontSizePx"]) || 13.3333; }) * this.scaleFactor;
@@ -31580,15 +31671,18 @@ const BnTableRenderer = class {
31580
31671
  * its label exactly fill the column. One scaling for all columns, the smallest that fits.
31581
31672
  */
31582
31673
  autoPercentageScaling() {
31674
+ var _a, _b;
31583
31675
  const emPx = this.emPx();
31584
31676
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31585
31677
  let scaling = Infinity;
31586
- for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
31678
+ for (const { isRelative, absMax, labels, sideMax } of this.barColumnLayouts.values()) {
31587
31679
  if (!isRelative || absMax <= 0)
31588
31680
  continue;
31589
31681
  const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31590
- const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
31591
- scaling = Math.min(scaling, maxPinLength / (absMax * fs));
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));
31592
31686
  }
31593
31687
  return Number.isFinite(scaling) && fs > 0 ? scaling : 0.5;
31594
31688
  }
@@ -31619,6 +31713,7 @@ const BnTableRenderer = class {
31619
31713
  const fs = emPx * BAR_LABEL_FONT_RATIO;
31620
31714
  const headSize = PIN_HEAD_SIZE * this.scaleFactor;
31621
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();
31622
31717
  for (const [key, cells] of cellsByKey) {
31623
31718
  const isRelative = key.startsWith("dr");
31624
31719
  const values = cells.map((cell) => cell.value);
@@ -31633,7 +31728,7 @@ const BnTableRenderer = class {
31633
31728
  const value = cell.value;
31634
31729
  const side = value > 0 || (value === 0 && zeroPositive) ? "pos" : "neg";
31635
31730
  const text = formatValue(value, isRelative, barLabelMeasureScale(cell), locale, percentDigits, percentSign);
31636
- labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, estimateLabelWidth(text, fs));
31731
+ labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, labelWidth(text, fs, fontFamily, isRelative));
31637
31732
  }
31638
31733
  return labels;
31639
31734
  };
@@ -31651,7 +31746,11 @@ const BnTableRenderer = class {
31651
31746
  labels = measure(percentDigits, percentSign);
31652
31747
  }
31653
31748
  const absMax = Math.max(...values.map(Math.abs)) * (isRelative ? 100 : 1);
31654
- layouts.set(key, { isRelative, absMax, labels, percentDigits, percentSign });
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 });
31655
31754
  }
31656
31755
  return layouts;
31657
31756
  }
@@ -1,4 +1,4 @@
1
- import { h, Host } from "@stencil/core";
1
+ import { forceUpdate, h, Host } from "@stencil/core";
2
2
  import { OrderDirection, TableRowType, TableType } from "../utils/model";
3
3
  import { dataPointAttributes, measure, measureContent, measureSign, readLayoutPoint, } from "../../../utils/layoutState";
4
4
  import { Table } from "../utils/Table";
@@ -15,7 +15,7 @@ import withComponentStatus from "../../../utils/withComponentStatus";
15
15
  import withComponentStyle from "../../../utils/withComponentStyle";
16
16
  import { getStores } from "../../../context/context-protocol";
17
17
  import { sourceExpr } from "../../../context/scopedDb";
18
- import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, estimateLabelWidth, PIN_HEAD_SIZE, pinLabelsFit, relativePinGeometry, } from "../utils/helper/barColumnGeometry";
18
+ import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, labelWidth, PIN_HEAD_SIZE, pinLabelsFit, relativePinGeometry, } from "../utils/helper/barColumnGeometry";
19
19
  import { NOT_AVAILABLE } from "../../../utils/relativeVariance";
20
20
  import { normalizeFillPattern } from "../../../utils/model/styles/SVGStyle";
21
21
  /**
@@ -109,9 +109,22 @@ export class BnTableRenderer {
109
109
  this.scaleFactor = 1;
110
110
  this.componentDataFormatDigits = new DataFormatDigits();
111
111
  this.barColumnLayouts = new Map();
112
+ // labels measured in the fallback font are too narrow or too wide once the web font is there: lay out again
113
+ this.relayout = () => {
114
+ if (this.barColumnLayouts.size > 0)
115
+ forceUpdate(this);
116
+ };
112
117
  withComponentStatus(this);
113
118
  this.componentStyle = withComponentStyle(this);
114
119
  }
120
+ connectedCallback() {
121
+ var _a;
122
+ (_a = document.fonts) === null || _a === void 0 ? void 0 : _a.addEventListener("loadingdone", this.relayout);
123
+ }
124
+ disconnectedCallback() {
125
+ var _a;
126
+ (_a = document.fonts) === null || _a === void 0 ? void 0 : _a.removeEventListener("loadingdone", this.relayout);
127
+ }
115
128
  /**
116
129
  * Collects the table's layout-state contribution: header/body regions,
117
130
  * counts, and - with `detail: "full"` - per-cell geometry paired with the
@@ -230,13 +243,13 @@ export class BnTableRenderer {
230
243
  }
231
244
  const locale = (_d = (_c = (_b = this.stores) === null || _b === void 0 ? void 0 : _b.context) === null || _c === void 0 ? void 0 : _c.state) === null || _d === void 0 ? void 0 : _d.locale;
232
245
  return (h("table", null, h("thead", null, this.table.getHeader().map((row) => (h("tr", { class: row.cssStyle }, row.getCells().map((cell) => (h("th", { colSpan: cell.colSpan, class: cell.cssStyle }, cell.getAsHeaderList().map((line, i, arr) => (h("span", null, i === arr.length - 1 ? (h("span", { class: line === " " ? "table-footer" : "" }, line, cell.drawPrefix(), " ")) : (h("span", null, line, cell.drawPrefix(), h("br", null))))))))))))), h("tbody", null, this.table.getBody().map((row) => (h("tr", Object.assign({ class: row.cssStyle }, dataPointAttributes(row.key, "element")), row === null || row === void 0 ? void 0 : row.getCells().map((cell) => {
233
- var _a, _b, _c, _d;
246
+ var _a, _b, _c, _d, _e;
234
247
  return (h("td", Object.assign({ colSpan: cell.colSpan, class: cell.type ? `${cell.cssStyle} ${cell.type}` : cell.cssStyle }, (row.key && cell.value != null && cell.value !== "" && cell.value !== "-"
235
248
  ? dataPointAttributes(Object.assign(Object.assign({}, row.key), { date: cell.date, scenario: cell.key }), "element")
236
249
  : {})), cell.prefix != null ? (h("span", { class: cell.cssStyle + " " + cell.prefixStyle }, cell.drawPrefix())) : (""), cell.isBarChart &&
237
250
  cell.columnStats &&
238
251
  cell.value !== NOT_AVAILABLE &&
239
- (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))));
252
+ (cell.waterfall !== undefined || row.type !== TableRowType.SUM) ? (cell.waterfall !== undefined ? (h(WaterfallBar, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: false, isPositiveGood: cell.direction === "pos", measureScale: cell.measureScale, locale: locale, columnWidthEm: this.barColumnMaxWidth, styles: this.barChartStyles, waterfall: cell.waterfall })) : (h(CellBarChart, { value: cell.value, scenario: cell.key, columnStats: cell.columnStats, height: 18 * this.scaleFactor, isRelative: cell.key.startsWith("dr"), isPositiveGood: cell.direction === "pos", measureScale: barLabelMeasureScale(cell), locale: locale, percentDigits: (_b = (_a = this.barColumnLayouts.get(cell.key)) === null || _a === void 0 ? void 0 : _a.percentDigits) !== null && _b !== void 0 ? _b : this.componentDataFormatDigits.percent, percentSign: (_c = this.barColumnLayouts.get(cell.key)) === null || _c === void 0 ? void 0 : _c.percentSign, columnWidthEm: this.barColumnMaxWidth, labelWidths: (_d = this.barColumnLayouts.get(cell.key)) === null || _d === void 0 ? void 0 : _d.labels, sideMax: (_e = this.barColumnLayouts.get(cell.key)) === null || _e === void 0 ? void 0 : _e.sideMax, styles: this.barChartStyles }))) : (cell.draw(this.componentDataFormatDigits, locale))));
240
253
  })))))));
241
254
  };
242
255
  return h(Host, { class: { "no-data": ((_a = this.table) === null || _a === void 0 ? void 0 : _a.hasNoData()) === true } }, helper());
@@ -337,7 +350,7 @@ export class BnTableRenderer {
337
350
  });
338
351
  }
339
352
  return this.componentStyle(($) => {
340
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
353
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
341
354
  const varianceStyles = (_a = $["bn-chart-time"]) === null || _a === void 0 ? void 0 : _a["varianceStyles"];
342
355
  const barStyles = (_b = $["bn-chart-time"]) === null || _b === void 0 ? void 0 : _b["barStyles"];
343
356
  // Default variance styles with fillpattern
@@ -391,13 +404,17 @@ export class BnTableRenderer {
391
404
  fontColor: ((_z = $["global"]) === null || _z === void 0 ? void 0 : _z["fontColor"]) || "#000000",
392
405
  fontSize: (((_0 = $["global"]) === null || _0 === void 0 ? void 0 : _0["fontSizePx"]) || 13.3333) * BAR_LABEL_FONT_RATIO * this.scaleFactor,
393
406
  emPx: (((_1 = $["global"]) === null || _1 === void 0 ? void 0 : _1["fontSizePx"]) || 13.3333) * this.scaleFactor,
394
- fontFamily: ((_2 = $["global"]) === null || _2 === void 0 ? void 0 : _2["fontFamily"]) || "'Noto Sans', sans-serif",
407
+ fontFamily: this.labelFontFamily(),
395
408
  unitScaling: resolvedUnit,
396
409
  percentageScaling: resolvedPercent,
397
410
  scaleFactor: this.scaleFactor,
398
411
  };
399
412
  });
400
413
  }
414
+ /** Font family of the bar and pin labels, also used to measure them. */
415
+ labelFontFamily() {
416
+ return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontFamily"]) || "'Noto Sans', sans-serif"; });
417
+ }
401
418
  /** The table's true em in px - the unit `.category-wN` and the bar columns share. */
402
419
  emPx() {
403
420
  return this.componentStyle(($) => { var _a; return ((_a = $["global"]) === null || _a === void 0 ? void 0 : _a["fontSizePx"]) || 13.3333; }) * this.scaleFactor;
@@ -424,15 +441,18 @@ export class BnTableRenderer {
424
441
  * its label exactly fill the column. One scaling for all columns, the smallest that fits.
425
442
  */
426
443
  autoPercentageScaling() {
444
+ var _a, _b;
427
445
  const emPx = this.emPx();
428
446
  const fs = emPx * BAR_LABEL_FONT_RATIO;
429
447
  let scaling = Infinity;
430
- for (const { isRelative, absMax, labels } of this.barColumnLayouts.values()) {
448
+ for (const { isRelative, absMax, labels, sideMax } of this.barColumnLayouts.values()) {
431
449
  if (!isRelative || absMax <= 0)
432
450
  continue;
433
451
  const headSize = PIN_HEAD_SIZE * this.scaleFactor;
434
- const { maxPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
435
- scaling = Math.min(scaling, maxPinLength / (absMax * fs));
452
+ // the longest pins of both sides share the room, each side takes what its pins need
453
+ const { negPinLength, posPinLength } = relativePinGeometry(this.barColumnMaxWidth, emPx, labels, headSize);
454
+ const percent = (((_a = sideMax.neg) !== null && _a !== void 0 ? _a : 0) + ((_b = sideMax.pos) !== null && _b !== void 0 ? _b : 0)) * 100;
455
+ scaling = Math.min(scaling, (negPinLength + posPinLength) / (percent * fs));
436
456
  }
437
457
  return Number.isFinite(scaling) && fs > 0 ? scaling : 0.5;
438
458
  }
@@ -463,6 +483,7 @@ export class BnTableRenderer {
463
483
  const fs = emPx * BAR_LABEL_FONT_RATIO;
464
484
  const headSize = PIN_HEAD_SIZE * this.scaleFactor;
465
485
  const locale = (_d = (_c = (_b = this.stores) === null || _b === void 0 ? void 0 : _b.context) === null || _c === void 0 ? void 0 : _c.state) === null || _d === void 0 ? void 0 : _d.locale;
486
+ const fontFamily = this.labelFontFamily();
466
487
  for (const [key, cells] of cellsByKey) {
467
488
  const isRelative = key.startsWith("dr");
468
489
  const values = cells.map((cell) => cell.value);
@@ -477,7 +498,7 @@ export class BnTableRenderer {
477
498
  const value = cell.value;
478
499
  const side = value > 0 || (value === 0 && zeroPositive) ? "pos" : "neg";
479
500
  const text = formatValue(value, isRelative, barLabelMeasureScale(cell), locale, percentDigits, percentSign);
480
- labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, estimateLabelWidth(text, fs));
501
+ labels[side] = Math.max((_a = labels[side]) !== null && _a !== void 0 ? _a : 0, labelWidth(text, fs, fontFamily, isRelative));
481
502
  }
482
503
  return labels;
483
504
  };
@@ -495,7 +516,11 @@ export class BnTableRenderer {
495
516
  labels = measure(percentDigits, percentSign);
496
517
  }
497
518
  const absMax = Math.max(...values.map(Math.abs)) * (isRelative ? 100 : 1);
498
- layouts.set(key, { isRelative, absMax, labels, percentDigits, percentSign });
519
+ const sideMax = {
520
+ neg: labels.neg === undefined ? undefined : Math.max(0, ...values.map((value) => -value)),
521
+ pos: labels.pos === undefined ? undefined : Math.max(0, ...values),
522
+ };
523
+ layouts.set(key, { isRelative, absMax, labels, sideMax, percentDigits, percentSign });
499
524
  }
500
525
  return layouts;
501
526
  }
@@ -1,7 +1,7 @@
1
1
  import { h } from "@stencil/core";
2
2
  import { Formatter } from "../../../../utils";
3
3
  import { VarianceScenario } from "../../../../utils/model/VarianceScenario";
4
- import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, estimateLabelWidth, PIN_HEAD_SIZE, relativePinGeometry, VARIANCE_AXIS_EM, } from "../helper/barColumnGeometry";
4
+ import { absoluteBarGeometry, BAR_LABEL_FONT_RATIO, labelWidth, PIN_HEAD_SIZE, relativePinGeometry, VARIANCE_AXIS_EM, } from "../helper/barColumnGeometry";
5
5
  /** Matches SYSTEM_STYLE_DEFAULTS.global.fontSizePx; only used when `styles` is omitted. */
6
6
  const DEFAULT_EM_PX = 13.3333;
7
7
  // Default fallback colors (should come from componentStyle)
@@ -259,7 +259,7 @@ const AbsoluteVarianceBar = (props) => {
259
259
  const s = styles.scaleFactor;
260
260
  const fs = styles.fontSize;
261
261
  const displayValue = formattedValue || formatValue(value, false, measureScale, locale);
262
- const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
262
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, false));
263
263
  const { totalWidth, axisX, breakGap, overdriveZoneWidth, cutWidth, labelGap } = absoluteBarGeometry(columnWidthEm, styles.emPx, sides);
264
264
  // Direct unit scaling via fontSize
265
265
  const scaledWidth = Math.abs(value) * styles.unitScaling * fs;
@@ -317,21 +317,23 @@ const AbsoluteVarianceBar = (props) => {
317
317
  */
318
318
  const RelativeVariancePin = (props) => {
319
319
  var _a, _b;
320
- const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, styles = DEFAULT_STYLES, } = props;
320
+ const { value, scenario, columnStats, height, isPositiveGood, formattedValue, measureScale, locale, percentDigits, percentSign, columnWidthEm = 10, labelWidths, sideMax, styles = DEFAULT_STYLES, } = props;
321
321
  const s = styles.scaleFactor;
322
322
  const fs = styles.fontSize;
323
323
  const headSize = PIN_HEAD_SIZE * s;
324
+ const pinPx = 100 * styles.percentageScaling * fs;
324
325
  const displayValue = formattedValue || formatValue(value, true, measureScale, locale, percentDigits, percentSign);
325
- const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, estimateLabelWidth(displayValue, fs));
326
- const { totalWidth, maxPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize);
326
+ const sides = labelWidths !== null && labelWidths !== void 0 ? labelWidths : ownLabelWidths(columnStats, labelWidth(displayValue, fs, styles.fontFamily, true));
327
+ const { neg = Math.max(0, -columnStats.min), pos = Math.max(0, columnStats.max) } = sideMax !== null && sideMax !== void 0 ? sideMax : {};
328
+ const { totalWidth, negPinLength, posPinLength, axisX, labelGap } = relativePinGeometry(columnWidthEm, styles.emPx, sides, headSize, { neg: neg * pinPx, pos: pos * pinPx });
327
329
  const centerY = height / 2;
328
330
  // Direct unit scaling via fontSize
329
- const percentValue = Math.abs(value) * 100;
330
- const scaledLength = percentValue * styles.percentageScaling * fs;
331
+ const scaledLength = Math.abs(value) * pinPx;
332
+ const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
333
+ const maxPinLength = isPositive ? posPinLength : negPinLength;
331
334
  // auto scaling puts the longest pin on the limit, float rounding must not cut it
332
335
  const isExtreme = scaledLength - maxPinLength > 1e-6;
333
336
  const pinLength = Math.min(scaledLength, maxPinLength);
334
- const isPositive = value > 0 || (value === 0 && zeroOnPositiveSide(sides));
335
337
  const direction = isPositive ? 1 : -1;
336
338
  const pinEndX = axisX + direction * pinLength;
337
339
  // Get style based on column type (scenario vs variance)
@@ -64,6 +64,43 @@ export function estimateLabelWidth(text, fontSize) {
64
64
  }
65
65
  return em * fontSize * LABEL_WIDTH_MARGIN;
66
66
  }
67
+ /** Safety pad on a measured label, in label font size: covers subpixel rounding only. */
68
+ const MEASURE_PAD = 0.15;
69
+ const FONT_SENTINEL = "1px __bn_font_sentinel__";
70
+ let measureContext;
71
+ /** One shared 2D context; null where the document has no canvas (spec tests, mock-doc). */
72
+ function labelMeasureContext() {
73
+ var _a, _b, _c;
74
+ if (measureContext === undefined) {
75
+ try {
76
+ measureContext =
77
+ 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);
78
+ }
79
+ catch (_d) {
80
+ measureContext = null;
81
+ }
82
+ }
83
+ return measureContext;
84
+ }
85
+ /**
86
+ * Rendered width of a value label in px, measured with the font the SVG text uses. Falls
87
+ * back to estimateLabelWidth where no canvas can measure.
88
+ */
89
+ export function labelWidth(text, fontSize, fontFamily, italic) {
90
+ const ctx = labelMeasureContext();
91
+ if (ctx) {
92
+ // the canvas ignores a font it cannot parse and keeps the last one: the sentinel shows that
93
+ ctx.font = FONT_SENTINEL;
94
+ const sentinel = ctx.font;
95
+ ctx.font = `${italic ? "italic " : ""}${fontSize}px ${fontFamily}`;
96
+ if (ctx.font === sentinel)
97
+ return estimateLabelWidth(text, fontSize);
98
+ const width = ctx.measureText(text).width;
99
+ if (Number.isFinite(width) && (width > 0 || text === ""))
100
+ return width + MEASURE_PAD * fontSize;
101
+ }
102
+ return estimateLabelWidth(text, fontSize);
103
+ }
67
104
  /** The SVG fills the cell's content box: the requested column width minus the cell padding. */
68
105
  function contentWidth(widthEm, emPx) {
69
106
  return Math.max(0, (widthEm - BAR_CELL_PADDING_EM) * emPx);
@@ -114,11 +151,46 @@ export function absoluteBarGeometry(widthEm, emPx, labels) {
114
151
  labelGap,
115
152
  };
116
153
  }
117
- export function relativePinGeometry(widthEm, emPx, labels, headSize) {
154
+ /**
155
+ * `reach` is the length in px the longest pin of each side wants at the column's scale.
156
+ * With it, the pin room is split by what each side needs, so a side with shorter pins
157
+ * gives its room to the other; room left after both is shared equally. Without it, both
158
+ * sides get the same extent.
159
+ */
160
+ export function relativePinGeometry(widthEm, emPx, labels, headSize, reach) {
161
+ var _a, _b;
118
162
  const totalWidth = contentWidth(widthEm, emPx);
119
163
  const labelGap = LABEL_GAP_EM * emPx;
120
164
  const { extent, axisX } = splitSides(totalWidth, labels, headSize / 2, labelGap);
121
- return { totalWidth, maxPinLength: extent, axisX, labelGap };
165
+ const hasNeg = labels.neg !== undefined;
166
+ const hasPos = labels.pos !== undefined || !hasNeg;
167
+ let neg = hasNeg ? extent : 0;
168
+ let pos = hasPos ? extent : 0;
169
+ if (reach && hasNeg && hasPos) {
170
+ const room = 2 * extent;
171
+ const wantNeg = Math.max(0, (_a = reach.neg) !== null && _a !== void 0 ? _a : 0);
172
+ const wantPos = Math.max(0, (_b = reach.pos) !== null && _b !== void 0 ? _b : 0);
173
+ if (wantNeg + wantPos <= room + 1e-6) {
174
+ neg = wantNeg + (room - wantNeg - wantPos) / 2;
175
+ pos = room - neg;
176
+ }
177
+ else if (wantNeg < extent) {
178
+ neg = wantNeg;
179
+ pos = room - neg;
180
+ }
181
+ else if (wantPos < extent) {
182
+ pos = wantPos;
183
+ neg = room - pos;
184
+ }
185
+ }
186
+ return {
187
+ totalWidth,
188
+ maxPinLength: Math.max(neg, pos),
189
+ negPinLength: neg,
190
+ posPinLength: pos,
191
+ axisX: hasNeg ? axisX - extent + neg : 0,
192
+ labelGap,
193
+ };
122
194
  }
123
195
  export function waterfallGeometry(widthEm, emPx) {
124
196
  const totalWidth = contentWidth(widthEm, emPx);
@@ -1 +1 @@
1
- import{B as o,d as s}from"./p-CLilvhjH.js";const p=o,r=s;export{p as BnTableRenderer,r as defineCustomElement}
1
+ import{B as o,d as s}from"./p-CHQ67u2G.js";const p=o,r=s;export{p as BnTableRenderer,r as defineCustomElement}