@bino-bi/bn-template-engine 1.0.0-next.48 → 1.0.0-next.50

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 (110) hide show
  1. package/dist/bn-template-engine/bn-template-engine.esm.js +1 -1
  2. package/dist/bn-template-engine/{p-b3f5eba0.entry.js → p-2de524d0.entry.js} +1 -1
  3. package/dist/bn-template-engine/{p-a6017fce.entry.js → p-510d2ee6.entry.js} +1 -1
  4. package/dist/bn-template-engine/{p-6ab684a9.entry.js → p-8fc90dc8.entry.js} +1 -1
  5. package/dist/bn-template-engine/p-BFl7OwcC.js +1 -0
  6. package/dist/bn-template-engine/{p-3ktFZb1x.js → p-Bxa4pX1M.js} +1 -1
  7. package/dist/bn-template-engine/p-c1d679af.entry.js +1 -0
  8. package/dist/cjs/bn-annotation-layer_30.cjs.entry.js +628 -108
  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-text.cjs.entry.js +2 -2
  12. package/dist/cjs/{withStyled-DHKHBdRb.js → withStyled-pkdctrK9.js} +18 -1
  13. package/dist/cjs/{withTranslation-CLwC9a8V.js → withTranslation-BIzTNYZM.js} +1 -1
  14. package/dist/collection/components/bn-annotation-layer/bn-annotation-layer.css +19 -0
  15. package/dist/collection/components/bn-annotation-layer/bn-annotation-layer.js +65 -8
  16. package/dist/collection/components/bn-annotation-layer/utils/marks.js +85 -5
  17. package/dist/collection/components/bn-annotation-layer/utils/matchPoint.js +7 -1
  18. package/dist/collection/components/bn-chart-time/bn-chart-time-renderer/bn-chart-time-renderer.js +20 -6
  19. package/dist/collection/components/bn-chart-time/bn-chart-time.js +55 -23
  20. package/dist/collection/components/bn-chart-time/utils/TimeChart.js +329 -53
  21. package/dist/collection/components/bn-chart-time/utils/elements/Column.js +4 -1
  22. package/dist/collection/components/bn-chart-time/utils/elements/Label.js +2 -1
  23. package/dist/collection/components/bn-chart-time/utils/elements/StackedColumn.js +53 -11
  24. package/dist/collection/utils/annotations.js +1 -1
  25. package/dist/collection/utils/constants.js +2 -0
  26. package/dist/collection/utils/layoutState/svgChartElements.js +10 -0
  27. package/dist/collection/utils/validate.js +16 -1
  28. package/dist/components/bn-annotation-layer.js +1 -1
  29. package/dist/components/bn-annotation-list.js +1 -1
  30. package/dist/components/bn-chart-bubble-renderer.js +1 -1
  31. package/dist/components/bn-chart-bubble.js +1 -1
  32. package/dist/components/bn-chart-bullet-renderer.js +1 -1
  33. package/dist/components/bn-chart-bullet.js +1 -1
  34. package/dist/components/bn-chart-scatter-renderer.js +1 -1
  35. package/dist/components/bn-chart-scatter.js +1 -1
  36. package/dist/components/bn-chart-structure-renderer.js +1 -1
  37. package/dist/components/bn-chart-structure.js +1 -1
  38. package/dist/components/bn-chart-time-renderer.js +1 -1
  39. package/dist/components/bn-chart-time.js +1 -1
  40. package/dist/components/bn-grid.js +1 -1
  41. package/dist/components/bn-image.js +1 -1
  42. package/dist/components/bn-layout-card.js +1 -1
  43. package/dist/components/bn-layout-page.js +1 -1
  44. package/dist/components/bn-page.js +1 -1
  45. package/dist/components/bn-table-renderer.js +1 -1
  46. package/dist/components/bn-table.js +1 -1
  47. package/dist/components/bn-text.js +1 -1
  48. package/dist/components/bn-title-renderer.js +1 -1
  49. package/dist/components/bn-title.js +1 -1
  50. package/dist/components/bn-tree.js +1 -1
  51. package/dist/components/{p-CLjuZoiT.js → p-B11cXOLy.js} +1 -1
  52. package/dist/components/p-BBdzTx8V.js +1 -0
  53. package/dist/components/{p-B1_wkOLx.js → p-BVNOmHh4.js} +1 -1
  54. package/dist/components/p-BXP6aagM.js +1 -0
  55. package/dist/components/{p-CG-iY7pQ.js → p-Blre9f28.js} +1 -1
  56. package/dist/components/p-CHJ6WE0l.js +1 -0
  57. package/dist/components/{p-Do4GqlMV.js → p-CHhxQoF9.js} +1 -1
  58. package/dist/components/{p-C-3TOa6K.js → p-CJ0-xdfZ.js} +1 -1
  59. package/dist/components/{p-BFoRIqDi.js → p-CO6bgquz.js} +1 -1
  60. package/dist/components/{p-CytMTBFu.js → p-CSJCkHEL.js} +1 -1
  61. package/dist/components/p-CVfmhtEW.js +1 -0
  62. package/dist/components/{p-Zuxd3z0q.js → p-Cc0gaLwp.js} +1 -1
  63. package/dist/components/p-CuUfiEu0.js +1 -0
  64. package/dist/components/{p-DUZCLRVu.js → p-D362XOg2.js} +1 -1
  65. package/dist/components/p-DBj1Yd3M.js +1 -0
  66. package/dist/components/{p-BjgkgCAQ.js → p-DHX92KDP.js} +1 -1
  67. package/dist/components/p-DHloMyxV.js +1 -0
  68. package/dist/components/{p-Uc0zvXcQ.js → p-Dd1QOWDV.js} +1 -1
  69. package/dist/components/p-DguYWLBj.js +1 -0
  70. package/dist/components/p-Do-IGHJk.js +1 -0
  71. package/dist/components/{p-DDo-fu3v.js → p-DsC0ew4j.js} +1 -1
  72. package/dist/components/p-HVN8BNWN.js +1 -0
  73. package/dist/components/{p-Dyzb4ika.js → p-YSRTB0jS.js} +1 -1
  74. package/dist/components/{p-CrZ_O3DT.js → p-k7heRDPP.js} +1 -1
  75. package/dist/components/{p-dO0utE8m.js → p-keKnrAVS.js} +1 -1
  76. package/dist/components/p-x0W2_FAX.js +1 -0
  77. package/dist/components/p-y12CmIFS.js +1 -0
  78. package/dist/esm/bn-annotation-layer_30.entry.js +628 -108
  79. package/dist/esm/bn-grid.entry.js +1 -1
  80. package/dist/esm/bn-image.entry.js +1 -1
  81. package/dist/esm/bn-text.entry.js +2 -2
  82. package/dist/esm/{withStyled-CTZFjg4L.js → withStyled-B62R20Zx.js} +18 -1
  83. package/dist/esm/{withTranslation-BESW5BNq.js → withTranslation-BEseMLQ6.js} +1 -1
  84. package/dist/schemas/stack.schema.json +22 -0
  85. package/dist/types/components/bn-annotation-layer/utils/marks.d.ts +48 -0
  86. package/dist/types/components/bn-annotation-layer/utils/matchPoint.d.ts +4 -0
  87. package/dist/types/components/bn-chart-time/bn-chart-time.d.ts +29 -6
  88. package/dist/types/components/bn-chart-time/utils/TimeChart.d.ts +51 -2
  89. package/dist/types/components/bn-chart-time/utils/elements/StackedColumn.d.ts +5 -1
  90. package/dist/types/components/bn-chart-time/utils/model/TimeChartComponentConfig.d.ts +6 -0
  91. package/dist/types/components.d.ts +6 -6
  92. package/dist/types/schemas.d.ts +6 -0
  93. package/dist/types/utils/annotations.d.ts +11 -1
  94. package/dist/types/utils/constants.d.ts +3 -1
  95. package/dist/types/utils/layoutState/types.d.ts +2 -0
  96. package/package.json +1 -1
  97. package/dist/bn-template-engine/p-203e7d6a.entry.js +0 -1
  98. package/dist/bn-template-engine/p-DXtYQE4s.js +0 -1
  99. package/dist/components/p-6gET6YXc.js +0 -1
  100. package/dist/components/p-BFWvVe7v.js +0 -1
  101. package/dist/components/p-C8rpOS5t.js +0 -1
  102. package/dist/components/p-CUoPUIej.js +0 -1
  103. package/dist/components/p-ChpAMqbN.js +0 -1
  104. package/dist/components/p-D3hr9wra.js +0 -1
  105. package/dist/components/p-DFybNETX.js +0 -1
  106. package/dist/components/p-DRmphEA0.js +0 -1
  107. package/dist/components/p-D_K7tYhY.js +0 -1
  108. package/dist/components/p-Dw3rv_Y9.js +0 -1
  109. package/dist/components/p-TNX3alPU.js +0 -1
  110. package/dist/components/p-j42D1yrQ.js +0 -1
@@ -200,6 +200,8 @@ export class TimeChart {
200
200
  this.scenarioSwitch = (_b = config.scenarioSwitch) !== null && _b !== void 0 ? _b : false;
201
201
  this.axisTitlePosition = (_c = config.axisTitlePosition) !== null && _c !== void 0 ? _c : "left";
202
202
  this.dateInterval = this.findDateInterval(config.dateInterval);
203
+ // the model of a stack by dimension holds the series
204
+ this.stackConfig = config.stackConfig;
203
205
  this.dataModel = this.populateModel(scenarios, this.dateInterval);
204
206
  this.variancesDataModel = this.populateVarianceModel(config.variances, this.dataModel);
205
207
  this.configTitle = config.title;
@@ -323,7 +325,9 @@ export class TimeChart {
323
325
  }).width;
324
326
  titleEnd = axisEnd + this.position.spacings.labelToColumn + titleWidth + this.styles.padding.right;
325
327
  }
326
- return Math.max(axisEnd + labelBuffer, bboxTotal, titleEnd);
328
+ const legendWidth = this.legendWidth("right");
329
+ const legendEnd = legendWidth > 0 ? axisEnd + this.position.spacings.labelToColumn + legendWidth + this.styles.padding.right : 0;
330
+ return Math.max(axisEnd + labelBuffer, bboxTotal, titleEnd, legendEnd);
327
331
  }
328
332
  getAllocatedCanvasHeight() {
329
333
  if (this.smallestVarianceDataPointValue.value == null ||
@@ -356,9 +360,71 @@ export class TimeChart {
356
360
  : this.scenariosArea.height;
357
361
  return Math.max(analyticalHeight, bboxHeight) + this.switchNamesHeight();
358
362
  }
359
- /** Room for the scenario names under the category labels of a series that changes its scenario. */
363
+ /**
364
+ * Room for the second row under the category labels: the scenario names of a
365
+ * `scenarioSwitch` chart, and in `two-row` mode the parent periods.
366
+ */
360
367
  switchNamesHeight() {
361
- return !this.hideAxisLabels && this.switchRuns().length > 1 ? this.position.dataLabelHeight : 0;
368
+ return !this.hideAxisLabels && this.contextRow().length > 0 ? this.position.dataLabelHeight : 0;
369
+ }
370
+ /** The parent period of a period in `two-row` mode: the year of a month, quarter or week, the month of a day. */
371
+ parentPeriodLabel(date) {
372
+ if (this.axisLabelsMode !== AxisLabelsMode.TWO_ROW || date == null || !date.isValid) {
373
+ return "";
374
+ }
375
+ switch (this.dateInterval) {
376
+ case DateDistance.MONTH:
377
+ case DateDistance.QUARTER:
378
+ return date.setLocale(this.locale).toFormat("yyyy");
379
+ case DateDistance.WEEK:
380
+ // week 1 can start in December: the week year, as the week labels use it
381
+ return date.setLocale(this.locale).toFormat("kkkk");
382
+ case DateDistance.DAY:
383
+ return date.setLocale(this.locale).toFormat("LLL yyyy");
384
+ default:
385
+ return "";
386
+ }
387
+ }
388
+ /**
389
+ * The texts of the second row under the category labels. `two-row` mode: the
390
+ * parent period under the first period and where it changes, joined with the
391
+ * scenario name where a run starts, each under its period. Else the scenario
392
+ * names alone, each under the middle of its run.
393
+ */
394
+ contextRow() {
395
+ const runs = this.switchRuns();
396
+ if (this.axisLabelsMode !== AxisLabelsMode.TWO_ROW) {
397
+ return runs.map((run) => ({
398
+ from: run.from,
399
+ to: run.to,
400
+ text: Formatter.translateScenarioLabel(run.scenario, this.i18n),
401
+ scenario: run.scenario,
402
+ }));
403
+ }
404
+ // the line chart writes its own labels and has no second row
405
+ if (!this.isVarianceChart() && this.determineEffectiveChartMode() !== ChartMode.BAR) {
406
+ return [];
407
+ }
408
+ const model = this.isVarianceChart() ? this.variancesDataModel : this.dataModel;
409
+ const entries = [];
410
+ let lastParent = "";
411
+ model.forEach((row, index) => {
412
+ var _a;
413
+ const run = runs.find((candidate) => candidate.from === index);
414
+ const parent = this.parentPeriodLabel(row.date);
415
+ const parts = [];
416
+ if (parent !== "" && parent !== lastParent) {
417
+ parts.push(parent);
418
+ }
419
+ lastParent = parent;
420
+ if (run) {
421
+ parts.push(Formatter.translateScenarioLabel(run.scenario, this.i18n));
422
+ }
423
+ if (parts.length > 0) {
424
+ entries.push({ from: index, to: index, text: parts.join(" "), scenario: (_a = run === null || run === void 0 ? void 0 : run.scenario) !== null && _a !== void 0 ? _a : "parent" });
425
+ }
426
+ });
427
+ return entries;
362
428
  }
363
429
  /** True for a scenario slot that has a value in this period. */
364
430
  static hasValue(dataPoint) {
@@ -369,7 +435,9 @@ export class TimeChart {
369
435
  * periods where the first scenario has none. Empty without `scenarioSwitch`.
370
436
  */
371
437
  followerSlots() {
372
- if (!this.scenarioSwitch || this.stackConfig != null || this.determineEffectiveChartMode() !== ChartMode.BAR) {
438
+ // a stack by scenario draws its scenarios on top of each other: nothing follows there
439
+ const stacksScenarios = this.stackConfig != null && this.stackConfig.by !== StackBy.DIMENSIONS;
440
+ if (!this.scenarioSwitch || stacksScenarios || this.determineEffectiveChartMode() !== ChartMode.BAR) {
373
441
  return [];
374
442
  }
375
443
  return ["scenario2", "scenario3", "scenario4"].filter((slot) => {
@@ -395,8 +463,13 @@ export class TimeChart {
395
463
  }
396
464
  /** Runs of periods with the same visible scenario. A period without a value belongs to the run before it. */
397
465
  switchRuns() {
466
+ const stacksScenarios = this.stackConfig != null && this.stackConfig.by !== StackBy.DIMENSIONS;
467
+ if (!this.scenarioSwitch || stacksScenarios || this.determineEffectiveChartMode() !== ChartMode.BAR) {
468
+ return [];
469
+ }
398
470
  const followers = this.followerSlots();
399
- if (followers.length === 0) {
471
+ // a chart with one scenario is one run: its scenario still gets its name under the axis
472
+ if (followers.length === 0 && this.scenarioPropOrder.length !== 1) {
400
473
  return [];
401
474
  }
402
475
  const runs = [];
@@ -413,6 +486,21 @@ export class TimeChart {
413
486
  });
414
487
  return runs;
415
488
  }
489
+ /**
490
+ * Stack by dimensions with `scenarioSwitch`: the scenarios with values that
491
+ * are not drawn, because they have values in periods of the first scenario
492
+ * or because the chart is a stacked area. The component warns about them.
493
+ */
494
+ getIgnoredSwitchScenarios() {
495
+ var _a;
496
+ if (!this.scenarioSwitch || ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.by) !== StackBy.DIMENSIONS || this.isVarianceChart()) {
497
+ return [];
498
+ }
499
+ const followers = this.followerSlots();
500
+ return ["scenario2", "scenario3", "scenario4"]
501
+ .filter((slot) => !followers.includes(slot) && this.dataModel.some((row) => TimeChart.hasValue(row[slot])))
502
+ .map((slot) => { var _a; return (_a = this.dataModel.find((row) => { var _a; return ((_a = row[slot]) === null || _a === void 0 ? void 0 : _a.scenario) != null; })) === null || _a === void 0 ? void 0 : _a[slot].scenario; });
503
+ }
416
504
  /** Number of periods where variances of one row overlap. The component warns about it. */
417
505
  getVarianceOverlaps() {
418
506
  return this.varianceOverlaps;
@@ -646,6 +734,8 @@ export class TimeChart {
646
734
  if (this.leftSideOffsetForHorizontalSync > -1) {
647
735
  maxLabelLengthLeft = this.leftSideOffsetForHorizontalSync;
648
736
  }
737
+ // the series names of a stack by dimension are not known to the component that hands in the offset
738
+ maxLabelLengthLeft = Math.max(maxLabelLengthLeft, this.legendWidth("left"));
649
739
  let min;
650
740
  let max;
651
741
  if (this.isVarianceChart()) {
@@ -735,6 +825,7 @@ export class TimeChart {
735
825
  return result;
736
826
  }
737
827
  populateModel(selectedScenarios, dateInterval) {
828
+ var _a;
738
829
  if (this.rowCount === 0 || !this.fromExpr) {
739
830
  return [];
740
831
  }
@@ -744,8 +835,64 @@ export class TimeChart {
744
835
  this.detectNaNCells(selectedScenarios, result);
745
836
  result = this.leftOuterJoinDates(result, dateInterval);
746
837
  const dataModel = this.createDataRows(result, selectedScenarios);
838
+ if (((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.by) === StackBy.DIMENSIONS) {
839
+ this.populateSeries(dataModel, selectedScenarios, dateInterval);
840
+ }
747
841
  return dataModel;
748
842
  }
843
+ /**
844
+ * Stack by dimension: one series per `category`, ordered by `categoryIndex`, else by first appearance.
845
+ * Every period gets every series, so a series keeps its place and colour when a value is missing.
846
+ */
847
+ populateSeries(dataModel, selectedScenarios, dateInterval) {
848
+ const rows = this.queryDataSet(dateInterval, ", [category]", ", [category]");
849
+ this.detectNaNCells(selectedScenarios, rows);
850
+ const keyOf = (row) => (row.category == null ? "" : String(row.category));
851
+ const timeOf = (date) => { var _a, _b; return (_b = (_a = date === null || date === void 0 ? void 0 : date.toMillis) === null || _a === void 0 ? void 0 : _a.call(date)) !== null && _b !== void 0 ? _b : Number.NaN; };
852
+ const order = [];
853
+ const byPeriod = new Map();
854
+ for (const row of rows) {
855
+ const key = keyOf(row);
856
+ if (!order.includes(key))
857
+ order.push(key);
858
+ const time = timeOf(row.calcdate);
859
+ if (!byPeriod.has(time))
860
+ byPeriod.set(time, new Map());
861
+ byPeriod.get(time).set(key, row);
862
+ }
863
+ // `categoryIndex` gives the order when the dataset has it, as for the rows of a structure chart
864
+ try {
865
+ const indexRows = this.db.exec(`SELECT [category], MIN(categoryIndex) AS idx FROM ${this.fromExpr} GROUP BY [category]`);
866
+ const indexOf = new Map(indexRows.map((row) => [row.category == null ? "" : String(row.category), row.idx]));
867
+ if (order.every((key) => Number.isFinite(indexOf.get(key)))) {
868
+ order.sort((a, b) => indexOf.get(a) - indexOf.get(b));
869
+ }
870
+ }
871
+ catch (_a) {
872
+ // no usable index: the order of first appearance stays
873
+ }
874
+ const slots = ["scenario1", "scenario2", "scenario3", "scenario4"];
875
+ for (const entry of dataModel) {
876
+ const period = byPeriod.get(timeOf(entry.date));
877
+ entry.series = order.map((key) => {
878
+ const row = period === null || period === void 0 ? void 0 : period.get(key);
879
+ const points = {};
880
+ slots.forEach((slot, index) => {
881
+ const scenario = selectedScenarios[index];
882
+ if (scenario == null)
883
+ return;
884
+ const raw = row === null || row === void 0 ? void 0 : row[scenario];
885
+ const value = typeof raw === "number" ? raw : Number.NaN;
886
+ const point = new DataPoint(value, scenario, this.pixelPerUnit, Scenario.NEU);
887
+ if (Number.isFinite(value)) {
888
+ point.key = { date: TimeChart.pointDate(entry.date), scenario, category: key };
889
+ }
890
+ points[slot] = point;
891
+ });
892
+ return { key, points };
893
+ });
894
+ }
895
+ }
749
896
  detectNaNCells(scenarios, res) {
750
897
  res.forEach((row) => {
751
898
  scenarios.forEach((scenario) => {
@@ -1123,6 +1270,10 @@ export class TimeChart {
1123
1270
  else if (this.axisLabelsMode === AxisLabelsMode.SHORT) {
1124
1271
  datelabel = dateStringFactory.getShortDateString(elements.date, this.dateInterval);
1125
1272
  }
1273
+ else if (this.axisLabelsMode === AxisLabelsMode.TWO_ROW && this.dateInterval !== DateDistance.YEAR) {
1274
+ // the parent period stands in the second row
1275
+ datelabel = dateStringFactory.getShortDateString(elements.date, this.dateInterval);
1276
+ }
1126
1277
  new Label(axisLabelGroup, this.position.getColumnCenterPositionX(index), this.position.getAxisPositionY(isRelative) + offset, "", datelabel, this.styles, hookLocation, HookLocation.Center).forceStringTypeDraw();
1127
1278
  });
1128
1279
  }
@@ -1154,7 +1305,8 @@ export class TimeChart {
1154
1305
  */
1155
1306
  drawScenarioSwitch(svgContainer) {
1156
1307
  const runs = this.switchRuns();
1157
- if (runs.length < 2) {
1308
+ const entries = this.contextRow();
1309
+ if (runs.length < 2 && entries.length === 0) {
1158
1310
  return;
1159
1311
  }
1160
1312
  const group = svgContainer.group().id("ScenarioSwitch");
@@ -1172,9 +1324,13 @@ export class TimeChart {
1172
1324
  if (namesHeight === 0) {
1173
1325
  return;
1174
1326
  }
1175
- runs.forEach((run) => {
1176
- const center = (this.position.getColumnCenterPositionX(run.from) + this.position.getColumnCenterPositionX(run.to)) / 2;
1177
- new Label(group, center, bottom - namesHeight, "SWITCH_" + run.scenario, Formatter.translateScenarioLabel(run.scenario, this.i18n), this.styles, HookLocation.Top, HookLocation.Center).forceStringTypeDraw();
1327
+ // in two-row mode a text starts at the left edge of its period, like the parent period it carries
1328
+ const leftAligned = this.axisLabelsMode === AxisLabelsMode.TWO_ROW;
1329
+ entries.forEach((entry) => {
1330
+ const x = leftAligned
1331
+ ? this.position.getColumnCenterPositionX(entry.from) - this.position.getColumnWidth() / 2
1332
+ : (this.position.getColumnCenterPositionX(entry.from) + this.position.getColumnCenterPositionX(entry.to)) / 2;
1333
+ new Label(group, x, bottom - namesHeight, "SWITCH_" + entry.scenario + "_" + entry.from, entry.text, this.styles, HookLocation.Top, leftAligned ? HookLocation.Left : HookLocation.Center).forceStringTypeDraw();
1178
1334
  });
1179
1335
  }
1180
1336
  drawScenarios(dataModel, svgContainer) {
@@ -1425,14 +1581,52 @@ export class TimeChart {
1425
1581
  * single one. Line mode: the area stacks every scenario in one stack.
1426
1582
  */
1427
1583
  stackGroups(row) {
1584
+ var _a;
1428
1585
  if (!this.stackConfig)
1429
1586
  return [];
1430
1587
  const isScenarioStack = this.stackConfig.by === StackBy.SCENARIOS;
1431
1588
  const isArea = this.determineEffectiveChartMode() === ChartMode.LINE;
1589
+ const relativeSpan = this.stackConfig.mode === StackMode.RELATIVE ? RELATIVE_STACK_UNITS : 0;
1590
+ // a segment without a value sorts like 0: NaN in the comparison would leave the stack unsorted
1591
+ const sortValue = (segment) => (Number.isFinite(segment.value) ? segment.value : 0);
1592
+ const sortByValue = (list) => {
1593
+ if (this.stackConfig.order === "asc") {
1594
+ list.sort((a, b) => sortValue(a) - sortValue(b));
1595
+ }
1596
+ else if (this.stackConfig.order === "desc") {
1597
+ list.sort((a, b) => sortValue(b) - sortValue(a));
1598
+ }
1599
+ };
1600
+ if (!isScenarioStack && !isArea && row.series) {
1601
+ // one stack of the scenario that is visible in this period
1602
+ const slot = (_a = this.visibleSlot(row, this.followerSlots())) !== null && _a !== void 0 ? _a : "scenario1";
1603
+ const series = [];
1604
+ row.series.forEach((entry, index) => {
1605
+ const dataPoint = entry.points[slot];
1606
+ if (dataPoint) {
1607
+ series.push({
1608
+ value: dataPoint.value,
1609
+ scenario: dataPoint.scenario,
1610
+ dataPoint,
1611
+ colorIndex: index,
1612
+ label: entry.key,
1613
+ });
1614
+ }
1615
+ });
1616
+ if (!series.some((segment) => Number.isFinite(segment.value)))
1617
+ return [];
1618
+ sortByValue(series);
1619
+ return [
1620
+ {
1621
+ prefix: "",
1622
+ segments: series,
1623
+ layout: layoutStack(series.map((segment) => segment.value), relativeSpan),
1624
+ hasValue: true,
1625
+ },
1626
+ ];
1627
+ }
1432
1628
  const segments = [];
1433
- for (const key of isScenarioStack || isArea
1434
- ? ["scenario1", "scenario2", "scenario3", "scenario4"]
1435
- : ["scenario1"]) {
1629
+ for (const key of isScenarioStack ? ["scenario1", "scenario2", "scenario3", "scenario4"] : ["scenario1"]) {
1436
1630
  const dp = row[key];
1437
1631
  if (dp && !dp.isEmpty() && dp.value != null) {
1438
1632
  segments.push({ value: dp.value, scenario: dp.scenario, dataPoint: dp });
@@ -1440,7 +1634,6 @@ export class TimeChart {
1440
1634
  }
1441
1635
  if (segments.length === 0)
1442
1636
  return [];
1443
- const relativeSpan = this.stackConfig.mode === StackMode.RELATIVE ? RELATIVE_STACK_UNITS : 0;
1444
1637
  const toGroup = (prefix, groupSegments) => ({
1445
1638
  prefix,
1446
1639
  segments: groupSegments,
@@ -1476,14 +1669,7 @@ export class TimeChart {
1476
1669
  return groups;
1477
1670
  }
1478
1671
  }
1479
- // a segment without a value sorts like 0: NaN in the comparison would leave the stack unsorted
1480
- const sortValue = (segment) => (Number.isFinite(segment.value) ? segment.value : 0);
1481
- if (this.stackConfig.order === "asc") {
1482
- segments.sort((a, b) => sortValue(a) - sortValue(b));
1483
- }
1484
- else if (this.stackConfig.order === "desc") {
1485
- segments.sort((a, b) => sortValue(b) - sortValue(a));
1486
- }
1672
+ sortByValue(segments);
1487
1673
  return [toGroup("", segments)];
1488
1674
  }
1489
1675
  /**
@@ -1503,9 +1689,10 @@ export class TimeChart {
1503
1689
  groups.forEach((group, groupIdx) => {
1504
1690
  let lastColumn = null;
1505
1691
  group.segments.forEach((seg, segIdx) => {
1692
+ var _a;
1506
1693
  const column = new StackedColumn(this.position, this.styles, seg.dataPoint, index, group.layout.segments[segIdx], seg.scenario,
1507
1694
  // overlay: one colour per group, back=lighter, front=darker
1508
- groups.length > 1 ? groups.length - 1 - groupIdx : segIdx, this.stackConfig.mode, group.layout, isScenarioStack, stackGroup);
1695
+ groups.length > 1 ? groups.length - 1 - groupIdx : ((_a = seg.colorIndex) !== null && _a !== void 0 ? _a : segIdx), this.stackConfig.mode, group.layout, isScenarioStack, stackGroup, this.stackConfig.notation === "scenario", this.hasPercentMark());
1509
1696
  column.draw(this.dataFormatDigits, this.measureScale);
1510
1697
  lastColumn = column;
1511
1698
  });
@@ -1524,49 +1711,137 @@ export class TimeChart {
1524
1711
  width: stackBox.width + this.position.spacings.columnWidth,
1525
1712
  };
1526
1713
  }
1714
+ /** True for a 100 % stack that prints plain shares and one `100%` mark. */
1715
+ hasPercentMark() {
1716
+ var _a;
1717
+ return ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE && this.stackConfig.percentMark === true;
1718
+ }
1719
+ /** The side of the `100%` mark: with the series names, at the left when they stand on both sides or nowhere. */
1720
+ percentMarkSide() {
1721
+ var _a;
1722
+ return ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.legend) === "right" ? "right" : "left";
1723
+ }
1724
+ /** The name of a series of a stack by dimension. A translation of the category wins. */
1725
+ seriesName(key) {
1726
+ return (this.i18n.get(key) || key);
1727
+ }
1728
+ /**
1729
+ * Width of the widest text the stack writes on one side of the columns, 0 when
1730
+ * there is none. The names of a stack by scenario at the left are part of the
1731
+ * left offset the component hands in, so they do not count here.
1732
+ */
1733
+ legendWidth(side) {
1734
+ var _a, _b;
1735
+ // the stacked area draws no names and no mark
1736
+ if (!this.stackConfig || this.determineEffectiveChartMode() !== ChartMode.BAR)
1737
+ return 0;
1738
+ const legend = (_a = this.stackConfig.legend) !== null && _a !== void 0 ? _a : "left";
1739
+ const texts = [];
1740
+ if (legend === side || legend === "both") {
1741
+ const series = (_b = this.dataModel.find((row) => row.series != null)) === null || _b === void 0 ? void 0 : _b.series;
1742
+ if (series) {
1743
+ texts.push(...series.map((entry) => this.seriesName(entry.key)));
1744
+ }
1745
+ else if (side === "right" && this.stackConfig.by === StackBy.SCENARIOS) {
1746
+ texts.push(...this.lastScenarios.map((scenario) => (this.i18n.get(scenario) || scenario)));
1747
+ }
1748
+ }
1749
+ if (this.hasPercentMark() && this.percentMarkSide() === side) {
1750
+ texts.push("100%");
1751
+ }
1752
+ let width = 0;
1753
+ for (const text of texts) {
1754
+ width = Math.max(width, CalculateSize(text, { font: this.styles.fontStyles.font, fontSize: this.styles.fontStyles.size + "px" }).width);
1755
+ }
1756
+ return width;
1757
+ }
1527
1758
  /**
1528
1759
  * Draw column headers next to stacked columns showing what each group/segment represents.
1529
1760
  * For overlay mode (multiple scenario types): shows one header per group.
1530
1761
  * For single-group: shows individual segment headers.
1531
- * The headers take their place from the first period.
1762
+ * The headers at the left take their place from the first period, the ones at
1763
+ * the right from the last period with a value.
1532
1764
  */
1533
1765
  drawStackedHeaders(dataModel, svgContainer) {
1766
+ var _a, _b, _c;
1534
1767
  if (!this.stackConfig || dataModel.length === 0)
1535
1768
  return;
1536
1769
  const isScenarioStack = this.stackConfig.by === StackBy.SCENARIOS;
1537
- if (!isScenarioStack)
1770
+ const hasSeries = dataModel.some((row) => row.series != null);
1771
+ if (!isScenarioStack && !hasSeries)
1538
1772
  return;
1539
1773
  const headerGroup = svgContainer.group().id("StackedHeaders");
1540
- const groups = this.stackGroups(dataModel[0]);
1541
- if (groups.length === 0)
1542
- return;
1543
1774
  const axisY = this.position.getColumnPositionY();
1544
- const xBase = this.position.getColumnCenterPositionX(0) - this.position.getColumnWidth() / 2;
1545
- const labelX = xBase - this.position.spacings.labelToColumn;
1546
- const drawHeader = (centerY, labelText) => {
1547
- new Label(headerGroup, labelX, centerY, "STACK_HEADER", labelText, this.styles, HookLocation.Center, HookLocation.Right).forceStringTypeDraw();
1548
- };
1549
- if (groups.length > 1) {
1550
- // Overlay mode: one header per group
1551
- groups.forEach((group, groupIdx) => {
1552
- const { positive, negative, total } = group.layout;
1553
- const pixelPerUnit = group.segments[0].dataPoint.pixelPerUnit;
1554
- // The front-most group (drawn last, e.g. AC) is fully visible, so its
1555
- // label is centered on the bar. Back groups are mostly hidden behind it
1556
- // and only peek out at their outer end, so their labels align with that edge.
1557
- const isFrontGroup = groupIdx === groups.length - 1;
1558
- const outerEnd = total < 0 ? 0 - negative : positive;
1559
- const centerY = axisY - (isFrontGroup ? (positive - negative) / 2 : outerEnd) * pixelPerUnit;
1560
- drawHeader(centerY, (this.i18n.get(group.prefix + "1") || group.prefix.toUpperCase()));
1775
+ const halfColumn = this.position.getColumnWidth() / 2;
1776
+ const gap = this.position.spacings.labelToColumn;
1777
+ const withValue = dataModel.map((row, index) => (this.stackGroups(row).length > 0 ? index : -1));
1778
+ const filled = withValue.filter((index) => index >= 0);
1779
+ // a stack by scenario keeps the first period, also when it is empty
1780
+ const firstIndex = hasSeries ? ((_a = filled[0]) !== null && _a !== void 0 ? _a : 0) : 0;
1781
+ const lastIndex = (_b = filled[filled.length - 1]) !== null && _b !== void 0 ? _b : dataModel.length - 1;
1782
+ const drawSide = (side) => {
1783
+ const index = side === "left" ? firstIndex : lastIndex;
1784
+ const groups = this.stackGroups(dataModel[index]);
1785
+ if (groups.length === 0)
1786
+ return;
1787
+ const center = this.position.getColumnCenterPositionX(index);
1788
+ const labelX = side === "left" ? center - halfColumn - gap : center + halfColumn + gap;
1789
+ const drawHeader = (centerY, labelText) => {
1790
+ new Label(headerGroup, labelX, centerY, "STACK_HEADER", labelText, this.styles, HookLocation.Center, side === "left" ? HookLocation.Right : HookLocation.Left).forceStringTypeDraw();
1791
+ };
1792
+ if (groups.length > 1) {
1793
+ // Overlay mode: one header per group
1794
+ groups.forEach((group, groupIdx) => {
1795
+ const { positive, negative, total } = group.layout;
1796
+ const pixelPerUnit = group.segments[0].dataPoint.pixelPerUnit;
1797
+ // The front-most group (drawn last, e.g. AC) is fully visible, so its
1798
+ // label is centered on the bar. Back groups are mostly hidden behind it
1799
+ // and only peek out at their outer end, so their labels align with that edge.
1800
+ const isFrontGroup = groupIdx === groups.length - 1;
1801
+ const outerEnd = total < 0 ? 0 - negative : positive;
1802
+ const centerY = axisY - (isFrontGroup ? (positive - negative) / 2 : outerEnd) * pixelPerUnit;
1803
+ drawHeader(centerY, (this.i18n.get(group.prefix + "1") || group.prefix.toUpperCase()));
1804
+ });
1805
+ return;
1806
+ }
1807
+ // Single group: one header per segment, centered on the segment
1808
+ const [group] = groups;
1809
+ group.segments.forEach((seg, segIdx) => {
1810
+ // a series without a value in this period has no segment to stand next to
1811
+ if (seg.label != null && !Number.isFinite(seg.value))
1812
+ return;
1813
+ const { start, end } = group.layout.segments[segIdx];
1814
+ drawHeader(axisY - ((start + end) / 2) * seg.dataPoint.pixelPerUnit, seg.label != null ? this.seriesName(seg.label) : (this.i18n.get(seg.scenario) || seg.scenario));
1561
1815
  });
1562
- return;
1563
- }
1564
- // Single group: one header per segment, centered on the segment
1565
- const [group] = groups;
1566
- group.segments.forEach((seg, segIdx) => {
1567
- const { start, end } = group.layout.segments[segIdx];
1568
- drawHeader(axisY - ((start + end) / 2) * seg.dataPoint.pixelPerUnit, (this.i18n.get(seg.scenario) || seg.scenario));
1569
- });
1816
+ };
1817
+ const legend = (_c = this.stackConfig.legend) !== null && _c !== void 0 ? _c : "left";
1818
+ if (legend === "left" || legend === "both")
1819
+ drawSide("left");
1820
+ if (legend === "right" || legend === "both")
1821
+ drawSide("right");
1822
+ if (this.hasPercentMark() && filled.length > 0) {
1823
+ this.drawPercentMark(headerGroup, this.percentMarkSide() === "left" ? firstIndex : lastIndex);
1824
+ }
1825
+ }
1826
+ /** The `100%` label with a pointer at the upper end of a 100 % stack, on the side of the series names. */
1827
+ drawPercentMark(group, index) {
1828
+ const side = this.percentMarkSide();
1829
+ const direction = side === "left" ? -1 : 1;
1830
+ const top = this.position.getColumnPositionY() - RELATIVE_STACK_UNITS * this.pixelPerUnit;
1831
+ const edge = this.position.getColumnCenterPositionX(index) + direction * (this.position.getColumnWidth() / 2);
1832
+ const size = this.styles.fontStyles.size * 0.45;
1833
+ const tip = edge + direction * 1;
1834
+ const base = tip + direction * size;
1835
+ group
1836
+ .polygon([
1837
+ [tip, top],
1838
+ [base, top - size / 2],
1839
+ [base, top + size / 2],
1840
+ ])
1841
+ .fill(this.styles.fontStyles.color || "#000000")
1842
+ .addClass("percent-mark");
1843
+ // above the pointer, so the name of the uppermost series keeps its place next to its segment
1844
+ new Label(group, base, top - size / 2, "STACK_PERCENT_MARK", "100%", this.styles, HookLocation.Bottom, side === "left" ? HookLocation.Right : HookLocation.Left).forceStringTypeDraw();
1570
1845
  }
1571
1846
  /**
1572
1847
  * Draw stacked area chart (line mode with stack).
@@ -1589,7 +1864,8 @@ export class TimeChart {
1589
1864
  const layerScenarios = [];
1590
1865
  // Identify active layers
1591
1866
  const activeLayers = [];
1592
- for (const key of scenarioKeys) {
1867
+ // a stack by dimensions draws its first scenario only
1868
+ for (const key of isScenarioStack ? scenarioKeys : ["scenario1"]) {
1593
1869
  if (dataModel.some((el) => el[key] && !el[key].isEmpty())) {
1594
1870
  activeLayers.push(key);
1595
1871
  }
@@ -70,7 +70,7 @@ export class Column {
70
70
  return this;
71
71
  }
72
72
  draw(dataFormatDigits = { percent: 2, decimal: 1 }, measureScale = new SiPrefix()) {
73
- var _a, _b, _c;
73
+ var _a, _b, _c, _d;
74
74
  // TODE: JHE: Remove DataFOrmatDigits Type Duplicates
75
75
  if (this.isEmpty()) {
76
76
  return this;
@@ -86,6 +86,9 @@ export class Column {
86
86
  }
87
87
  this.scenarioGroup.attr(dataPointAttributes(this.dataPoint.key));
88
88
  (_c = this.columnGroup) === null || _c === void 0 ? void 0 : _c.attr(dataPointAttributes(this.dataPoint.key, "element"));
89
+ if (this.dataPoint.key && Number.isFinite(this.dataPoint.value)) {
90
+ (_d = this.columnGroup) === null || _d === void 0 ? void 0 : _d.attr("data-bn-value", this.dataPoint.value);
91
+ }
89
92
  if (this.columnGroup != null) {
90
93
  if (this.isOverflowing && this.overflowConfig) {
91
94
  this.drawOverdriveBar(fillStyle, strokeStyle);
@@ -182,7 +182,8 @@ export class Label {
182
182
  .stroke(background)
183
183
  .fill(background)
184
184
  .opacity(0.9)
185
- .move(rectX - 1, rectY)
185
+ // the box of a centred label is already centred with its margin
186
+ .move(this.horizontalHookLocation === HookLocation.Center ? rectX : rectX - 1, rectY)
186
187
  .insertBefore(text);
187
188
  }
188
189
  return text;