@bino-bi/bn-template-engine 1.0.0-next.49 → 1.0.0-next.51

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.
Files changed (107) hide show
  1. package/dist/bn-template-engine/bn-template-engine.esm.js +1 -1
  2. package/dist/bn-template-engine/p-0cad5598.entry.js +1 -0
  3. package/dist/bn-template-engine/{p-2de524d0.entry.js → p-150bdb68.entry.js} +1 -1
  4. package/dist/bn-template-engine/{p-Bxa4pX1M.js → p-Bvunba5p.js} +1 -1
  5. package/dist/bn-template-engine/p-NxJ6P79y.js +1 -0
  6. package/dist/bn-template-engine/{p-8fc90dc8.entry.js → p-b75a4b52.entry.js} +1 -1
  7. package/dist/bn-template-engine/{p-510d2ee6.entry.js → p-fdf42a77.entry.js} +1 -1
  8. package/dist/cjs/bn-annotation-layer_30.cjs.entry.js +1045 -63
  9. package/dist/cjs/bn-grid.cjs.entry.js +1 -1
  10. package/dist/cjs/bn-image.cjs.entry.js +1 -1
  11. package/dist/cjs/bn-template-engine.cjs.js +1 -1
  12. package/dist/cjs/bn-text.cjs.entry.js +2 -2
  13. package/dist/cjs/loader.cjs.js +1 -1
  14. package/dist/cjs/{withStyled-pkdctrK9.js → withStyled-BISQLGLe.js} +9 -0
  15. package/dist/cjs/{withTranslation-BIzTNYZM.js → withTranslation-B7T30EVU.js} +1 -1
  16. package/dist/collection/components/bn-annotation-layer/bn-annotation-layer.js +109 -11
  17. package/dist/collection/components/bn-annotation-layer/utils/marks.js +126 -4
  18. package/dist/collection/components/bn-annotation-layer/utils/matchPoint.js +2 -1
  19. package/dist/collection/components/bn-chart-structure/bn-chart-structure-renderer/bn-chart-structure-renderer.js +133 -0
  20. package/dist/collection/components/bn-chart-structure/bn-chart-structure.js +258 -2
  21. package/dist/collection/components/bn-chart-structure/utils/StructureChart.js +507 -27
  22. package/dist/collection/components/bn-chart-structure/utils/elements/Bar.js +3 -1
  23. package/dist/collection/components/bn-chart-structure/utils/elements/Label.js +12 -0
  24. package/dist/collection/components/bn-chart-structure/utils/elements/StackedBar.js +23 -8
  25. package/dist/collection/components/bn-chart-structure/utils/helper/ElementPositions.js +19 -3
  26. package/dist/collection/components/bn-chart-structure/utils/model/StructureChartComponentConfig.js +23 -0
  27. package/dist/collection/components/bn-context/bn-context.js +56 -1
  28. package/dist/collection/components/bn-layout-card/bn-layout-card.js +62 -2
  29. package/dist/collection/utils/formatter.js +13 -2
  30. package/dist/collection/utils/validate.js +9 -0
  31. package/dist/components/bn-annotation-layer.js +1 -1
  32. package/dist/components/bn-chart-bubble-renderer.js +1 -1
  33. package/dist/components/bn-chart-bubble.js +1 -1
  34. package/dist/components/bn-chart-bullet-renderer.js +1 -1
  35. package/dist/components/bn-chart-bullet.js +1 -1
  36. package/dist/components/bn-chart-scatter-renderer.js +1 -1
  37. package/dist/components/bn-chart-scatter.js +1 -1
  38. package/dist/components/bn-chart-structure-renderer.js +1 -1
  39. package/dist/components/bn-chart-structure.js +1 -1
  40. package/dist/components/bn-chart-time-renderer.js +1 -1
  41. package/dist/components/bn-chart-time.js +1 -1
  42. package/dist/components/bn-context.js +1 -1
  43. package/dist/components/bn-grid.js +1 -1
  44. package/dist/components/bn-image.js +1 -1
  45. package/dist/components/bn-layout-card.js +1 -1
  46. package/dist/components/bn-layout-page.js +1 -1
  47. package/dist/components/bn-page.js +1 -1
  48. package/dist/components/bn-table-renderer.js +1 -1
  49. package/dist/components/bn-table.js +1 -1
  50. package/dist/components/bn-text.js +1 -1
  51. package/dist/components/bn-title-renderer.js +1 -1
  52. package/dist/components/bn-title.js +1 -1
  53. package/dist/components/bn-tree.js +1 -1
  54. package/dist/components/p-BOwmXBm-.js +1 -0
  55. package/dist/components/p-Bs5V6Ibu.js +1 -0
  56. package/dist/components/{p-CVfmhtEW.js → p-CU9CdEBF.js} +1 -1
  57. package/dist/components/{p-Cc0gaLwp.js → p-CZSp0rSS.js} +1 -1
  58. package/dist/components/p-CcfvGlBq.js +1 -0
  59. package/dist/components/p-CiljAoC5.js +1 -0
  60. package/dist/components/p-D0v9GMIf.js +1 -0
  61. package/dist/components/{p-DBj1Yd3M.js → p-D65TnLS9.js} +1 -1
  62. package/dist/components/p-DKcw-1ot.js +1 -0
  63. package/dist/components/{p-k7heRDPP.js → p-DLmQ_mi7.js} +1 -1
  64. package/dist/components/p-DOKqRsAu.js +1 -0
  65. package/dist/components/p-Dpc8_R-Y.js +1 -0
  66. package/dist/components/p-LHIKI9Dp.js +1 -0
  67. package/dist/components/{p-Blre9f28.js → p-NdDdfez6.js} +1 -1
  68. package/dist/components/p-Zz3fAPcH.js +1 -0
  69. package/dist/components/{p-y12CmIFS.js → p-dSUX43yf.js} +1 -1
  70. package/dist/components/p-jEqo8YD5.js +1 -0
  71. package/dist/esm/bn-annotation-layer_30.entry.js +1045 -63
  72. package/dist/esm/bn-grid.entry.js +1 -1
  73. package/dist/esm/bn-image.entry.js +1 -1
  74. package/dist/esm/bn-template-engine.js +1 -1
  75. package/dist/esm/bn-text.entry.js +2 -2
  76. package/dist/esm/loader.js +1 -1
  77. package/dist/esm/{withStyled-B62R20Zx.js → withStyled-CrTxIZ0Q.js} +9 -0
  78. package/dist/esm/{withTranslation-BEseMLQ6.js → withTranslation-BtSJmx1J.js} +1 -1
  79. package/dist/schemas/stack.schema.json +42 -0
  80. package/dist/types/components/bn-annotation-layer/utils/marks.d.ts +53 -0
  81. package/dist/types/components/bn-annotation-layer/utils/matchPoint.d.ts +2 -0
  82. package/dist/types/components/bn-chart-structure/bn-chart-structure-renderer/bn-chart-structure-renderer.d.ts +11 -1
  83. package/dist/types/components/bn-chart-structure/bn-chart-structure.d.ts +49 -0
  84. package/dist/types/components/bn-chart-structure/utils/StructureChart.d.ts +45 -0
  85. package/dist/types/components/bn-chart-structure/utils/elements/Label.d.ts +4 -0
  86. package/dist/types/components/bn-chart-structure/utils/elements/StackedBar.d.ts +13 -1
  87. package/dist/types/components/bn-chart-structure/utils/helper/ElementPositions.d.ts +13 -1
  88. package/dist/types/components/bn-chart-structure/utils/model/StructureChartComponentConfig.d.ts +23 -0
  89. package/dist/types/components/bn-context/bn-context.d.ts +11 -0
  90. package/dist/types/components/bn-layout-card/bn-layout-card.d.ts +12 -0
  91. package/dist/types/components.d.ts +113 -2
  92. package/dist/types/schemas.d.ts +12 -0
  93. package/dist/types/utils/annotations.d.ts +1 -1
  94. package/dist/types/utils/formatter.d.ts +5 -0
  95. package/package.json +1 -1
  96. package/dist/bn-template-engine/p-BFl7OwcC.js +0 -1
  97. package/dist/bn-template-engine/p-c1d679af.entry.js +0 -1
  98. package/dist/components/p-B11cXOLy.js +0 -1
  99. package/dist/components/p-BVNOmHh4.js +0 -1
  100. package/dist/components/p-CHhxQoF9.js +0 -1
  101. package/dist/components/p-CO6bgquz.js +0 -1
  102. package/dist/components/p-CmvpJzLd.js +0 -1
  103. package/dist/components/p-CuUfiEu0.js +0 -1
  104. package/dist/components/p-DHloMyxV.js +0 -1
  105. package/dist/components/p-Dd1QOWDV.js +0 -1
  106. package/dist/components/p-HVN8BNWN.js +0 -1
  107. package/dist/components/p-x0W2_FAX.js +0 -1
@@ -18,6 +18,9 @@ import { SiPrefix } from "../../../utils/model/SiPrefix";
18
18
  import { sourceExpr } from "../../../context/scopedDb";
19
19
  import { errorText } from "../../../utils/queryError";
20
20
  import { levelKey } from "../../../utils/layoutState/dataPoint";
21
+ import { SYSTEM_STYLE_DEFAULTS } from "../../../stores/componentStyles";
22
+ /** The id of the row that sums the rows a limit cut off. */
23
+ const REST_ROW_ID = 9999;
21
24
  /**
22
25
  * Orchestrates the full rendering pipeline for an IBCS structure (categorical) chart.
23
26
  *
@@ -74,6 +77,11 @@ export class StructureChart {
74
77
  this.stackLabelClearance = { left: 0, right: 0 };
75
78
  this.fromExpr = "";
76
79
  this.rowCount = 0;
80
+ this.sums = "none";
81
+ this.sumTitle = "";
82
+ this.scenarioLabelTop = false;
83
+ this.referenceLine = null;
84
+ this.italicCategories = [];
77
85
  this.db = db;
78
86
  this.tableName = tableName;
79
87
  this.ruleSet = ruleSet || new IBCSRuleSet();
@@ -92,7 +100,7 @@ export class StructureChart {
92
100
  if (this.titleLength > leftSideWidth) {
93
101
  leftSideWidth = this.titleLength;
94
102
  }
95
- return new ElementPositions(new Padding(this.styles.padding), this.styles.scaleFactors, this.styles.fontStyles.size, this.dataModel.length, this.leftSideOffsetForHorizontalSync, this.titleLength, leftSideWidth, categoryWidth, !!this.stackConfig && this.variancesDataModel.length === 0);
103
+ return new ElementPositions(new Padding(this.styles.padding), this.styles.scaleFactors, this.styles.fontStyles.size, this.dataModel.length, this.leftSideOffsetForHorizontalSync, this.titleLength, leftSideWidth, categoryWidth, (!!this.stackConfig && this.variancesDataModel.length === 0) || this.scenarioLabelTop, this.rowLayout());
96
104
  }
97
105
  setStyles(newStyle) {
98
106
  this.styles = newStyle;
@@ -111,7 +119,17 @@ export class StructureChart {
111
119
  this.pixelPerPercentage = config.pixelPerPercentage || 100;
112
120
  this.setSource(config.filter);
113
121
  this.level = config.level;
122
+ this.sums = config.sums;
123
+ this.sumTitle = config.sumTitle;
124
+ this.scenarioLabelTop = config.scenarioLabelTop;
125
+ this.referenceLine = config.referenceLine;
126
+ this.italicCategories = config.italicCategories;
114
127
  this.dataModel = this.populateModel(scenarios, config.level, config.sortOrder);
128
+ // Populate per-dimension stack segments when stacking by a data dimension
129
+ if (config.stackConfig && config.stackConfig.by !== StackBy.SCENARIOS) {
130
+ this.populateDimensionStackSegments(scenarios, config.stackConfig, config.level);
131
+ }
132
+ this.addSumRows(scenarios);
115
133
  for (const row of this.dataModel) {
116
134
  for (const dataPoint of [row.scenario1, row.scenario2, row.scenario3, row.scenario4]) {
117
135
  // a category without a value has no bar: nothing an annotation can point at
@@ -120,18 +138,17 @@ export class StructureChart {
120
138
  }
121
139
  }
122
140
  }
123
- // Populate per-dimension stack segments when stacking by a data dimension
124
- if (config.stackConfig && config.stackConfig.by !== StackBy.SCENARIOS) {
125
- this.populateDimensionStackSegments(scenarios, config.stackConfig, config.level);
126
- }
127
141
  this.variancesDataModel = this.populateVarianceModel(config.variances, this.dataModel);
128
142
  this.configTitle = config.title;
129
143
  this.configVariances = (_b = config === null || config === void 0 ? void 0 : config.variances) !== null && _b !== void 0 ? _b : [];
130
144
  this.updateTitle();
131
- this.smallestDataPointValue = this.findSmallestDataPointValue(this.dataModel);
132
- this.smallestVarianceDataPointValue = this.findSmallestDataPointValue(this.variancesDataModel);
133
- this.highestDataPointValue = this.findHighestDataPointValue(this.dataModel);
134
- this.highestVarianceDataPointValue = this.findHighestDataPointValue(this.variancesDataModel);
145
+ // a sum row has no bar: it does not count for the scale
146
+ const bars = this.dataModel.filter((row) => !row.sum);
147
+ this.smallestDataPointValue = this.findSmallestDataPointValue(bars);
148
+ const varianceBars = this.variancesDataModel.filter((row) => !row.sum);
149
+ this.smallestVarianceDataPointValue = this.findSmallestDataPointValue(varianceBars);
150
+ this.highestDataPointValue = this.findHighestDataPointValue(bars);
151
+ this.highestVarianceDataPointValue = this.findHighestDataPointValue(varianceBars);
135
152
  this.axisScenario = this.setAxisScenario(config.variances);
136
153
  this.measureScale = config.measureScale;
137
154
  this.measureUnit = config.measureUnit;
@@ -247,7 +264,7 @@ export class StructureChart {
247
264
  * from variable width components.
248
265
  */
249
266
  getUnitBarExtent() {
250
- var _a, _b;
267
+ var _a, _b, _c, _d;
251
268
  const stacked = this.isStacked();
252
269
  const vt = this.getVarianceType();
253
270
  const isVariance = vt !== VarianceType.NONE;
@@ -257,8 +274,9 @@ export class StructureChart {
257
274
  // both sides of the axis; a relative stack is laid out on a span of 1, so its sides add up to 1 * ppu
258
275
  fixedDomain: stacked ? this.stackSides.positive + this.stackSides.negative : undefined,
259
276
  relativeVariance: vt === VarianceType.RELATIVE,
260
- maxPositive: (_a = highest === null || highest === void 0 ? void 0 : highest.value) !== null && _a !== void 0 ? _a : 0,
261
- minNegative: (_b = smallest === null || smallest === void 0 ? void 0 : smallest.value) !== null && _b !== void 0 ? _b : 0,
277
+ // a reference line right of the longest bar needs its room too
278
+ maxPositive: Math.max((_a = highest === null || highest === void 0 ? void 0 : highest.value) !== null && _a !== void 0 ? _a : 0, !stacked && !isVariance ? ((_c = (_b = this.referenceLine) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : 0) : 0),
279
+ minNegative: (_d = smallest === null || smallest === void 0 ? void 0 : smallest.value) !== null && _d !== void 0 ? _d : 0,
262
280
  });
263
281
  }
264
282
  /**
@@ -292,6 +310,8 @@ export class StructureChart {
292
310
  return (this.position.getAxisEndPositionY() +
293
311
  this.position.spacings.labelToAxis +
294
312
  this.position.fontSize +
313
+ // the 100% mark stands below the axis end
314
+ (this.hasPercentMark() ? this.position.fontSize * 0.8 : 0) +
295
315
  this.styles.padding.bottom);
296
316
  }
297
317
  patternToString() {
@@ -353,11 +373,15 @@ export class StructureChart {
353
373
  this.drawStackedScenarios(this.dataModel, svgContainer);
354
374
  this.drawStackedHeaders(this.dataModel, svgContainer);
355
375
  this.drawAxisAndLabels(this.dataModel, svgContainer, true);
376
+ this.drawPercentMark(svgContainer);
356
377
  }
357
378
  else {
358
379
  this.drawScenarios(this.dataModel, svgContainer);
359
380
  this.drawAxisAndLabels(this.dataModel, svgContainer);
360
381
  }
382
+ if (this.variancesDataModel.length === 0) {
383
+ this.drawReferenceLine(svgContainer);
384
+ }
361
385
  }
362
386
  setPattern(fc, fcPolygonPattern) {
363
387
  this.styles.pattern.set(fc, fcPolygonPattern);
@@ -474,7 +498,7 @@ export class StructureChart {
474
498
  };
475
499
  return obj;
476
500
  }, {
477
- id: 9999,
501
+ id: REST_ROW_ID,
478
502
  category: "REST",
479
503
  type: VarianceType.NONE,
480
504
  scenario1: new DataPoint(NaN, selectedScenarios[0], this.pixelPerUnit, Scenario.NEU),
@@ -661,7 +685,7 @@ export class StructureChart {
661
685
  // a 100 % stack prints no total
662
686
  const totalLabelRoom = (layout) => {
663
687
  var _a;
664
- return ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE
688
+ return ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE && this.stackConfig.labels !== "value"
665
689
  ? 0
666
690
  : (labelToBar + this.calculateTotalLabelWidth(layout.total)) / this.pixelPerUnit;
667
691
  };
@@ -669,6 +693,8 @@ export class StructureChart {
669
693
  }
670
694
  /** Layouts of the stacks of a row that are drawn, back to front. */
671
695
  drawnStackLayouts(row) {
696
+ if (this.isTextSum(row))
697
+ return [];
672
698
  return this.stackGroups(row)
673
699
  .filter((group) => group.hasValue)
674
700
  .map((group) => group.layout);
@@ -716,6 +742,7 @@ export class StructureChart {
716
742
  scenario2: new DataPoint(null, null, null, null),
717
743
  scenario3: new DataPoint(null, null, null, null),
718
744
  scenario4: new DataPoint(null, null, null, null),
745
+ sum: dataModel[index].sum,
719
746
  });
720
747
  }
721
748
  }
@@ -787,6 +814,7 @@ export class StructureChart {
787
814
  let isRelative = false;
788
815
  if (!this.hideCategories) {
789
816
  dataModel.forEach((elements, index) => {
817
+ var _a;
790
818
  if (elements["type"] === VarianceType.RELATIVE) {
791
819
  isRelative = true;
792
820
  }
@@ -808,9 +836,23 @@ export class StructureChart {
808
836
  new Label(axisLabelGroup, this.position.getCategoryPositionX(isPositive) + clearance, this.position.getBarCenterPositionY(index), "",
809
837
  // Category labels come from the data, but the synthetic "REST"
810
838
  // overflow bucket is engine-generated and translatable, as in bn-table.
811
- this.i18n.get(elements.category) || elements.category, this.styles, HookLocation.Center, textHook).draw();
839
+ this.i18n.get(elements.category) || elements.category, this.styles, HookLocation.Center, textHook)
840
+ .setEmphasis(!!((_a = this.dataModel[index]) === null || _a === void 0 ? void 0 : _a.sum), this.isItalic(elements.category))
841
+ .draw();
812
842
  });
813
843
  }
844
+ // a thin line above a sum row, from the labels to the end of the row
845
+ dataModel.forEach((_elements, index) => {
846
+ const row = this.dataModel[index];
847
+ if (!(row === null || row === void 0 ? void 0 : row.sum))
848
+ return;
849
+ const y = this.position.getBarCenterPositionY(index) - this.position.spacings.categoryWidth / 2;
850
+ const left = this.hideCategories ? this.position.getAxisPositionX() : this.styles.padding.left;
851
+ axisLabelGroup
852
+ .line(left, y, this.position.getAxisPositionX() + this.sumRowExtent(row), y)
853
+ .stroke({ color: "#000000", width: Math.max(0.5, this.position.spacings.axisLineWidth / 2) })
854
+ .addClass("sum-line");
855
+ });
814
856
  new Axis({
815
857
  scenarioType: this.axisScenario,
816
858
  labelText: this.title,
@@ -819,20 +861,60 @@ export class StructureChart {
819
861
  }
820
862
  drawScenarios(dataModel, svgContainer) {
821
863
  const barsGroup = svgContainer.group().id("Scenarios");
822
- const scenarioFactory = new ScenarioFactory(this.position, barsGroup, this.scenarioStyles(), dataModel.length, this.i18n);
864
+ // the name of the scenario stands below the last bar, or above the first one
865
+ let lastBar = -1;
866
+ dataModel.forEach((row, index) => {
867
+ if (!row.sum)
868
+ lastBar = index;
869
+ });
870
+ const scenarioFactory = new ScenarioFactory(this.position, barsGroup, this.scenarioStyles(),
871
+ // the factory names the last bar. The row layout leaves room for the name when a sum row follows.
872
+ this.scenarioLabelTop ? 0 : lastBar + 1, this.i18n);
823
873
  scenarioFactory.setOverflowConfig(this.overflowConfig);
824
874
  dataModel.forEach((elements, index) => {
875
+ if (elements.sum) {
876
+ this.drawSumValue(elements, index, barsGroup);
877
+ return;
878
+ }
825
879
  scenarioFactory.createPrimaryBar(elements["scenario1"], index).draw(this.dataFormatDigits, this.measureScale);
826
880
  scenarioFactory.createSecondaryBar(elements["scenario2"], index).draw();
827
881
  scenarioFactory.createPrimaryMarker(elements["scenario3"], index).draw();
828
882
  scenarioFactory.createSecondaryMarker(elements["scenario4"], index).draw();
829
883
  });
884
+ const first = dataModel.find((row) => !row.sum);
885
+ if (this.scenarioLabelTop && first && !first.scenario1.isEmpty() && first.scenario1.scenario != null) {
886
+ const scenario = first.scenario1.scenario;
887
+ new Label(barsGroup,
888
+ // from the axis on: the first bar can be shorter than the name
889
+ this.position.getBarPositionX() + this.position.spacings.labelToAxis, this.position.getStackedHeaderPositionY(), "SCENARIO_HEADER", this.i18n.get(scenario) || scenario, this.styles, HookLocation.Bottom, HookLocation.Left).draw();
890
+ }
891
+ }
892
+ /** The value of a sum row of a chart with plain bars or variances: a bold number at the axis, no bar. */
893
+ drawSumValue(row, index, group) {
894
+ const point = row.scenario1;
895
+ if (point.isEmpty() || !Number.isFinite(point.value))
896
+ return;
897
+ const negative = point.value < 0;
898
+ const label = new Label(group,
899
+ // like the label of a bar without length: a negative number stands left of the axis
900
+ this.position.getLabelPositionX(negative ? -0.000001 : 0), this.position.getBarCenterPositionY(index), "SUM_" + index, point.value, this.styles, HookLocation.Center, negative ? HookLocation.Right : HookLocation.Left)
901
+ .setEmphasis(true, false)
902
+ .setPoint(point.key);
903
+ if (row.type === VarianceType.RELATIVE)
904
+ label.setToRelativeVarianceLabel();
905
+ else if (row.type === VarianceType.ABSOLUTE)
906
+ label.setToVarianceLabel();
907
+ label.draw(this.dataFormatDigits, this.measureScale);
830
908
  }
831
909
  drawVarianceScenario(dataModel, svgContainer) {
832
910
  const barsGroup = svgContainer.group().id("Scenarios");
833
911
  const scenarioFactory = new ScenarioFactory(this.position, barsGroup, this.scenarioStyles(), dataModel.length, this.i18n);
834
912
  scenarioFactory.setOverflowConfig(this.overflowConfig);
835
913
  dataModel.forEach((elements, index) => {
914
+ if (elements.sum) {
915
+ this.drawSumValue(elements, index, barsGroup);
916
+ return;
917
+ }
836
918
  if (elements["type"] === VarianceType.RELATIVE) {
837
919
  scenarioFactory.createRelativeVarianceBar(elements["scenario1"], index).draw(this.dataFormatDigits);
838
920
  }
@@ -865,14 +947,27 @@ export class StructureChart {
865
947
  return;
866
948
  const stackGroup = svgContainer.group().id("StackedScenarios");
867
949
  const isScenarioStack = this.stackConfig.by === StackBy.SCENARIOS;
950
+ const relative = this.stackConfig.mode === StackMode.RELATIVE;
951
+ const valueLabels = relative && this.stackConfig.labels === "value";
952
+ const label = valueLabels ? "value" : this.stackConfig.percentMark ? "plain-share" : "share";
868
953
  dataModel.forEach((elements, index) => {
869
954
  const groups = this.stackGroups(elements);
955
+ if (this.isTextSum(elements)) {
956
+ this.drawSumText(elements, index, stackGroup);
957
+ return;
958
+ }
959
+ const options = {
960
+ label,
961
+ total: valueLabels,
962
+ bold: !!elements.sum,
963
+ italic: this.isItalic(elements.category),
964
+ };
870
965
  groups.forEach((group, groupIdx) => {
871
966
  let lastBar = null;
872
967
  group.segments.forEach((seg, segIdx) => {
873
968
  const bar = new StackedBar(this.position, this.styles, seg.dataPoint, index, group.layout.segments[segIdx], seg.scenario,
874
969
  // overlay: one colour per group, back=lighter, front=darker
875
- groups.length > 1 ? groups.length - 1 - groupIdx : segIdx, this.stackConfig.mode, group.layout, isScenarioStack, stackGroup);
970
+ groups.length > 1 ? groups.length - 1 - groupIdx : segIdx, this.stackConfig.mode, group.layout, isScenarioStack, stackGroup, options);
876
971
  bar.draw(this.dataFormatDigits, this.measureScale);
877
972
  lastBar = bar;
878
973
  });
@@ -890,9 +985,10 @@ export class StructureChart {
890
985
  * The headers take their place from the first row.
891
986
  */
892
987
  drawStackedHeaders(dataModel, svgContainer) {
988
+ var _a;
893
989
  if (!this.stackConfig || dataModel.length === 0)
894
990
  return;
895
- const firstRow = dataModel[0];
991
+ const firstRow = (_a = dataModel.find((row) => !this.isTextSum(row))) !== null && _a !== void 0 ? _a : dataModel[0];
896
992
  // Dimension stacking without segments draws one plain bar: nothing to name
897
993
  if (this.stackConfig.by !== StackBy.SCENARIOS && (!firstRow.stackSegments || firstRow.stackSegments.length === 0)) {
898
994
  return;
@@ -917,6 +1013,11 @@ export class StructureChart {
917
1013
  }
918
1014
  // Single group: one header per segment, centered on the segment
919
1015
  const [group] = groups;
1016
+ const totalDrawn = this.stackConfig.mode !== StackMode.RELATIVE || this.stackConfig.labels === "value";
1017
+ if (this.stackConfig.totalTitle && totalDrawn) {
1018
+ // above the total of the first bar, which starts behind the bar
1019
+ new Label(headerGroup, this.position.getLabelPositionX(group.layout.positive * this.pixelPerUnit), headerY, "STACK_TOTAL_HEADER", this.stackConfig.totalTitle, this.styles, HookLocation.Bottom, HookLocation.Left).draw();
1020
+ }
920
1021
  group.segments.forEach((seg, segIdx) => {
921
1022
  const { start, end } = group.layout.segments[segIdx];
922
1023
  drawHeader(axisX + ((start + end) / 2) * seg.dataPoint.pixelPerUnit, this.i18n.get(seg.scenario) || seg.scenario);
@@ -969,7 +1070,7 @@ export class StructureChart {
969
1070
  return width;
970
1071
  }
971
1072
  calculateRightSideWidth(varianceType) {
972
- var _a, _b, _c;
1073
+ var _a, _b, _c, _d, _e, _f;
973
1074
  // Stacked charts: width based on the positive side of the stacks, not individual max
974
1075
  if (this.isStacked()) {
975
1076
  const labelToBar = this.styles.fontStyles.size * this.styles.scaleFactors.labelToBar;
@@ -981,28 +1082,42 @@ export class StructureChart {
981
1082
  this.position.getCategoryToAxisSpaceing() +
982
1083
  this.calculateCategoryWidth());
983
1084
  }
984
- return Math.max(width, this.titleLength);
1085
+ if ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.totalTitle) {
1086
+ const title = CalculateSize(this.stackConfig.totalTitle, {
1087
+ font: this.styles.fontStyles.font,
1088
+ fontSize: this.styles.fontStyles.size + "px",
1089
+ }).width;
1090
+ width = Math.max(width, this.stackSides.positive * this.pixelPerUnit + labelToBar + title);
1091
+ }
1092
+ return Math.max(width, this.titleLength, (_c = (_b = this.sumColumns()) === null || _b === void 0 ? void 0 : _b.width) !== null && _c !== void 0 ? _c : 0, this.referenceLineExtent());
985
1093
  }
986
1094
  let width = 0;
987
1095
  const labelToBar = this.styles.fontStyles.size * this.styles.scaleFactors.labelToBar;
988
1096
  if (VarianceType.NONE === varianceType &&
989
- ((_a = this.highestDataPointValue) === null || _a === void 0 ? void 0 : _a.value) != null &&
1097
+ ((_d = this.highestDataPointValue) === null || _d === void 0 ? void 0 : _d.value) != null &&
990
1098
  this.highestDataPointValue.value >= 0) {
991
1099
  const sizeLabelValue = this.calculateTextBoxWidth(this.highestDataPointValue.value);
992
1100
  width = Math.abs(this.highestDataPointValue.getScaledUnit()) + labelToBar + sizeLabelValue;
993
1101
  }
994
1102
  else if (VarianceType.RELATIVE === varianceType &&
995
- ((_b = this.highestVarianceDataPointValue) === null || _b === void 0 ? void 0 : _b.value) != null &&
1103
+ ((_e = this.highestVarianceDataPointValue) === null || _e === void 0 ? void 0 : _e.value) != null &&
996
1104
  this.highestVarianceDataPointValue.value >= 0) {
997
1105
  const sizeLabelValue = this.calculatePercentageTextBoxWidth(this.highestVarianceDataPointValue.value);
998
1106
  width = Math.abs(this.highestVarianceDataPointValue.getScaledPercentUnit()) + labelToBar + sizeLabelValue;
999
1107
  }
1000
1108
  else if (VarianceType.ABSOLUTE === varianceType &&
1001
- ((_c = this.highestVarianceDataPointValue) === null || _c === void 0 ? void 0 : _c.value) != null &&
1109
+ ((_f = this.highestVarianceDataPointValue) === null || _f === void 0 ? void 0 : _f.value) != null &&
1002
1110
  this.highestVarianceDataPointValue.value >= 0) {
1003
1111
  const sizeLabelValue = this.calculateTextBoxWidth(this.highestVarianceDataPointValue.value);
1004
1112
  width = Math.abs(this.highestVarianceDataPointValue.getScaledUnit()) + labelToBar + sizeLabelValue;
1005
1113
  }
1114
+ width = Math.max(width, this.referenceLineExtent());
1115
+ // the bold number of a sum row stands at the axis
1116
+ for (const row of this.dataModel) {
1117
+ if (row.sum && VarianceType.NONE === varianceType && Number.isFinite(row.scenario1.value)) {
1118
+ width = Math.max(width, this.sumRowExtent(row));
1119
+ }
1120
+ }
1006
1121
  if (width <= this.titleLength) {
1007
1122
  return this.titleLength;
1008
1123
  }
@@ -1013,6 +1128,7 @@ export class StructureChart {
1013
1128
  * at the stack dimension level and attach stackSegments to each dataModel row.
1014
1129
  */
1015
1130
  populateDimensionStackSegments(scenarios, _stackConfig, level) {
1131
+ var _a;
1016
1132
  if (this.rowCount === 0 || !this.fromExpr || scenarios.length === 0)
1017
1133
  return;
1018
1134
  // Derive stack column from level: stack by the next-lower hierarchy dimension
@@ -1031,6 +1147,7 @@ export class StructureChart {
1031
1147
  let result;
1032
1148
  try {
1033
1149
  result = this.db.exec(`SELECT [${displayColumn}] as displayGroup, [${stackColumn}] as stackLabel, ` +
1150
+ `MIN([${stackColumn}Index]) as stackIndex, ` +
1034
1151
  `SUM([${scenario}] * transformOperator(operation)) as stackValue ` +
1035
1152
  `FROM ${this.fromExpr} GROUP BY [${displayColumn}], [${stackColumn}]`);
1036
1153
  }
@@ -1038,19 +1155,46 @@ export class StructureChart {
1038
1155
  this.failQuery(e);
1039
1156
  return;
1040
1157
  }
1158
+ // The series of the chart: by the index column of the stack dimension, else in the order of the rows
1159
+ const series = new Map();
1160
+ for (const row of result) {
1161
+ if (row.stackValue == null || row.stackLabel == null)
1162
+ continue;
1163
+ const label = row.stackLabel;
1164
+ const index = Number.isFinite(Number(row.stackIndex)) && row.stackIndex != null ? Number(row.stackIndex) : NaN;
1165
+ const known = series.get(label);
1166
+ if (known === undefined || (Number.isNaN(known) && !Number.isNaN(index)) || index < known) {
1167
+ series.set(label, index);
1168
+ }
1169
+ }
1170
+ const order = [...series.keys()];
1171
+ if ([...series.values()].every((index) => !Number.isNaN(index))) {
1172
+ order.sort((a, b) => series.get(a) - series.get(b));
1173
+ }
1041
1174
  // Group results by displayGroup
1042
1175
  const groupMap = new Map();
1043
1176
  for (const row of result) {
1044
1177
  const key = row.displayGroup;
1045
1178
  if (!groupMap.has(key))
1046
- groupMap.set(key, []);
1179
+ groupMap.set(key, new Map());
1047
1180
  if (row.stackValue != null && row.stackLabel != null) {
1048
- groupMap.get(key).push({ label: row.stackLabel, value: row.stackValue });
1181
+ groupMap.get(key).set(row.stackLabel, row.stackValue);
1182
+ }
1183
+ }
1184
+ // the REST row stands for the rows the limit cut off: it gets their sum per series
1185
+ const shown = new Set(this.dataModel.map((row) => row.category));
1186
+ const rest = new Map();
1187
+ for (const [category, values] of groupMap) {
1188
+ if (shown.has(category))
1189
+ continue;
1190
+ for (const [label, value] of values) {
1191
+ rest.set(label, ((_a = rest.get(label)) !== null && _a !== void 0 ? _a : 0) + value);
1049
1192
  }
1050
1193
  }
1051
- // Attach stackSegments to each dataModel row
1194
+ // Attach stackSegments to each dataModel row: every row has every series, so place and colour stay
1052
1195
  for (const row of this.dataModel) {
1053
- const segments = groupMap.get(row.category);
1196
+ const values = row.id === REST_ROW_ID && !groupMap.has(row.category) ? rest : groupMap.get(row.category);
1197
+ const segments = values && values.size > 0 ? order.map((label) => { var _a; return ({ label, value: (_a = values.get(label)) !== null && _a !== void 0 ? _a : NaN }); }) : undefined;
1054
1198
  if (segments && segments.length > 0) {
1055
1199
  row.stackSegments = segments.map((seg) => {
1056
1200
  const dataPoint = new DataPoint(seg.value, scenario, this.pixelPerUnit, Scenario.NEU);
@@ -1063,6 +1207,342 @@ export class StructureChart {
1063
1207
  }
1064
1208
  }
1065
1209
  }
1210
+ /** A sum row that is printed as numbers: every stack but a 100 % stack, where it is a bar of shares. */
1211
+ isTextSum(row) {
1212
+ return !!row.sum && !!this.stackConfig && this.stackConfig.mode !== StackMode.RELATIVE;
1213
+ }
1214
+ isItalic(category) {
1215
+ return this.italicCategories.includes(category);
1216
+ }
1217
+ hasPercentMark() {
1218
+ var _a;
1219
+ return (!!((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.percentMark) &&
1220
+ this.stackConfig.mode === StackMode.RELATIVE &&
1221
+ this.variancesDataModel.length === 0 &&
1222
+ this.isStacked());
1223
+ }
1224
+ /**
1225
+ * Adds the sum rows: one below the rows of each `rowGroup`, which keeps the rows of a
1226
+ * group together, and one over all rows at the end.
1227
+ */
1228
+ addSumRows(scenarios) {
1229
+ if (this.sums === "none" || this.dataModel.length === 0)
1230
+ return;
1231
+ // a cell without a value is left out, and a sum over no value stays NaN
1232
+ const add = (sum, value) => Number.isFinite(value) ? (Number.isFinite(sum) ? sum : 0) + value : sum;
1233
+ const sumRow = (members, category, sum) => {
1234
+ var _a, _b;
1235
+ const point = (key, index) => new DataPoint(scenarios[index] === undefined
1236
+ ? undefined
1237
+ : members.reduce((total, row) => add(total, row[key].value), NaN), scenarios[index], this.pixelPerUnit, Scenario.NEU);
1238
+ const row = {
1239
+ id: 10000 + this.dataModel.length,
1240
+ category,
1241
+ type: VarianceType.NONE,
1242
+ scenario1: point("scenario1", 0),
1243
+ scenario2: point("scenario2", 1),
1244
+ scenario3: point("scenario3", 2),
1245
+ scenario4: point("scenario4", 3),
1246
+ sum,
1247
+ };
1248
+ if (members.some((member) => member.stackSegments)) {
1249
+ const values = new Map();
1250
+ for (const member of members) {
1251
+ for (const segment of (_a = member.stackSegments) !== null && _a !== void 0 ? _a : []) {
1252
+ values.set(segment.label, add((_b = values.get(segment.label)) !== null && _b !== void 0 ? _b : NaN, segment.value));
1253
+ }
1254
+ }
1255
+ row.stackSegments = [...values].map(([label, value]) => {
1256
+ const dataPoint = new DataPoint(value, scenarios[0], this.pixelPerUnit, Scenario.NEU);
1257
+ return { label, value, dataPoint };
1258
+ });
1259
+ }
1260
+ return row;
1261
+ };
1262
+ const rows = this.dataModel;
1263
+ const result = [];
1264
+ const groups = this.sums === "total" ? new Map() : this.rowGroups();
1265
+ if (groups.size > 0) {
1266
+ const names = [...new Set([...groups.values()].sort((a, b) => a.index - b.index).map((group) => group.name))];
1267
+ for (const name of names) {
1268
+ const members = rows.filter((row) => { var _a; return ((_a = groups.get(row.category)) === null || _a === void 0 ? void 0 : _a.name) === name; });
1269
+ if (members.length === 0)
1270
+ continue;
1271
+ const sum = sumRow(members, name, "group");
1272
+ sum.gapAfter = true;
1273
+ result.push(...members, sum);
1274
+ }
1275
+ // a row without a group, like REST, comes last
1276
+ result.push(...rows.filter((row) => !groups.has(row.category)));
1277
+ }
1278
+ else {
1279
+ result.push(...rows);
1280
+ }
1281
+ if (this.sums === "total" || this.sums === "all") {
1282
+ result.push(sumRow(rows, this.sumTitle || "SUM_TOTAL", "total"));
1283
+ }
1284
+ this.dataModel = result;
1285
+ }
1286
+ /** The `rowGroup` of each row of a chart at level `category`, with the place of the group. */
1287
+ rowGroups() {
1288
+ const groups = new Map();
1289
+ if (this.level !== AggregationLevel.CATEGORY && this.level !== AggregationLevel.CATEGORYINDEX)
1290
+ return groups;
1291
+ let result;
1292
+ try {
1293
+ result = this.db.exec(`SELECT [category] as category, [rowGroup] as rowGroup, MIN([rowGroupIndex]) as rowGroupIndex ` +
1294
+ `FROM ${this.fromExpr} GROUP BY [category], [rowGroup]`);
1295
+ }
1296
+ catch (e) {
1297
+ this.failQuery(e);
1298
+ return groups;
1299
+ }
1300
+ const first = new Map();
1301
+ result.forEach((row, position) => {
1302
+ var _a;
1303
+ if (row.rowGroup == null || row.rowGroup === "")
1304
+ return;
1305
+ const name = String(row.rowGroup);
1306
+ const given = Number(row.rowGroupIndex);
1307
+ // without an index column the groups keep the order of the rows
1308
+ const index = row.rowGroupIndex != null && Number.isFinite(given) ? given : ((_a = first.get(name)) !== null && _a !== void 0 ? _a : position);
1309
+ if (!first.has(name))
1310
+ first.set(name, index);
1311
+ groups.set(String(row.category), { name, index: Math.min(index, first.get(name)) });
1312
+ });
1313
+ return groups;
1314
+ }
1315
+ /** Rows of different height (`stack.thickness: total`) and the room below a group. Null: all rows are equal. */
1316
+ rowLayout() {
1317
+ var _a;
1318
+ const rows = this.dataModel;
1319
+ // the component does not set it together with variances: a variance bar has one thickness
1320
+ const byTotal = ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.thickness) === "total";
1321
+ // the name of the scenario stands below the last bar: it needs room when a sum row follows
1322
+ const lastBar = rows.reduce((last, row, index) => (row.sum ? last : index), -1);
1323
+ const nameBelow = !this.stackConfig && !this.scenarioLabelTop && lastBar >= 0 && lastBar < rows.length - 1;
1324
+ if (!byTotal && !nameBelow && !rows.some((row) => row.gapAfter))
1325
+ return null;
1326
+ const totals = rows.map((row) => {
1327
+ if (!byTotal || row.sum)
1328
+ return null;
1329
+ const groups = this.stackGroups(row);
1330
+ const front = groups[groups.length - 1];
1331
+ return front ? Math.abs(front.layout.total) : 0;
1332
+ });
1333
+ const weights = this.rowWeights(totals);
1334
+ const start = [];
1335
+ const weight = [];
1336
+ let at = 0;
1337
+ rows.forEach((row, index) => {
1338
+ const rowWeight = weights[index];
1339
+ start.push(at);
1340
+ weight.push(rowWeight);
1341
+ at +=
1342
+ rowWeight + (row.gapAfter && index < rows.length - 1 ? 0.5 : 0) + (nameBelow && index === lastBar ? 0.8 : 0);
1343
+ });
1344
+ return { start, weight, total: at };
1345
+ }
1346
+ /**
1347
+ * The height of each row in units of a normal row: by its total, with the same height
1348
+ * over all. A row is at least one line of text high, and the others give up that room.
1349
+ * Null stands for a row that keeps the normal height.
1350
+ */
1351
+ rowWeights(totals) {
1352
+ const minimum = Math.min(1, 1 / this.styles.scaleFactors.categoryWidth);
1353
+ const weights = totals.map((total) => (total === null ? 1 : 0));
1354
+ const open = new Set(totals.flatMap((total, index) => (total === null ? [] : [index])));
1355
+ let room = open.size;
1356
+ // a row below the minimum is set to it, then the rest is shared again
1357
+ for (;;) {
1358
+ const sum = [...open].reduce((all, index) => all + totals[index], 0);
1359
+ if (sum <= 0) {
1360
+ open.forEach((index) => (weights[index] = room / open.size));
1361
+ break;
1362
+ }
1363
+ const small = [...open].filter((index) => (totals[index] / sum) * room < minimum);
1364
+ if (small.length === 0 || small.length === open.size) {
1365
+ open.forEach((index) => (weights[index] = (totals[index] / sum) * room));
1366
+ break;
1367
+ }
1368
+ for (const index of small) {
1369
+ weights[index] = minimum;
1370
+ room -= minimum;
1371
+ open.delete(index);
1372
+ }
1373
+ }
1374
+ return weights;
1375
+ }
1376
+ boldWidth(text) {
1377
+ return CalculateSize(text, {
1378
+ font: this.styles.fontStyles.font,
1379
+ fontSize: this.styles.fontStyles.size + "px",
1380
+ fontWeight: "bold",
1381
+ }).width;
1382
+ }
1383
+ sumText(value) {
1384
+ const digits = this.dataFormatDigits.decimal;
1385
+ return Formatter.number(value, "decimal", digits, digits, this.measureScale.value, this.styles.locale);
1386
+ }
1387
+ /**
1388
+ * Where the numbers of the text sum rows stand, as distance from the axis: one column
1389
+ * per series, the same for all sum rows, then the total. Null without such a row.
1390
+ */
1391
+ sumColumns() {
1392
+ const rows = this.dataModel.filter((row) => this.isTextSum(row));
1393
+ if (rows.length === 0)
1394
+ return null;
1395
+ const font = this.styles.fontStyles.size;
1396
+ const key = font * 0.6;
1397
+ const keyGap = font * 0.3;
1398
+ const columnGap = font * 1.4;
1399
+ const widths = [];
1400
+ let totalWidth = 0;
1401
+ for (const row of rows) {
1402
+ const groups = this.stackGroups(row);
1403
+ const front = groups[groups.length - 1];
1404
+ if (!front)
1405
+ continue;
1406
+ front.segments.forEach((segment, index) => {
1407
+ var _a;
1408
+ const width = Number.isFinite(segment.value) ? this.boldWidth(this.sumText(segment.value)) : 0;
1409
+ widths[index] = Math.max((_a = widths[index]) !== null && _a !== void 0 ? _a : 0, width);
1410
+ });
1411
+ totalWidth = Math.max(totalWidth, this.boldWidth(this.sumText(front.layout.total)));
1412
+ }
1413
+ const x = [];
1414
+ let at = font * 0.3;
1415
+ // one series: its sum is the total
1416
+ for (const width of widths.length > 1 ? widths : []) {
1417
+ x.push(at);
1418
+ at += key + keyGap + width + columnGap;
1419
+ }
1420
+ return { x, totalX: at, width: at + totalWidth, key, keyGap };
1421
+ }
1422
+ /** A sum row of a stack as numbers: per series a small square in its colour and the sum, then the total. */
1423
+ drawSumText(row, index, group) {
1424
+ const columns = this.sumColumns();
1425
+ const groups = this.stackGroups(row);
1426
+ const front = groups[groups.length - 1];
1427
+ if (!columns || !front)
1428
+ return;
1429
+ const axisX = this.position.getBarPositionX();
1430
+ const y = this.position.getBarCenterPositionY(index);
1431
+ const colors = this.styles.stackColors;
1432
+ const sumGroup = group.group().id(`stacked_sum_${index}`);
1433
+ front.segments.forEach((segment, segmentIndex) => {
1434
+ if (!Number.isFinite(segment.value) || columns.x[segmentIndex] === undefined)
1435
+ return;
1436
+ const x = axisX + columns.x[segmentIndex];
1437
+ sumGroup
1438
+ .rect(columns.key, columns.key)
1439
+ .fill({ color: colors[segmentIndex % colors.length] })
1440
+ .cy(y)
1441
+ .x(x);
1442
+ new Label(sumGroup, x + columns.key + columns.keyGap, y, `SUM_SEG_${segmentIndex}`, segment.value, this.styles, HookLocation.Center, HookLocation.Left)
1443
+ .setEmphasis(true, false)
1444
+ .setPoint(segment.dataPoint.key)
1445
+ .draw(this.dataFormatDigits, this.measureScale);
1446
+ });
1447
+ new Label(sumGroup, axisX + columns.totalX, y, "SUM_TOTAL", front.layout.total, this.styles, HookLocation.Center, HookLocation.Left)
1448
+ .setEmphasis(true, false)
1449
+ .setPoint(levelKey(this.level, row.category), "total")
1450
+ .draw(this.dataFormatDigits, this.measureScale);
1451
+ }
1452
+ /** How far a sum row reaches right of the axis, in px. */
1453
+ sumRowExtent(row) {
1454
+ var _a, _b, _c;
1455
+ const labelToBar = this.styles.fontStyles.size * this.styles.scaleFactors.labelToBar;
1456
+ if (this.variancesDataModel.length > 0)
1457
+ return 0;
1458
+ if (this.isTextSum(row))
1459
+ return (_b = (_a = this.sumColumns()) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
1460
+ if (this.stackConfig) {
1461
+ const total = this.stackConfig.labels === "value" ? labelToBar + this.boldWidth(this.sumText((_c = row.scenario1.value) !== null && _c !== void 0 ? _c : 0)) : 0;
1462
+ return this.stackSides.positive * this.pixelPerUnit + total;
1463
+ }
1464
+ return Number.isFinite(row.scenario1.value)
1465
+ ? labelToBar + this.boldWidth(this.sumText(row.scenario1.value))
1466
+ : 0;
1467
+ }
1468
+ /** Where the reference line stands, as distance from the axis in px. Null without a line in this chart. */
1469
+ referenceLineOffset() {
1470
+ var _a;
1471
+ const line = this.referenceLine;
1472
+ if (!line || !Number.isFinite(line.value) || this.variancesDataModel.length > 0)
1473
+ return null;
1474
+ const relative = ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE;
1475
+ return (relative ? line.value / 100 : line.value) * this.pixelPerUnit;
1476
+ }
1477
+ /** The room right of the axis that the reference line and its label need. */
1478
+ referenceLineExtent() {
1479
+ var _a;
1480
+ const offset = this.referenceLineOffset();
1481
+ if (offset === null || offset <= 0)
1482
+ return 0;
1483
+ const label = ((_a = this.referenceLine) === null || _a === void 0 ? void 0 : _a.label)
1484
+ ? CalculateSize(this.referenceLine.label, {
1485
+ font: this.styles.fontStyles.font,
1486
+ fontSize: this.styles.fontStyles.size + "px",
1487
+ }).width
1488
+ : 0;
1489
+ return offset + Math.max(label / 2, this.styles.fontStyles.size * 0.25);
1490
+ }
1491
+ /** The end of a 100 % stack: a line along the bar ends and one `100%` below it. */
1492
+ drawPercentMark(svgContainer) {
1493
+ if (!this.hasPercentMark())
1494
+ return;
1495
+ const group = svgContainer.group().id("PercentMark");
1496
+ const font = this.styles.fontStyles.size;
1497
+ const x = this.position.getBarPositionX() + this.stackSides.positive * this.pixelPerUnit;
1498
+ const top = this.position.getAxisPositionY();
1499
+ const bottom = this.position.getAxisEndPositionY();
1500
+ const tip = bottom + font * 0.15;
1501
+ const base = tip + font * 0.5;
1502
+ group.line(x, top, x, bottom).stroke({ color: "#000000", width: this.position.spacings.axisLineWidth });
1503
+ group.polygon(`${x},${tip} ${x - font * 0.25},${base} ${x + font * 0.25},${base}`).fill("#000000");
1504
+ new Label(group, x, base + font * 0.1, "PERCENT_MARK", "100%", this.styles, HookLocation.Top, HookLocation.Center)
1505
+ .setEmphasis(true, false)
1506
+ .draw();
1507
+ }
1508
+ /** A line across the bars at one value, with its label above the first bar. In a 100 % stack the value is a percent. */
1509
+ drawReferenceLine(svgContainer) {
1510
+ const line = this.referenceLine;
1511
+ const offset = this.referenceLineOffset();
1512
+ if (!line || offset === null || this.dataModel.length === 0)
1513
+ return;
1514
+ const group = svgContainer.group().id("ReferenceLine");
1515
+ const font = this.styles.fontStyles.size;
1516
+ const color = `var(--bn-annotation-color, ${SYSTEM_STYLE_DEFAULTS["bn-annotation"].color})`;
1517
+ const x = this.position.getBarPositionX() + offset;
1518
+ const top = this.position.getStackedHeaderPositionY() - (this.stackConfig || this.scenarioLabelTop ? font * 1.1 : 0);
1519
+ // down to the last row from the top that reaches the line, or over all rows when the first one does not
1520
+ const reaches = (row) => {
1521
+ var _a, _b;
1522
+ if (row.sum)
1523
+ return false;
1524
+ if (!this.stackConfig)
1525
+ return ((_a = row.scenario1.value) !== null && _a !== void 0 ? _a : 0) >= line.value;
1526
+ const first = (_b = this.stackGroups(row)[0]) === null || _b === void 0 ? void 0 : _b.layout.segments[0];
1527
+ return first != null && first.end * this.pixelPerUnit >= x - this.position.getBarPositionX();
1528
+ };
1529
+ let lastBar = this.dataModel.findIndex((row) => !reaches(row)) - 1;
1530
+ if (lastBar < 0)
1531
+ lastBar = this.dataModel.reduce((last, row, index) => (row.sum ? last : index), 0);
1532
+ const bottom = this.position.getBarCenterPositionY(lastBar) + this.position.getBarWidthAt(lastBar) / 2;
1533
+ group
1534
+ .line(x, top, x, bottom)
1535
+ .attr("style", `stroke: ${color}; stroke-width: ${Math.max(1, this.position.spacings.axisLineWidth)}px`);
1536
+ group
1537
+ .polygon(`${x},${top + font * 0.5} ${x - font * 0.25},${top} ${x + font * 0.25},${top}`)
1538
+ .attr("style", `fill: ${color}`);
1539
+ if (line.label) {
1540
+ group
1541
+ .plain(line.label)
1542
+ .attr({ x, y: top - font * 0.3, "text-anchor": "middle" })
1543
+ .attr("style", `fill: ${color}`);
1544
+ }
1545
+ }
1066
1546
  hasNoData() {
1067
1547
  return this.dataModel.length === 0;
1068
1548
  }