@bino-bi/bn-template-engine 1.0.0-next.62 → 1.0.0-next.64

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 (24) hide show
  1. package/dist/bn-template-engine/bn-template-engine.esm.js +1 -1
  2. package/dist/bn-template-engine/{p-24a8322b.entry.js → p-07c55f50.entry.js} +1 -1
  3. package/dist/cjs/bn-annotation-layer_30.cjs.entry.js +687 -261
  4. package/dist/cjs/bn-template-engine.cjs.js +1 -1
  5. package/dist/cjs/loader.cjs.js +1 -1
  6. package/dist/collection/components/bn-chart-time/bn-chart-time-renderer/bn-chart-time-renderer.js +8 -8
  7. package/dist/collection/components/bn-chart-time/bn-chart-time.js +116 -34
  8. package/dist/collection/components/bn-chart-time/utils/TimeChart.js +532 -216
  9. package/dist/collection/components/bn-chart-time/utils/helper/ElementPositions.js +23 -1
  10. package/dist/collection/components/bn-chart-time/utils/model/TimeChartComponentConfig.js +15 -8
  11. package/dist/components/bn-chart-time-renderer.js +1 -1
  12. package/dist/components/bn-chart-time.js +1 -1
  13. package/dist/components/p-B8-9TksQ.js +1 -0
  14. package/dist/esm/bn-annotation-layer_30.entry.js +687 -261
  15. package/dist/esm/bn-template-engine.js +1 -1
  16. package/dist/esm/loader.js +1 -1
  17. package/dist/types/components/bn-chart-time/bn-chart-time-renderer/bn-chart-time-renderer.d.ts +2 -2
  18. package/dist/types/components/bn-chart-time/bn-chart-time.d.ts +30 -16
  19. package/dist/types/components/bn-chart-time/utils/TimeChart.d.ts +63 -30
  20. package/dist/types/components/bn-chart-time/utils/helper/ElementPositions.d.ts +8 -0
  21. package/dist/types/components/bn-chart-time/utils/model/TimeChartComponentConfig.d.ts +16 -7
  22. package/dist/types/components.d.ts +8 -8
  23. package/package.json +1 -1
  24. package/dist/components/p-Cj1iKmdF.js +0 -1
@@ -107,8 +107,9 @@ export class TimeChart {
107
107
  this.lastScenarios = [];
108
108
  this.axisLabelsStep = 1;
109
109
  this.scalingIndicator = 0;
110
- this.totalColumn = null;
111
- this.totalVariance = null;
110
+ this.totals = null;
111
+ this.totalColumns = [];
112
+ this.totalVariances = [];
112
113
  this.highestColumnDataPointValue = new DataPoint(0, "default", 0, Scenario.NEU);
113
114
  this.highestPeriodVarianceDataPointValue = new DataPoint(0, "default", 0, Scenario.NEU);
114
115
  this.scenarioSwitch = false;
@@ -211,7 +212,7 @@ export class TimeChart {
211
212
  this.intervalSpanLimit = config.intervalSpanLimit;
212
213
  this.axisLabelsStep = (_a = config.axisLabelsStep) !== null && _a !== void 0 ? _a : 1;
213
214
  this.scalingIndicator = (_b = config.scalingIndicator) !== null && _b !== void 0 ? _b : 0;
214
- this.totalColumn = (_c = config.totalColumn) !== null && _c !== void 0 ? _c : null;
215
+ this.totals = (_c = config.totals) !== null && _c !== void 0 ? _c : null;
215
216
  this.scenarioSwitch = (_d = config.scenarioSwitch) !== null && _d !== void 0 ? _d : false;
216
217
  this.axisTitlePosition = (_e = config.axisTitlePosition) !== null && _e !== void 0 ? _e : "left";
217
218
  this.dateInterval = this.findDateInterval(config.dateInterval);
@@ -263,13 +264,16 @@ export class TimeChart {
263
264
  this.highestColumnDataPointValue = this.highestDataPointValue;
264
265
  // the highest period variance: a break mark is for a variance of a period, the total has its own cut
265
266
  this.highestPeriodVarianceDataPointValue = this.highestVarianceDataPointValue;
266
- this.totalVariance = this.totalVariancePoint();
267
- if (this.totalVariance != null && Number.isFinite(this.totalVariance.value)) {
268
- if (this.totalVariance.value > Number(this.highestVarianceDataPointValue.value)) {
269
- this.highestVarianceDataPointValue = this.totalVariance;
267
+ this.totalColumns = this.computeTotalColumns();
268
+ this.totalVariances = this.computeTotalVariances();
269
+ for (const point of this.totalVariances) {
270
+ if (point == null || !Number.isFinite(point.value))
271
+ continue;
272
+ if (point.value > Number(this.highestVarianceDataPointValue.value)) {
273
+ this.highestVarianceDataPointValue = point;
270
274
  }
271
- if (this.totalVariance.value < Number(this.smallestVarianceDataPointValue.value)) {
272
- this.smallestVarianceDataPointValue = this.totalVariance;
275
+ if (point.value < Number(this.smallestVarianceDataPointValue.value)) {
276
+ this.smallestVarianceDataPointValue = point;
273
277
  }
274
278
  }
275
279
  if (this.hasScalingIndicator() && this.scalingIndicator > Number(this.highestDataPointValue.value)) {
@@ -278,7 +282,7 @@ export class TimeChart {
278
282
  this.highestDataPointValue = new DataPoint(this.scalingIndicator, scenario, this.pixelPerUnit, variance);
279
283
  }
280
284
  // the total column has its own scale: it counts for the height with the pixels it needs
281
- const totalHeight = this.isVarianceChart() ? 0 : this.totalColumnHeight();
285
+ const totalHeight = this.isVarianceChart() ? 0 : this.totalsHeight();
282
286
  if (totalHeight > this.highestDataPointValue.getScaledUnit() && this.pixelPerUnit > 0) {
283
287
  const { scenario, variance } = this.highestDataPointValue;
284
288
  this.highestDataPointValue = new DataPoint(totalHeight / this.pixelPerUnit, scenario, this.pixelPerUnit, variance);
@@ -398,8 +402,8 @@ export class TimeChart {
398
402
  }).width;
399
403
  indicatorEnd = this.scalingIndicatorLabelX() + textWidth + this.styles.padding.right;
400
404
  }
401
- const totalEnd = this.hasTotalColumn()
402
- ? this.totalColumnGeometry().right + this.totalColumnTextWidth() + this.styles.padding.right
405
+ const totalEnd = this.hasEndTotal()
406
+ ? this.totalGeometry(0).right + this.endTotalTextWidth() + this.styles.padding.right
403
407
  : 0;
404
408
  return Math.max(axisEnd + labelBuffer, bboxTotal, titleEnd, legendEnd, indicatorEnd, totalEnd);
405
409
  }
@@ -639,9 +643,9 @@ export class TimeChart {
639
643
  const points = this.variancesDataModel
640
644
  .filter((entry) => entry.type === VarianceType.RELATIVE)
641
645
  .map((entry) => entry.scenario1);
642
- if (points.length > 0 && this.totalVariance != null) {
643
- // the pin of the total column is one of the row
644
- points.push(this.totalVariance);
646
+ if (points.length > 0) {
647
+ // the pins of the total columns are pins of the row
648
+ points.push(...this.totalVariances.filter((point) => point != null));
645
649
  }
646
650
  if (points.length === 0 || !points.some((point) => point.isClipped())) {
647
651
  return;
@@ -742,7 +746,7 @@ export class TimeChart {
742
746
  if (this.isVarianceChart()) {
743
747
  this.drawVarianceScenario(this.variancesDataModel, svgContainer);
744
748
  // before the axis: the line of a scenario switch runs over the full height of the row
745
- this.drawTotalVariance(svgContainer);
749
+ this.drawTotalVariances(svgContainer);
746
750
  this.drawAxisAndLabels(this.variancesDataModel, svgContainer);
747
751
  }
748
752
  else if (this.stackConfig != null) {
@@ -769,7 +773,7 @@ export class TimeChart {
769
773
  this.drawOverlayLayer(this.dataModel, svgContainer);
770
774
  this.drawAxisAndLabels(this.dataModel, svgContainer);
771
775
  this.drawScalingIndicator(svgContainer);
772
- this.drawTotalColumn(svgContainer);
776
+ this.drawTotalColumns(svgContainer);
773
777
  }
774
778
  }
775
779
  }
@@ -799,7 +803,11 @@ export class TimeChart {
799
803
  scalingIndicatorLabelX() {
800
804
  const gap = this.position.spacings.labelToColumn;
801
805
  const half = this.position.getColumnWidth() / 2 + this.position.spacings.scenarioOffset;
802
- const right = this.position.getColumnCenterPositionX(Math.max(0, this.dataModel.length - 1)) + half;
806
+ let right = this.position.getColumnCenterPositionX(Math.max(0, this.dataModel.length - 1)) + half;
807
+ if (this.hasTotals() && !this.hasEndTotal()) {
808
+ // a total stands behind the last period: the value goes behind it
809
+ right = Math.max(right, this.totalGeometry(this.totalColumns.length - 1).right);
810
+ }
803
811
  const legendWidth = this.legendWidth("right");
804
812
  return right + gap + (legendWidth > 0 ? legendWidth + gap : 0);
805
813
  }
@@ -832,154 +840,448 @@ export class TimeChart {
832
840
  const { left, right, height } = band;
833
841
  const axisY = this.position.getColumnPositionY();
834
842
  const group = svgContainer.group().id("ScalingIndicator");
835
- group
836
- .rect(right - left, height)
837
- .x(left)
838
- .y(axisY - height)
839
- .fill(SCALING_INDICATOR_FILL);
843
+ // totals on the axis have their own band at their own scale: the band of the periods leaves their place free
844
+ const half = this.position.getColumnWidth() / 2 + this.position.spacings.scenarioOffset;
845
+ const runs = this.hasTotals() && !this.hasEndTotal()
846
+ ? this.totalRuns().map((run) => ({
847
+ left: this.position.getColumnCenterPositionX(run.from) - half,
848
+ right: this.position.getColumnCenterPositionX(run.to) + half,
849
+ }))
850
+ : [{ left, right }];
851
+ for (const run of runs) {
852
+ group
853
+ .rect(run.right - run.left, height)
854
+ .x(run.left)
855
+ .y(axisY - height)
856
+ .fill(SCALING_INDICATOR_FILL);
857
+ }
840
858
  group.back();
841
859
  new Label(svgContainer.group().id("ScalingIndicatorLabel"), this.scalingIndicatorLabelX(), axisY - height, "SCALING_INDICATOR", this.scalingIndicatorText(), this.styles, HookLocation.Center, HookLocation.Left).forceStringTypeDraw();
842
860
  }
843
- /** Where a title at the right starts when a total column stands there: behind the column and its texts. */
861
+ /** Where a title at the right starts when a total column stands at the end: behind the column and its texts. */
844
862
  rightTitleX() {
845
- if (!this.hasTotalColumn()) {
863
+ if (!this.hasEndTotal()) {
846
864
  return undefined;
847
865
  }
848
- return this.totalColumnGeometry().right + this.totalColumnTextWidth() + this.position.spacings.labelToColumn;
866
+ return this.totalGeometry(0).right + this.endTotalTextWidth() + this.position.spacings.labelToColumn;
867
+ }
868
+ /** True for a chart with total columns. */
869
+ hasTotals() {
870
+ return this.totalColumns.length > 0;
871
+ }
872
+ /** True for the one total column of `by: all`: it stands apart at the end, with its own axis and its names. */
873
+ hasEndTotal() {
874
+ return this.hasTotals() && this.totals.by === "all";
849
875
  }
850
- /** True for a chart with a total column: plain columns, no stack, no line. */
851
- hasTotalColumn() {
852
- return this.totalColumnParts() != null;
876
+ /** The index of a total column for the positions: the totals follow the periods. */
877
+ totalIndex(total) {
878
+ // the index behind the last period stays what it is without totals: the end of the overlay lines
879
+ return this.dataModel.length + 1 + total;
853
880
  }
854
- /** Place of the total column: right of the last period, with a gap. `left` and `right` are the ends of its axis. */
855
- totalColumnGeometry() {
881
+ /** Place of a total column. `left` and `right` are the ends of its band, and of its own axis at the end. */
882
+ totalGeometry(total) {
856
883
  var _a, _b;
857
- const columnWidth = this.position.getColumnWidth();
858
- const width = columnWidth * ((_b = (_a = this.totalColumn) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 1);
859
- // one period of room between the last period and the column: the axis of the periods ends before it
860
- const center = this.position.getColumnCenterPositionX(this.dataModel.length + 1) + (width - columnWidth) / 2;
884
+ const width = this.position.getColumnWidth() * ((_b = (_a = this.totals) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 1);
885
+ const center = this.position.getColumnCenterPositionX(this.totalIndex(total));
861
886
  const margin = this.position.spacings.scenarioOffset;
862
- // the value of the band of the periods stands in the gap: the column moves right by its width.
863
- // Every tier of the chart knows the indicator, so the column has the same place in all of them.
864
- const x = center + (this.scalingIndicator > 0 ? this.scalingIndicatorTextWidth() + margin : 0);
865
- return { center: x, width, left: x - width / 2 - 2 * margin, right: x + width / 2 + margin };
887
+ return { center, width, left: center - width / 2 - 2 * margin, right: center + width / 2 + margin };
866
888
  }
867
889
  /**
868
- * What the total column of the base chart shows: one part per scenario of the
869
- * series (the first scenario and the ones that follow it), from the sum of its
870
- * periods, and the sum of the first other scenario as the reference behind.
871
- * The variance rows read it too, for the same answer to "is there a total
872
- * column". Null without one, and when a part is not positive: a stack cannot
873
- * show a negative part, so such a chart has no total column at all.
890
+ * The places of the periods and of the total columns on the axis, in category
891
+ * widths, and the number of places the axis of the periods covers. A total of
892
+ * `by: all` stands one period behind the last one, with its own axis. The
893
+ * other totals stand right behind the last period of their group, and the
894
+ * periods after them move on.
874
895
  */
875
- totalColumnParts() {
876
- var _a;
877
- if (this.totalColumn == null ||
878
- this.stackConfig != null ||
879
- this.dataModel.length === 0 ||
880
- this.determineEffectiveChartMode() !== ChartMode.BAR) {
881
- return null;
896
+ totalSlots(columnWidth, categoryWidth, fontSize, scenarioOffset) {
897
+ var _a, _b, _c;
898
+ const count = this.dataModel.length;
899
+ const extra = categoryWidth > 0 ? ((((_b = (_a = this.totals) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 1) - 1) * columnWidth) / categoryWidth : 0;
900
+ const slots = [];
901
+ const totals = [];
902
+ if (this.totals.by === "all") {
903
+ for (let index = 0; index < count; index++)
904
+ slots.push(index);
905
+ // the value of the band of the periods stands in the gap: the column moves right by its width.
906
+ // Every tier of the chart knows the indicator, so the column has the same place in all of them.
907
+ const shift = this.scalingIndicator > 0 && categoryWidth > 0
908
+ ? (this.scalingIndicatorTextWidth() + scenarioOffset * fontSize) / (categoryWidth * fontSize)
909
+ : 0;
910
+ totals.push(count + 1 + extra / 2 + shift);
911
+ return { slots: [...slots, count, ...totals], axisColumns: count };
882
912
  }
913
+ // the names of the scenarios stand right of the last period: its total keeps one place of room for them.
914
+ // A series with a scenario switch has its names under the axis.
883
915
  const followers = this.followerSlots();
884
- const sums = new Map();
885
- for (const row of this.dataModel) {
886
- const slot = this.visibleSlot(row, followers);
887
- const point = slot ? row[slot] : null;
888
- if (slot == null || !Number.isFinite(point === null || point === void 0 ? void 0 : point.value))
889
- continue;
890
- const sum = (_a = sums.get(slot)) !== null && _a !== void 0 ? _a : { scenario: point.scenario, value: 0 };
891
- sum.value += point.value;
892
- sums.set(slot, sum);
916
+ const lastRow = this.dataModel[count - 1];
917
+ const lastIsFollower = followers.includes((_c = this.visibleSlot(lastRow, followers)) !== null && _c !== void 0 ? _c : "scenario1");
918
+ // the second marker prints its name there too
919
+ const markerName = !followers.includes("scenario4") && this.dataModel.some((row) => TimeChart.hasValue(row.scenario4));
920
+ const namesRoom = lastIsFollower && !markerName ? 0 : 1;
921
+ let offset = 0;
922
+ let next = 0;
923
+ for (let index = 0; index < count; index++) {
924
+ slots.push(index + offset);
925
+ const group = this.totalColumns[next];
926
+ if (group != null && group.to === index) {
927
+ const room = index === count - 1 ? namesRoom : 0;
928
+ totals.push(index + offset + room + 1 + extra / 2);
929
+ offset += room + 1 + extra;
930
+ next++;
931
+ }
932
+ }
933
+ return { slots: [...slots, count + offset, ...totals], axisColumns: count + offset };
934
+ }
935
+ /** The runs of periods between the total columns on the axis: each ends with the last period of a total, or of the chart. */
936
+ totalRuns() {
937
+ const runs = [];
938
+ let from = 0;
939
+ for (const column of this.totalColumns) {
940
+ runs.push({ from, to: column.to });
941
+ from = column.to + 1;
942
+ }
943
+ if (from < this.dataModel.length) {
944
+ runs.push({ from, to: this.dataModel.length - 1 });
893
945
  }
894
- const parts = [...sums.values()];
895
- if (parts.length === 0 || parts.some((part) => !(part.value > 0))) {
946
+ return runs;
947
+ }
948
+ /** The start of the group of a date, null for one group over all periods. */
949
+ totalGroupStart(date, period = false) {
950
+ var _a, _b;
951
+ const by = (_b = (_a = this.totals) === null || _a === void 0 ? void 0 : _a.by) !== null && _b !== void 0 ? _b : "all";
952
+ if (by === "all" || date == null || !date.isValid) {
896
953
  return null;
897
954
  }
898
- let reference = null;
955
+ // a week belongs to the year, quarter or month of its Thursday, as its number does
956
+ const day = period && this.dateInterval === DateDistance.WEEK && by !== "week" ? date.plus({ days: 3 }) : date;
957
+ return day.startOf(by);
958
+ }
959
+ /** The name of a total column under the axis: the text of the config at the end, else the name of its period. */
960
+ totalLabel(start) {
961
+ var _a, _b;
962
+ if (start == null) {
963
+ return (_b = (_a = this.totals) === null || _a === void 0 ? void 0 : _a.label) !== null && _b !== void 0 ? _b : "";
964
+ }
965
+ const interval = {
966
+ year: DateDistance.YEAR,
967
+ quarter: DateDistance.QUARTER,
968
+ month: DateDistance.MONTH,
969
+ week: DateDistance.WEEK,
970
+ }[this.totals.by];
971
+ return this.createDateFactory().getShortDateString(start, interval);
972
+ }
973
+ /** The scenarios of the series: the first one, and the ones that follow it. */
974
+ seriesSlotKeys() {
975
+ return ["scenario1", ...this.followerSlots()];
976
+ }
977
+ /** The slot of the reference of a total: the first scenario with values that is not part of the series. */
978
+ referenceSlot() {
979
+ const followers = this.followerSlots();
899
980
  for (const slot of ["scenario2", "scenario3", "scenario4"]) {
900
981
  if (followers.includes(slot))
901
982
  continue;
902
- const values = this.dataModel.map((row) => row[slot]).filter((point) => Number.isFinite(point === null || point === void 0 ? void 0 : point.value));
903
- if (values.length > 0) {
904
- reference = {
905
- scenario: values[0].scenario,
906
- value: values.reduce((sum, point) => sum + point.value, 0),
907
- };
908
- break;
983
+ if (this.dataModel.some((row) => { var _a; return Number.isFinite((_a = row[slot]) === null || _a === void 0 ? void 0 : _a.value); })) {
984
+ return slot;
909
985
  }
910
986
  }
911
- return {
912
- parts,
913
- height: parts.reduce((sum, part) => sum + part.value, 0),
914
- total: parts.reduce((sum, part) => sum + part.value, 0),
915
- reference,
916
- };
987
+ return null;
917
988
  }
918
- /** Height of the total column in pixels: its parts, its reference and its band of the scaling indicator. 0 without one. */
919
- totalColumnHeight() {
989
+ /** The look of one column for several scenarios: a forecast wins, then a plan, then the first one. */
990
+ mixedScenario(scenarios) {
920
991
  var _a, _b;
921
- const total = this.totalColumnParts();
922
- if (total == null) {
992
+ return ((_b = (_a = scenarios.find((scenario) => getScenarioPrefix(scenario) === "fc")) !== null && _a !== void 0 ? _a : scenarios.find((scenario) => getScenarioPrefix(scenario) === "pl")) !== null && _b !== void 0 ? _b : scenarios[0]);
993
+ }
994
+ /** The rows of the totals dataset, one per group, with the scenario columns added up. */
995
+ totalDatasetRows() {
996
+ var _a, _b, _c, _d;
997
+ const name = (_a = this.totals) === null || _a === void 0 ? void 0 : _a.dataset;
998
+ if (!name) {
999
+ return null;
1000
+ }
1001
+ const from = sourceExpr(this.db, name, "");
1002
+ const rows = new Map();
1003
+ if (!from) {
1004
+ return rows;
1005
+ }
1006
+ let result;
1007
+ try {
1008
+ result = this.db.exec(`SELECT * FROM ${from}`);
1009
+ }
1010
+ catch (e) {
1011
+ this.failQuery(e);
1012
+ return rows;
1013
+ }
1014
+ for (const row of result) {
1015
+ const date = TimeChart.parseDate(row.date);
1016
+ const start = date ? this.totalGroupStart(date) : null;
1017
+ const key = start ? ((_b = start.toISODate()) !== null && _b !== void 0 ? _b : "") : "";
1018
+ if (this.totals.by !== "all" && key === "")
1019
+ continue;
1020
+ const sums = (_c = rows.get(key)) !== null && _c !== void 0 ? _c : {};
1021
+ for (const [column, raw] of Object.entries(row)) {
1022
+ // a value can come as a text, like in the dataset of the chart
1023
+ const value = typeof raw === "string" && raw.trim() !== "" ? Number(raw) : raw;
1024
+ if (column !== "date" && typeof value === "number" && Number.isFinite(value)) {
1025
+ sums[column] = ((_d = sums[column]) !== null && _d !== void 0 ? _d : 0) + value;
1026
+ }
1027
+ }
1028
+ rows.set(key, sums);
1029
+ }
1030
+ return rows;
1031
+ }
1032
+ static parseDate(value) {
1033
+ if (value instanceof Date) {
1034
+ return DateTime.fromJSDate(value, { zone: "utc" });
1035
+ }
1036
+ if (typeof value === "string") {
1037
+ const date = DateTime.fromISO(value, { zone: "utc" });
1038
+ return date.isValid ? date : null;
1039
+ }
1040
+ if (typeof value === "number") {
1041
+ return DateTime.fromMillis(value, { zone: "utc" });
1042
+ }
1043
+ return null;
1044
+ }
1045
+ /**
1046
+ * The total columns of the chart. A group is the run of periods of one year,
1047
+ * quarter, month or week, or all periods. With `sum` a column stacks one part
1048
+ * per scenario of the series, else it is one column in the look of the
1049
+ * scenarios in it. The first other scenario is the reference behind. The
1050
+ * variance rows read the same list, for the same answer to "are there
1051
+ * totals". Empty without the config, with a stack, in line mode, and when a
1052
+ * part is not positive: a column cannot show that, so the chart has none.
1053
+ */
1054
+ computeTotalColumns() {
1055
+ var _a, _b, _c, _d, _e;
1056
+ if (this.totals == null ||
1057
+ this.stackConfig != null ||
1058
+ this.dataModel.length === 0 ||
1059
+ this.determineEffectiveChartMode() !== ChartMode.BAR) {
1060
+ return [];
1061
+ }
1062
+ const followers = this.followerSlots();
1063
+ const series = this.seriesSlotKeys();
1064
+ const referenceSlot = this.referenceSlot();
1065
+ const referenceScenario = referenceSlot
1066
+ ? (_a = this.dataModel.find((row) => { var _a; return ((_a = row[referenceSlot]) === null || _a === void 0 ? void 0 : _a.scenario) != null; })) === null || _a === void 0 ? void 0 : _a[referenceSlot].scenario
1067
+ : null;
1068
+ const fromData = this.totalDatasetRows();
1069
+ // the groups: runs of periods with the same start
1070
+ const groups = [];
1071
+ this.dataModel.forEach((row, index) => {
1072
+ const start = this.totalGroupStart(row.date, true);
1073
+ const last = groups[groups.length - 1];
1074
+ if (last != null && (start == null || (last.start != null && +last.start === +start))) {
1075
+ last.to = index;
1076
+ }
1077
+ else {
1078
+ groups.push({ from: index, to: index, start });
1079
+ }
1080
+ });
1081
+ // a total per period would repeat every column: `by` is not coarser than the periods of the chart
1082
+ if (this.totals.by !== "all" && groups.length === this.dataModel.length && groups.length > 1) {
1083
+ return [];
1084
+ }
1085
+ const columns = [];
1086
+ for (const group of groups) {
1087
+ const rows = this.dataModel.slice(group.from, group.to + 1);
1088
+ let parts = [];
1089
+ let reference = null;
1090
+ let counted = [];
1091
+ let scenarios;
1092
+ if (fromData != null) {
1093
+ // the values as the dataset gives them: one part per scenario of the series
1094
+ const data = (_c = fromData.get(group.start ? ((_b = group.start.toISODate()) !== null && _b !== void 0 ? _b : "") : "")) !== null && _c !== void 0 ? _c : {};
1095
+ for (const slot of series) {
1096
+ const scenario = (_d = this.dataModel.find((row) => { var _a; return ((_a = row[slot]) === null || _a === void 0 ? void 0 : _a.scenario) != null; })) === null || _d === void 0 ? void 0 : _d[slot].scenario;
1097
+ // a 0 is a scenario without a value in this total
1098
+ if (scenario != null && Number.isFinite(data[scenario]) && data[scenario] !== 0) {
1099
+ parts.push({ scenario, value: data[scenario] });
1100
+ }
1101
+ }
1102
+ if (referenceScenario != null && Number.isFinite(data[referenceScenario])) {
1103
+ reference = { scenario: referenceScenario, value: data[referenceScenario] };
1104
+ }
1105
+ scenarios = parts.map((part) => part.scenario);
1106
+ if (this.totals.aggregation !== "sum" && parts.length > 1 && parts.every((part) => part.value > 0)) {
1107
+ // an average or a last value is one column: the dataset gives the value, not its parts
1108
+ parts = [
1109
+ { scenario: this.mixedScenario(scenarios), value: parts.reduce((sum, part) => sum + part.value, 0) },
1110
+ ];
1111
+ }
1112
+ }
1113
+ else {
1114
+ // the periods where the series has a value. The reference counts in the same periods only,
1115
+ // as the variance of the total does: else the two would not tell the same.
1116
+ const used = rows.filter((row) => {
1117
+ var _a;
1118
+ const slot = this.visibleSlot(row, followers);
1119
+ return slot != null && Number.isFinite((_a = row[slot]) === null || _a === void 0 ? void 0 : _a.value);
1120
+ });
1121
+ counted = used.map((row) => this.dataModel.indexOf(row));
1122
+ scenarios = [...new Set(used.map((row) => row[this.visibleSlot(row, followers)].scenario))];
1123
+ const visible = used.map((row) => row[this.visibleSlot(row, followers)]);
1124
+ // with `last` the one period that gives the value counts. A reference with a gap in
1125
+ // the counted periods is no reference: half a sum would stand against a full one.
1126
+ const referenceRows = this.totals.aggregation === "last" ? used.slice(-1) : used;
1127
+ const found = referenceSlot
1128
+ ? referenceRows.map((row) => row[referenceSlot]).filter((point) => Number.isFinite(point === null || point === void 0 ? void 0 : point.value))
1129
+ : [];
1130
+ const references = found.length === referenceRows.length ? found : [];
1131
+ const sum = (points) => points.reduce((total, point) => total + point.value, 0);
1132
+ if (this.totals.aggregation === "sum") {
1133
+ const sums = new Map();
1134
+ for (const point of visible) {
1135
+ sums.set(point.scenario, ((_e = sums.get(point.scenario)) !== null && _e !== void 0 ? _e : 0) + point.value);
1136
+ }
1137
+ parts = [...sums.entries()].map(([scenario, value]) => ({ scenario, value }));
1138
+ if (references.length > 0) {
1139
+ reference = { scenario: referenceScenario, value: sum(references) };
1140
+ }
1141
+ }
1142
+ else if (visible.length > 0) {
1143
+ const last = this.totals.aggregation === "last";
1144
+ parts = [
1145
+ {
1146
+ scenario: last
1147
+ ? visible[visible.length - 1].scenario
1148
+ : this.mixedScenario(visible.map((point) => point.scenario)),
1149
+ value: last ? visible[visible.length - 1].value : sum(visible) / visible.length,
1150
+ },
1151
+ ];
1152
+ if (references.length > 0) {
1153
+ reference = {
1154
+ scenario: referenceScenario,
1155
+ value: last ? references[references.length - 1].value : sum(references) / references.length,
1156
+ };
1157
+ }
1158
+ }
1159
+ }
1160
+ if (parts.length === 0) {
1161
+ // a group without a value of the series has no total, the others keep theirs
1162
+ continue;
1163
+ }
1164
+ if (parts.some((part) => !(part.value > 0))) {
1165
+ return [];
1166
+ }
1167
+ const total = parts.reduce((sum, part) => sum + part.value, 0);
1168
+ columns.push(Object.assign(Object.assign({}, group), { label: this.totalLabel(group.start), parts, total, reference, counted, scenarios }));
1169
+ }
1170
+ return columns;
1171
+ }
1172
+ /** Height of the highest total column in pixels: its parts, its reference and its band of the scaling indicator. */
1173
+ totalsHeight() {
1174
+ if (!this.hasTotals()) {
923
1175
  return 0;
924
1176
  }
925
1177
  const band = this.hasScalingIndicator() ? this.scalingIndicator : 0;
926
- return Math.max(total.height, (_b = (_a = total.reference) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 0, band) * this.totalColumn.pixelPerUnit;
1178
+ const units = Math.max(...this.totalColumns.map((column) => { var _a, _b; return Math.max(column.total, (_b = (_a = column.reference) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 0); }));
1179
+ return Math.max(units, band) * this.totals.pixelPerUnit;
927
1180
  }
928
1181
  /**
929
- * The variance of the total column in a variance chart: the sum of the series
930
- * against the sum of the reference, over the periods where both have a value.
931
- * It takes the notation of the forecast when a forecast is part of the sum.
1182
+ * The variances of the total columns in a variance chart, one per column:
1183
+ * the series against the reference, put together the way the total is. With
1184
+ * a dataset the values come from its row. A total takes the notation of the
1185
+ * forecast when a forecast is part of it.
932
1186
  */
933
- totalVariancePoint() {
934
- var _a, _b, _c, _d, _e, _f;
935
- if (!this.hasTotalColumn() || !this.isVarianceChart()) {
936
- return null;
1187
+ computeTotalVariances() {
1188
+ var _a, _b;
1189
+ if (!this.hasTotals() || !this.isVarianceChart()) {
1190
+ return [];
937
1191
  }
938
1192
  const firstRow = (_a = this.dataModel[0]) !== null && _a !== void 0 ? _a : null;
939
1193
  const candidates = ((_b = this.configVariances) !== null && _b !== void 0 ? _b : [])
940
1194
  .map((name) => ({ name, variance: new VarianceScenario(name) }))
941
1195
  .filter((entry) => entry.variance.isValid);
942
1196
  if (candidates.length === 0) {
943
- return null;
1197
+ return [];
944
1198
  }
945
- let base = 0;
946
- let delta = 0;
947
- const notations = new Set();
948
- for (const row of this.dataModel) {
949
- for (const { variance } of candidates) {
950
- const slots = this.mapScenarios(variance, firstRow);
951
- const baseValue = (_d = row[(_c = slots.get(variance.base)) !== null && _c !== void 0 ? _c : ""]) === null || _d === void 0 ? void 0 : _d.value;
952
- const deltaValue = (_f = row[(_e = slots.get(variance.delta)) !== null && _e !== void 0 ? _e : ""]) === null || _f === void 0 ? void 0 : _f.value;
953
- if (Number.isFinite(baseValue) && Number.isFinite(deltaValue)) {
954
- base += baseValue;
955
- delta += deltaValue;
956
- notations.add(variance.base.slice(0, 2));
957
- break;
1199
+ const first = candidates[0].variance;
1200
+ const relative = first.type === VarianceType.RELATIVE;
1201
+ const fromData = this.totalDatasetRows();
1202
+ const aggregation = this.totals.aggregation;
1203
+ return this.totalColumns.map((column) => {
1204
+ var _a, _b, _c, _d, _e, _f;
1205
+ let base;
1206
+ let delta;
1207
+ const notations = new Set();
1208
+ if (fromData != null) {
1209
+ base = 0;
1210
+ const data = (_b = fromData.get(column.start ? ((_a = column.start.toISODate()) !== null && _a !== void 0 ? _a : "") : "")) !== null && _b !== void 0 ? _b : {};
1211
+ // the bases of the row are the scenarios of the series, each counts once
1212
+ const bases = new Set(candidates.map((entry) => entry.variance.base));
1213
+ // a row for a part of the series only has no total: the reference of the dataset is the one of the whole total
1214
+ const covered = column.scenarios.filter((scenario) => bases.has(scenario)).length;
1215
+ if (covered > 0 && covered < column.scenarios.length) {
1216
+ return null;
958
1217
  }
1218
+ for (const name of bases) {
1219
+ // a 0 is a scenario without a value in this total, as for the parts
1220
+ if (Number.isFinite(data[name]) && data[name] !== 0) {
1221
+ base += data[name];
1222
+ notations.add(name.slice(0, 2));
1223
+ }
1224
+ }
1225
+ if (notations.size === 0 || !Number.isFinite(data[first.delta])) {
1226
+ return null;
1227
+ }
1228
+ delta = data[first.delta];
959
1229
  }
1230
+ else {
1231
+ // the same periods as the column, so the two tell the same: with `last` the one
1232
+ // period of the value. Without both values in each of them there is no variance.
1233
+ const rows = (aggregation === "last" ? column.counted.slice(-1) : column.counted).map((index) => this.dataModel[index]);
1234
+ const pairs = [];
1235
+ for (const row of rows) {
1236
+ for (const { variance } of candidates) {
1237
+ const slots = this.mapScenarios(variance, firstRow);
1238
+ const baseValue = (_d = row[(_c = slots.get(variance.base)) !== null && _c !== void 0 ? _c : ""]) === null || _d === void 0 ? void 0 : _d.value;
1239
+ const deltaValue = (_f = row[(_e = slots.get(variance.delta)) !== null && _e !== void 0 ? _e : ""]) === null || _f === void 0 ? void 0 : _f.value;
1240
+ if (Number.isFinite(baseValue) && Number.isFinite(deltaValue)) {
1241
+ pairs.push({ base: baseValue, delta: deltaValue, notation: variance.base.slice(0, 2) });
1242
+ break;
1243
+ }
1244
+ }
1245
+ }
1246
+ if (pairs.length === 0 || pairs.length !== rows.length) {
1247
+ return null;
1248
+ }
1249
+ const divisor = aggregation === "average" ? pairs.length : 1;
1250
+ base = pairs.reduce((sum, pair) => sum + pair.base, 0) / divisor;
1251
+ delta = pairs.reduce((sum, pair) => sum + pair.delta, 0) / divisor;
1252
+ pairs.forEach((pair) => notations.add(pair.notation));
1253
+ }
1254
+ const point = new DataPoint(relative ? relativeVariance(base - delta, delta) : base - delta, first.delta, relative ? this.pixelPerPercentage : this.pixelPerUnit, first.direction);
1255
+ if (this.scenarioSwitch) {
1256
+ point.notation = notations.has("fc") ? "fc" : notations.size === 1 ? [...notations][0] : "ac";
1257
+ }
1258
+ point.key = this.totalKey(column, candidates[0].name);
1259
+ return point;
1260
+ });
1261
+ }
1262
+ /** The point key of an element of a total column: `row: total`, and the start of its group when there are several. */
1263
+ totalKey(column, scenario) {
1264
+ const key = { row: "total" };
1265
+ if (column.start != null) {
1266
+ key.date = TimeChart.pointDate(column.start);
960
1267
  }
961
- if (notations.size === 0) {
962
- return null;
963
- }
964
- const first = candidates[0].variance;
965
- const relative = first.type === VarianceType.RELATIVE;
966
- const point = new DataPoint(relative ? relativeVariance(base - delta, delta) : base - delta, first.delta, relative ? this.pixelPerPercentage : this.pixelPerUnit, first.direction);
967
- if (this.scenarioSwitch) {
968
- point.notation = notations.has("fc") ? "fc" : notations.size === 1 ? [...notations][0] : "ac";
1268
+ if (scenario) {
1269
+ key.scenario = scenario;
969
1270
  }
970
- point.key = { row: "total", scenario: candidates[0].name };
971
- return point;
1271
+ return key;
972
1272
  }
973
- /** Width of the texts right of the total column: the names of its parts, and the value of the scaling indicator. */
974
- totalColumnTextWidth() {
1273
+ /** Width of the texts right of the total column at the end: the names of its parts, and the value of its band. */
1274
+ endTotalTextWidth() {
1275
+ if (!this.hasEndTotal()) {
1276
+ return 0;
1277
+ }
975
1278
  const width = (text) => CalculateSize(text, { font: this.styles.fontStyles.font, fontSize: this.styles.fontStyles.size + "px" }).width;
976
1279
  const gap = this.position.spacings.labelToColumn;
977
- const total = this.totalColumnParts();
978
- let names = 0;
979
- if (total != null && total.parts.length > 1) {
980
- names = gap + Math.max(...total.parts.map((part) => width(this.scenarioName(part.scenario))));
981
- }
982
- const indicator = total != null && this.hasScalingIndicator() ? gap + width(this.scalingIndicatorText()) : 0;
1280
+ const parts = this.totalColumns[0].parts;
1281
+ const names = !this.isVarianceChart() && parts.length > 1
1282
+ ? gap + Math.max(...parts.map((part) => width(this.scenarioName(part.scenario))))
1283
+ : 0;
1284
+ const indicator = this.hasScalingIndicator() ? gap + width(this.scalingIndicatorText()) : 0;
983
1285
  return names + indicator;
984
1286
  }
985
1287
  scenarioName(scenario) {
@@ -1000,131 +1302,137 @@ export class TimeChart {
1000
1302
  solid: pattern ? null : color,
1001
1303
  };
1002
1304
  }
1003
- /** The axis of the total column: its own piece of line, in the look of the axis of the chart. */
1305
+ /** The axis of the total column at the end: its own piece of line, in the look of the axis of the chart. */
1004
1306
  drawTotalAxis(group, y) {
1005
- const { left, right } = this.totalColumnGeometry();
1307
+ const { left, right } = this.totalGeometry(0);
1006
1308
  const scenario = this.axisScenario == null ? "default" : `${this.axisScenario}`.slice(0, 2);
1007
1309
  group.line(left, y, right, y).addClass("axis-" + scenario);
1008
1310
  }
1009
1311
  /**
1010
- * The total column of the base chart: a stack of the parts at the scale of the
1011
- * column, the reference behind it, the total above and the names of the parts
1012
- * at the right. With a scaling indicator the column has its own band, and a
1013
- * line links it to the band of the periods.
1312
+ * The total columns of the base chart, at their own scale: the parts, the
1313
+ * reference behind, the total above and the name under the axis. With a
1314
+ * scaling indicator a column has its own band. The column at the end has its
1315
+ * own axis and the names of its parts at the right, and a line links its band
1316
+ * to the band of the periods.
1014
1317
  */
1015
- drawTotalColumn(svgContainer) {
1016
- const total = this.isVarianceChart() ? null : this.totalColumnParts();
1017
- if (total == null) {
1318
+ drawTotalColumns(svgContainer) {
1319
+ if (!this.hasTotals() || this.isVarianceChart()) {
1018
1320
  return;
1019
1321
  }
1020
- const { center, width, left, right } = this.totalColumnGeometry();
1021
- const scale = this.totalColumn.pixelPerUnit;
1322
+ const scale = this.totals.pixelPerUnit;
1022
1323
  const axisY = this.position.getColumnPositionY();
1023
1324
  const gap = this.position.spacings.labelToColumn;
1325
+ const atEnd = this.hasEndTotal();
1024
1326
  const group = svgContainer.group().id("TotalColumn");
1025
- if (this.hasScalingIndicator()) {
1026
- const height = this.scalingIndicator * scale;
1027
- const periods = this.scalingIndicatorBand();
1028
- group
1029
- .rect(right - left, height)
1030
- .x(left)
1031
- .y(axisY - height)
1032
- .fill(SCALING_INDICATOR_FILL);
1033
- if (periods != null) {
1034
- // the line starts behind the value of the band of the periods
1035
- const from = this.scalingIndicatorLabelX() + this.scalingIndicatorTextWidth() + gap / 2;
1327
+ const labelGroup = svgContainer.group().id("TotalColumnLabels");
1328
+ this.totalColumns.forEach((column, index) => {
1329
+ const { center, width, left, right } = this.totalGeometry(index);
1330
+ if (this.hasScalingIndicator()) {
1331
+ const height = this.scalingIndicator * scale;
1332
+ const periods = this.scalingIndicatorBand();
1333
+ const band = group
1334
+ .rect(right - left, height)
1335
+ .x(left)
1336
+ .y(axisY - height)
1337
+ .fill(SCALING_INDICATOR_FILL);
1338
+ // behind the columns of the periods that stand next to it
1339
+ band.back();
1340
+ if (atEnd && periods != null) {
1341
+ // the line starts behind the value of the band of the periods
1342
+ const from = this.scalingIndicatorLabelX() + this.scalingIndicatorTextWidth() + gap / 2;
1343
+ group
1344
+ .line(Math.min(from, left), axisY - periods.height, left, axisY - height)
1345
+ .stroke({ color: SCALING_INDICATOR_LINE, width: 1 })
1346
+ .id("ScalingIndicatorLink");
1347
+ }
1348
+ }
1349
+ if (column.reference != null && column.reference.value > 0) {
1350
+ const look = this.columnLook(column.reference.scenario);
1351
+ const height = column.reference.value * scale;
1036
1352
  group
1037
- .line(Math.min(from, left), axisY - periods.height, left, axisY - height)
1038
- .stroke({ color: SCALING_INDICATOR_LINE, width: 1 })
1039
- .id("ScalingIndicatorLink");
1353
+ .rect(width, height)
1354
+ .cx(center - this.position.spacings.scenarioOffset)
1355
+ .y(axisY - height)
1356
+ .fill(look.fill)
1357
+ .stroke(look.stroke)
1358
+ .attr(dataPointAttributes(this.totalKey(column, column.reference.scenario), "element"))
1359
+ .attr("data-bn-value", column.reference.value);
1040
1360
  }
1041
- }
1042
- if (total.reference != null && total.reference.value > 0) {
1043
- const look = this.columnLook(total.reference.scenario);
1044
- const height = total.reference.value * scale;
1045
- group
1046
- .rect(width, height)
1047
- .cx(center - this.position.spacings.scenarioOffset)
1048
- .y(axisY - height)
1049
- .fill(look.fill)
1050
- .stroke(look.stroke)
1051
- .attr(dataPointAttributes({ row: "total", scenario: total.reference.scenario }, "element"))
1052
- .attr("data-bn-value", total.reference.value);
1053
- }
1054
- let top = axisY;
1055
- const labelGroup = svgContainer.group().id("TotalColumnLabels");
1056
- for (const part of total.parts) {
1057
- const look = this.columnLook(part.scenario);
1058
- const height = part.value * scale;
1059
- top -= height;
1060
- const key = { row: "total", scenario: part.scenario };
1061
- group
1062
- .rect(width, height)
1063
- .cx(center)
1064
- .y(top)
1065
- .fill(look.fill)
1066
- .stroke(look.stroke)
1067
- .attr(dataPointAttributes(key, "element"))
1068
- .attr("data-bn-value", part.value);
1069
- if (total.parts.length > 1) {
1070
- if (height >= this.position.dataLabelHeight) {
1071
- const label = new Label(labelGroup, center, top + height / 2, "TOTAL_PART", part.value, this.styles, HookLocation.Center, HookLocation.Center).setPoint(key);
1072
- // a label on a hatching keeps the background of a plain label
1073
- (look.solid ? label.setSegmentFill(look.solid) : label).draw(true, this.dataFormatDigits, this.measureScale);
1361
+ let top = axisY;
1362
+ for (const part of column.parts) {
1363
+ const look = this.columnLook(part.scenario);
1364
+ const height = part.value * scale;
1365
+ top -= height;
1366
+ const key = this.totalKey(column, part.scenario);
1367
+ group
1368
+ .rect(width, height)
1369
+ .cx(center)
1370
+ .y(top)
1371
+ .fill(look.fill)
1372
+ .stroke(look.stroke)
1373
+ .attr(dataPointAttributes(key, "element"))
1374
+ .attr("data-bn-value", part.value);
1375
+ if (column.parts.length > 1) {
1376
+ if (height >= this.position.dataLabelHeight) {
1377
+ const label = new Label(labelGroup, center, top + height / 2, "TOTAL_PART", part.value, this.styles, HookLocation.Center, HookLocation.Center).setPoint(key);
1378
+ // a label on a hatching keeps the background of a plain label
1379
+ (look.solid ? label.setSegmentFill(look.solid) : label).draw(true, this.dataFormatDigits, this.measureScale);
1380
+ }
1381
+ if (atEnd) {
1382
+ new Label(labelGroup, center + width / 2 + gap,
1383
+ // the name of a thin top part stays below the total above the column
1384
+ Math.max(top + height / 2, axisY - column.total * scale + this.position.dataLabelHeight / 2), "TOTAL_NAME", this.scenarioName(part.scenario), this.styles, HookLocation.Center, HookLocation.Left).forceStringTypeDraw();
1385
+ }
1074
1386
  }
1075
- new Label(labelGroup, center + width / 2 + gap,
1076
- // the name of a thin top part stays below the total above the column
1077
- Math.max(top + height / 2, axisY - total.height * scale + this.position.dataLabelHeight / 2), "TOTAL_NAME", this.scenarioName(part.scenario), this.styles, HookLocation.Center, HookLocation.Left).forceStringTypeDraw();
1078
1387
  }
1079
- }
1080
- new Label(labelGroup, center, top - gap, "TOTAL", total.total, this.styles, HookLocation.Bottom, HookLocation.Center)
1081
- .setPoint({ row: "total" }, "total")
1082
- .draw(true, this.dataFormatDigits, this.measureScale);
1083
- this.drawTotalAxis(group, this.position.getAxisPositionY(false));
1084
- if (this.hasScalingIndicator()) {
1085
- new Label(labelGroup, right +
1086
- this.totalColumnTextWidth() -
1087
- CalculateSize(this.scalingIndicatorText(), {
1088
- font: this.styles.fontStyles.font,
1089
- fontSize: this.styles.fontStyles.size + "px",
1090
- }).width, axisY - this.scalingIndicator * scale, "SCALING_INDICATOR_TOTAL", this.scalingIndicatorText(), this.styles, HookLocation.Center, HookLocation.Left).forceStringTypeDraw();
1091
- }
1092
- if (this.hideAxisLabels === false && this.totalColumn.label) {
1093
- new Label(labelGroup, center, this.position.getAxisPositionY(false) + this.position.spacings.labelToAxis, "TOTAL_LABEL", this.totalColumn.label, this.styles, HookLocation.Top, HookLocation.Center).forceStringTypeDraw();
1388
+ new Label(labelGroup, center, top - gap, "TOTAL", column.total, this.styles, HookLocation.Bottom, HookLocation.Center)
1389
+ .setPoint(this.totalKey(column), "total")
1390
+ .draw(true, this.dataFormatDigits, this.measureScale);
1391
+ if (this.hideAxisLabels === false && column.label) {
1392
+ new Label(labelGroup, center, this.position.getAxisPositionY(false) + this.position.spacings.labelToAxis, "TOTAL_LABEL", column.label, this.styles, HookLocation.Top, HookLocation.Center).forceStringTypeDraw();
1393
+ }
1394
+ });
1395
+ if (atEnd) {
1396
+ this.drawTotalAxis(group, this.position.getAxisPositionY(false));
1397
+ if (this.hasScalingIndicator()) {
1398
+ new Label(labelGroup, this.totalGeometry(0).right + this.endTotalTextWidth() - this.scalingIndicatorTextWidth(), axisY - this.scalingIndicator * scale, "SCALING_INDICATOR_TOTAL", this.scalingIndicatorText(), this.styles, HookLocation.Center, HookLocation.Left).forceStringTypeDraw();
1399
+ }
1094
1400
  }
1095
1401
  }
1096
- /** The variance of the total column, in the tier of its variance chart: a column or a pin like the others. */
1097
- drawTotalVariance(svgContainer) {
1402
+ /** The variances of the total columns, in the tier of their variance chart: columns or pins like the others. */
1403
+ drawTotalVariances(svgContainer) {
1098
1404
  var _a;
1099
- const point = this.totalVariance;
1100
- if (point == null) {
1405
+ if (!this.totalVariances.some((point) => point != null)) {
1101
1406
  return;
1102
1407
  }
1103
1408
  // inside the group of the variances: its box is the height of the row, also below the axis
1104
1409
  const scenarios = (_a = svgContainer.findOne("#Scenarios")) !== null && _a !== void 0 ? _a : svgContainer.group().id("Scenarios");
1105
1410
  const group = scenarios.group().id("TotalColumn");
1106
1411
  const labelGroup = scenarios.group().id("TotalColumnLabels");
1107
- const { center } = this.totalColumnGeometry();
1108
- // the elements take their place from an index: the one of the centre of the total column
1109
- const index = (center - this.position.getColumnCenterPositionX(0)) /
1110
- (this.position.getColumnCenterPositionX(1) - this.position.getColumnCenterPositionX(0));
1111
1412
  const relative = this.variancesDataModel.some((entry) => entry.type === VarianceType.RELATIVE);
1112
- if (relative) {
1113
- new Needle(this.position, this.scenarioStyles(), point, index, group.group())
1114
- .updateLabelGroup(labelGroup.group())
1115
- .draw(this.dataFormatDigits);
1116
- }
1117
- else {
1413
+ this.totalVariances.forEach((point, total) => {
1414
+ if (point == null) {
1415
+ return;
1416
+ }
1417
+ const index = this.totalIndex(total);
1418
+ if (relative) {
1419
+ new Needle(this.position, this.scenarioStyles(), point, index, group.group())
1420
+ .updateLabelGroup(labelGroup.group())
1421
+ .draw(this.dataFormatDigits);
1422
+ return;
1423
+ }
1118
1424
  // a row whose period variances are cut cuts the total the same way. The part above the
1119
- // break is scaled by the largest overflow: that is the one of the total here.
1425
+ // break is scaled by the largest overflow: that can be the one of the total.
1120
1426
  const overflow = this.overflowConfig
1121
1427
  ? Object.assign(Object.assign({}, this.overflowConfig), { maxOverflow: Math.max(this.overflowConfig.maxOverflow, point.getScaledUnit() - (this.overflowConfig.cutHeight - this.overflowConfig.scenarioCutOffset)) }) : null;
1122
1428
  new Column(this.position, this.scenarioStyles(), point, index, true, group.group(), null, overflow)
1123
1429
  .updateLabelGroup(labelGroup.group())
1124
1430
  .setAsVariance()
1125
1431
  .draw(this.dataFormatDigits, this.measureScale);
1432
+ });
1433
+ if (this.hasEndTotal()) {
1434
+ this.drawTotalAxis(group, this.position.getAxisPositionY(relative));
1126
1435
  }
1127
- this.drawTotalAxis(group, this.position.getAxisPositionY(relative));
1128
1436
  const box = scenarios.bbox();
1129
1437
  this.scenariosArea = {
1130
1438
  height: box.height + this.styles.padding.top + this.styles.padding.bottom,
@@ -1201,6 +1509,7 @@ export class TimeChart {
1201
1509
  return new Map([...defaultI18n, ...i18n]);
1202
1510
  }
1203
1511
  createElementPosition(scenarios) {
1512
+ var _a, _b;
1204
1513
  let maxLabelLengthLeft;
1205
1514
  // only need scenarios written on the leftside of bar/ marker
1206
1515
  const scenarioLabelLeft = CalculateSize(scenarios
@@ -1244,7 +1553,7 @@ export class TimeChart {
1244
1553
  if (this.overflowConfig) {
1245
1554
  const { cutHeight, overdriveZoneHeight, breakGap } = this.overflowConfig;
1246
1555
  // the total column is not cut: it keeps its room above the cut columns
1247
- const virtualHeight = Math.max(cutHeight + overdriveZoneHeight + breakGap, this.isVarianceChart() ? 0 : this.totalColumnHeight());
1556
+ const virtualHeight = Math.max(cutHeight + overdriveZoneHeight + breakGap, this.isVarianceChart() ? 0 : this.totalsHeight());
1248
1557
  // Create a virtual DataPoint with pixelPerUnit=1 so getScaledUnit() returns virtualHeight directly
1249
1558
  max = new DataPoint(virtualHeight, max.scenario, 1, max.variance);
1250
1559
  }
@@ -1275,7 +1584,13 @@ export class TimeChart {
1275
1584
  effectiveScaleFactors = cloned;
1276
1585
  }
1277
1586
  }
1278
- return new ElementPositions(new Padding(this.styles.padding), effectiveScaleFactors, this.styles.fontStyles, min, max, this.dataModel.length, maxLabelLengthLeft);
1587
+ // total columns take places on the axis: the periods behind them move on
1588
+ const places = this.hasTotals()
1589
+ ? this.totalSlots(effectiveScaleFactors.columnWidth, effectiveScaleFactors.categoryWidth, this.styles.fontStyles.size, effectiveScaleFactors.scenarioOffset)
1590
+ : null;
1591
+ const position = new ElementPositions(new Padding(this.styles.padding), effectiveScaleFactors, this.styles.fontStyles, min, max, (_a = places === null || places === void 0 ? void 0 : places.axisColumns) !== null && _a !== void 0 ? _a : this.dataModel.length, maxLabelLengthLeft);
1592
+ position.setSlots((_b = places === null || places === void 0 ? void 0 : places.slots) !== null && _b !== void 0 ? _b : null);
1593
+ return position;
1279
1594
  }
1280
1595
  calculateTitleLength(minWidth) {
1281
1596
  let tmpTitle = "_______"; // default width if this.title is sorther
@@ -1813,7 +2128,8 @@ export class TimeChart {
1813
2128
  const bottom = namesHeight > 0 ? height - this.styles.padding.bottom : height;
1814
2129
  const lineWidth = Math.max(1, this.position.spacings.auxiliaryLineWidth);
1815
2130
  runs.slice(1).forEach((run) => {
1816
- const x = (this.position.getColumnCenterPositionX(run.from - 1) + this.position.getColumnCenterPositionX(run.from)) / 2;
2131
+ // half a period before the first period of the run: a total column can stand between the two runs
2132
+ const x = this.position.getColumnCenterPositionX(run.from) - this.position.spacings.categoryWidth / 2;
1817
2133
  group
1818
2134
  .line(x, 0, x, bottom)
1819
2135
  .stroke({ color: this.styles.fontStyles.color || "#000000", width: lineWidth })