@bino-bi/bn-template-engine 1.0.0-next.50 → 1.0.0-next.52
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.
- package/dist/bn-template-engine/bn-template-engine.esm.js +1 -1
- package/dist/bn-template-engine/{p-2de524d0.entry.js → p-150bdb68.entry.js} +1 -1
- package/dist/bn-template-engine/p-4d99f1bb.entry.js +1 -0
- package/dist/bn-template-engine/{p-Bxa4pX1M.js → p-Bvunba5p.js} +1 -1
- package/dist/bn-template-engine/p-NxJ6P79y.js +1 -0
- package/dist/bn-template-engine/{p-8fc90dc8.entry.js → p-b75a4b52.entry.js} +1 -1
- package/dist/bn-template-engine/{p-510d2ee6.entry.js → p-fdf42a77.entry.js} +1 -1
- package/dist/cjs/bn-annotation-layer_30.cjs.entry.js +1114 -63
- package/dist/cjs/bn-grid.cjs.entry.js +1 -1
- package/dist/cjs/bn-image.cjs.entry.js +1 -1
- package/dist/cjs/bn-template-engine.cjs.js +1 -1
- package/dist/cjs/bn-text.cjs.entry.js +2 -2
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/{withStyled-pkdctrK9.js → withStyled-BISQLGLe.js} +9 -0
- package/dist/cjs/{withTranslation-BIzTNYZM.js → withTranslation-B7T30EVU.js} +1 -1
- package/dist/collection/components/bn-annotation-layer/bn-annotation-layer.js +109 -11
- package/dist/collection/components/bn-annotation-layer/utils/marks.js +126 -4
- package/dist/collection/components/bn-annotation-layer/utils/matchPoint.js +2 -1
- package/dist/collection/components/bn-chart-structure/bn-chart-structure-renderer/bn-chart-structure-renderer.js +133 -0
- package/dist/collection/components/bn-chart-structure/bn-chart-structure.js +258 -2
- package/dist/collection/components/bn-chart-structure/utils/StructureChart.js +549 -27
- package/dist/collection/components/bn-chart-structure/utils/elements/Bar.js +9 -1
- package/dist/collection/components/bn-chart-structure/utils/elements/Label.js +12 -0
- package/dist/collection/components/bn-chart-structure/utils/elements/Needle.js +6 -0
- package/dist/collection/components/bn-chart-structure/utils/elements/StackedBar.js +23 -8
- package/dist/collection/components/bn-chart-structure/utils/helper/ElementPositions.js +22 -3
- package/dist/collection/components/bn-chart-structure/utils/model/StructureChartComponentConfig.js +23 -0
- package/dist/collection/components/bn-context/bn-context.js +68 -1
- package/dist/collection/components/bn-layout-card/bn-layout-card.js +62 -2
- package/dist/collection/utils/formatter.js +13 -2
- package/dist/collection/utils/validate.js +9 -0
- package/dist/components/bn-annotation-layer.js +1 -1
- package/dist/components/bn-chart-bubble-renderer.js +1 -1
- package/dist/components/bn-chart-bubble.js +1 -1
- package/dist/components/bn-chart-bullet-renderer.js +1 -1
- package/dist/components/bn-chart-bullet.js +1 -1
- package/dist/components/bn-chart-scatter-renderer.js +1 -1
- package/dist/components/bn-chart-scatter.js +1 -1
- package/dist/components/bn-chart-structure-renderer.js +1 -1
- package/dist/components/bn-chart-structure.js +1 -1
- package/dist/components/bn-chart-time-renderer.js +1 -1
- package/dist/components/bn-chart-time.js +1 -1
- package/dist/components/bn-context.js +1 -1
- package/dist/components/bn-grid.js +1 -1
- package/dist/components/bn-image.js +1 -1
- package/dist/components/bn-layout-card.js +1 -1
- package/dist/components/bn-layout-page.js +1 -1
- package/dist/components/bn-page.js +1 -1
- package/dist/components/bn-table-renderer.js +1 -1
- package/dist/components/bn-table.js +1 -1
- package/dist/components/bn-text.js +1 -1
- package/dist/components/bn-title-renderer.js +1 -1
- package/dist/components/bn-title.js +1 -1
- package/dist/components/bn-tree.js +1 -1
- package/dist/components/p-BOwmXBm-.js +1 -0
- package/dist/components/p-Bs5V6Ibu.js +1 -0
- package/dist/components/{p-CVfmhtEW.js → p-CU9CdEBF.js} +1 -1
- package/dist/components/{p-Cc0gaLwp.js → p-CZSp0rSS.js} +1 -1
- package/dist/components/p-CcfvGlBq.js +1 -0
- package/dist/components/p-CiljAoC5.js +1 -0
- package/dist/components/p-D0v9GMIf.js +1 -0
- package/dist/components/{p-DBj1Yd3M.js → p-D65TnLS9.js} +1 -1
- package/dist/components/p-DKcw-1ot.js +1 -0
- package/dist/components/{p-k7heRDPP.js → p-DLmQ_mi7.js} +1 -1
- package/dist/components/p-DOKqRsAu.js +1 -0
- package/dist/components/p-LHIKI9Dp.js +1 -0
- package/dist/components/{p-Blre9f28.js → p-NdDdfez6.js} +1 -1
- package/dist/components/p-Zz3fAPcH.js +1 -0
- package/dist/components/{p-y12CmIFS.js → p-dSUX43yf.js} +1 -1
- package/dist/components/p-iJP2X4dc.js +1 -0
- package/dist/components/p-jEqo8YD5.js +1 -0
- package/dist/esm/bn-annotation-layer_30.entry.js +1114 -63
- package/dist/esm/bn-grid.entry.js +1 -1
- package/dist/esm/bn-image.entry.js +1 -1
- package/dist/esm/bn-template-engine.js +1 -1
- package/dist/esm/bn-text.entry.js +2 -2
- package/dist/esm/loader.js +1 -1
- package/dist/esm/{withStyled-B62R20Zx.js → withStyled-CrTxIZ0Q.js} +9 -0
- package/dist/esm/{withTranslation-BEseMLQ6.js → withTranslation-BtSJmx1J.js} +1 -1
- package/dist/schemas/stack.schema.json +21 -1
- package/dist/types/components/bn-annotation-layer/utils/marks.d.ts +53 -0
- package/dist/types/components/bn-annotation-layer/utils/matchPoint.d.ts +2 -0
- package/dist/types/components/bn-chart-structure/bn-chart-structure-renderer/bn-chart-structure-renderer.d.ts +11 -1
- package/dist/types/components/bn-chart-structure/bn-chart-structure.d.ts +49 -0
- package/dist/types/components/bn-chart-structure/utils/StructureChart.d.ts +52 -0
- package/dist/types/components/bn-chart-structure/utils/elements/Bar.d.ts +2 -0
- package/dist/types/components/bn-chart-structure/utils/elements/Label.d.ts +4 -0
- package/dist/types/components/bn-chart-structure/utils/elements/Needle.d.ts +2 -0
- package/dist/types/components/bn-chart-structure/utils/elements/StackedBar.d.ts +13 -1
- package/dist/types/components/bn-chart-structure/utils/helper/ElementPositions.d.ts +15 -1
- package/dist/types/components/bn-chart-structure/utils/model/StructureChartComponentConfig.d.ts +23 -0
- package/dist/types/components/bn-context/bn-context.d.ts +12 -0
- package/dist/types/components/bn-layout-card/bn-layout-card.d.ts +12 -0
- package/dist/types/components.d.ts +113 -2
- package/dist/types/schemas.d.ts +7 -1
- package/dist/types/utils/annotations.d.ts +1 -1
- package/dist/types/utils/formatter.d.ts +5 -0
- package/package.json +1 -1
- package/dist/bn-template-engine/p-BFl7OwcC.js +0 -1
- package/dist/bn-template-engine/p-c1d679af.entry.js +0 -1
- package/dist/components/p-B11cXOLy.js +0 -1
- package/dist/components/p-BVNOmHh4.js +0 -1
- package/dist/components/p-CHhxQoF9.js +0 -1
- package/dist/components/p-CO6bgquz.js +0 -1
- package/dist/components/p-CmvpJzLd.js +0 -1
- package/dist/components/p-CuUfiEu0.js +0 -1
- package/dist/components/p-DHloMyxV.js +0 -1
- package/dist/components/p-Dd1QOWDV.js +0 -1
- package/dist/components/p-HVN8BNWN.js +0 -1
- 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,17 +138,15 @@ 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
|
-
|
|
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);
|
|
132
148
|
this.smallestVarianceDataPointValue = this.findSmallestDataPointValue(this.variancesDataModel);
|
|
133
|
-
this.highestDataPointValue = this.findHighestDataPointValue(
|
|
149
|
+
this.highestDataPointValue = this.findHighestDataPointValue(bars);
|
|
134
150
|
this.highestVarianceDataPointValue = this.findHighestDataPointValue(this.variancesDataModel);
|
|
135
151
|
this.axisScenario = this.setAxisScenario(config.variances);
|
|
136
152
|
this.measureScale = config.measureScale;
|
|
@@ -247,7 +263,7 @@ export class StructureChart {
|
|
|
247
263
|
* from variable width components.
|
|
248
264
|
*/
|
|
249
265
|
getUnitBarExtent() {
|
|
250
|
-
var _a, _b;
|
|
266
|
+
var _a, _b, _c, _d;
|
|
251
267
|
const stacked = this.isStacked();
|
|
252
268
|
const vt = this.getVarianceType();
|
|
253
269
|
const isVariance = vt !== VarianceType.NONE;
|
|
@@ -257,8 +273,9 @@ export class StructureChart {
|
|
|
257
273
|
// 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
274
|
fixedDomain: stacked ? this.stackSides.positive + this.stackSides.negative : undefined,
|
|
259
275
|
relativeVariance: vt === VarianceType.RELATIVE,
|
|
260
|
-
|
|
261
|
-
|
|
276
|
+
// a reference line right of the longest bar needs its room too
|
|
277
|
+
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),
|
|
278
|
+
minNegative: (_d = smallest === null || smallest === void 0 ? void 0 : smallest.value) !== null && _d !== void 0 ? _d : 0,
|
|
262
279
|
});
|
|
263
280
|
}
|
|
264
281
|
/**
|
|
@@ -292,6 +309,8 @@ export class StructureChart {
|
|
|
292
309
|
return (this.position.getAxisEndPositionY() +
|
|
293
310
|
this.position.spacings.labelToAxis +
|
|
294
311
|
this.position.fontSize +
|
|
312
|
+
// the 100% mark stands below the axis end
|
|
313
|
+
(this.hasPercentMark() ? this.position.fontSize * 0.8 : 0) +
|
|
295
314
|
this.styles.padding.bottom);
|
|
296
315
|
}
|
|
297
316
|
patternToString() {
|
|
@@ -353,11 +372,15 @@ export class StructureChart {
|
|
|
353
372
|
this.drawStackedScenarios(this.dataModel, svgContainer);
|
|
354
373
|
this.drawStackedHeaders(this.dataModel, svgContainer);
|
|
355
374
|
this.drawAxisAndLabels(this.dataModel, svgContainer, true);
|
|
375
|
+
this.drawPercentMark(svgContainer);
|
|
356
376
|
}
|
|
357
377
|
else {
|
|
358
378
|
this.drawScenarios(this.dataModel, svgContainer);
|
|
359
379
|
this.drawAxisAndLabels(this.dataModel, svgContainer);
|
|
360
380
|
}
|
|
381
|
+
if (this.variancesDataModel.length === 0) {
|
|
382
|
+
this.drawReferenceLine(svgContainer);
|
|
383
|
+
}
|
|
361
384
|
}
|
|
362
385
|
setPattern(fc, fcPolygonPattern) {
|
|
363
386
|
this.styles.pattern.set(fc, fcPolygonPattern);
|
|
@@ -474,7 +497,7 @@ export class StructureChart {
|
|
|
474
497
|
};
|
|
475
498
|
return obj;
|
|
476
499
|
}, {
|
|
477
|
-
id:
|
|
500
|
+
id: REST_ROW_ID,
|
|
478
501
|
category: "REST",
|
|
479
502
|
type: VarianceType.NONE,
|
|
480
503
|
scenario1: new DataPoint(NaN, selectedScenarios[0], this.pixelPerUnit, Scenario.NEU),
|
|
@@ -661,7 +684,7 @@ export class StructureChart {
|
|
|
661
684
|
// a 100 % stack prints no total
|
|
662
685
|
const totalLabelRoom = (layout) => {
|
|
663
686
|
var _a;
|
|
664
|
-
return ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE
|
|
687
|
+
return ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE && this.stackConfig.labels !== "value"
|
|
665
688
|
? 0
|
|
666
689
|
: (labelToBar + this.calculateTotalLabelWidth(layout.total)) / this.pixelPerUnit;
|
|
667
690
|
};
|
|
@@ -669,6 +692,8 @@ export class StructureChart {
|
|
|
669
692
|
}
|
|
670
693
|
/** Layouts of the stacks of a row that are drawn, back to front. */
|
|
671
694
|
drawnStackLayouts(row) {
|
|
695
|
+
if (this.isTextSum(row))
|
|
696
|
+
return [];
|
|
672
697
|
return this.stackGroups(row)
|
|
673
698
|
.filter((group) => group.hasValue)
|
|
674
699
|
.map((group) => group.layout);
|
|
@@ -716,6 +741,7 @@ export class StructureChart {
|
|
|
716
741
|
scenario2: new DataPoint(null, null, null, null),
|
|
717
742
|
scenario3: new DataPoint(null, null, null, null),
|
|
718
743
|
scenario4: new DataPoint(null, null, null, null),
|
|
744
|
+
sum: dataModel[index].sum,
|
|
719
745
|
});
|
|
720
746
|
}
|
|
721
747
|
}
|
|
@@ -787,6 +813,7 @@ export class StructureChart {
|
|
|
787
813
|
let isRelative = false;
|
|
788
814
|
if (!this.hideCategories) {
|
|
789
815
|
dataModel.forEach((elements, index) => {
|
|
816
|
+
var _a;
|
|
790
817
|
if (elements["type"] === VarianceType.RELATIVE) {
|
|
791
818
|
isRelative = true;
|
|
792
819
|
}
|
|
@@ -808,9 +835,23 @@ export class StructureChart {
|
|
|
808
835
|
new Label(axisLabelGroup, this.position.getCategoryPositionX(isPositive) + clearance, this.position.getBarCenterPositionY(index), "",
|
|
809
836
|
// Category labels come from the data, but the synthetic "REST"
|
|
810
837
|
// overflow bucket is engine-generated and translatable, as in bn-table.
|
|
811
|
-
this.i18n.get(elements.category) || elements.category, this.styles, HookLocation.Center, textHook)
|
|
838
|
+
this.i18n.get(elements.category) || elements.category, this.styles, HookLocation.Center, textHook)
|
|
839
|
+
.setEmphasis(!!((_a = this.dataModel[index]) === null || _a === void 0 ? void 0 : _a.sum), this.isItalic(elements.category))
|
|
840
|
+
.draw();
|
|
812
841
|
});
|
|
813
842
|
}
|
|
843
|
+
// a thin line above a sum row, from the labels to the end of the row
|
|
844
|
+
dataModel.forEach((_elements, index) => {
|
|
845
|
+
const row = this.dataModel[index];
|
|
846
|
+
if (!(row === null || row === void 0 ? void 0 : row.sum))
|
|
847
|
+
return;
|
|
848
|
+
const y = this.position.getBarCenterPositionY(index) - this.position.spacings.categoryWidth / 2;
|
|
849
|
+
const left = this.hideCategories ? this.position.getAxisPositionX() : this.styles.padding.left;
|
|
850
|
+
axisLabelGroup
|
|
851
|
+
.line(left, y, this.position.getAxisPositionX() + this.sumRowExtent(row), y)
|
|
852
|
+
.stroke({ color: "#000000", width: Math.max(0.5, this.position.spacings.axisLineWidth / 2) })
|
|
853
|
+
.addClass("sum-line");
|
|
854
|
+
});
|
|
814
855
|
new Axis({
|
|
815
856
|
scenarioType: this.axisScenario,
|
|
816
857
|
labelText: this.title,
|
|
@@ -819,25 +860,111 @@ export class StructureChart {
|
|
|
819
860
|
}
|
|
820
861
|
drawScenarios(dataModel, svgContainer) {
|
|
821
862
|
const barsGroup = svgContainer.group().id("Scenarios");
|
|
822
|
-
|
|
863
|
+
// the name of the scenario stands below the last bar, or above the first one
|
|
864
|
+
let lastBar = -1;
|
|
865
|
+
dataModel.forEach((row, index) => {
|
|
866
|
+
if (!row.sum)
|
|
867
|
+
lastBar = index;
|
|
868
|
+
});
|
|
869
|
+
const scenarioFactory = new ScenarioFactory(this.position, barsGroup, this.scenarioStyles(),
|
|
870
|
+
// the factory names the last bar. The row layout leaves room for the name when a sum row follows.
|
|
871
|
+
this.scenarioLabelTop ? 0 : lastBar + 1, this.i18n);
|
|
823
872
|
scenarioFactory.setOverflowConfig(this.overflowConfig);
|
|
824
873
|
dataModel.forEach((elements, index) => {
|
|
874
|
+
if (elements.sum) {
|
|
875
|
+
this.drawSumValue(elements, index, barsGroup);
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
825
878
|
scenarioFactory.createPrimaryBar(elements["scenario1"], index).draw(this.dataFormatDigits, this.measureScale);
|
|
826
879
|
scenarioFactory.createSecondaryBar(elements["scenario2"], index).draw();
|
|
827
880
|
scenarioFactory.createPrimaryMarker(elements["scenario3"], index).draw();
|
|
828
881
|
scenarioFactory.createSecondaryMarker(elements["scenario4"], index).draw();
|
|
829
882
|
});
|
|
883
|
+
const first = dataModel.find((row) => !row.sum);
|
|
884
|
+
if (this.scenarioLabelTop && first && !first.scenario1.isEmpty() && first.scenario1.scenario != null) {
|
|
885
|
+
const scenario = first.scenario1.scenario;
|
|
886
|
+
new Label(barsGroup,
|
|
887
|
+
// from the axis on: the first bar can be shorter than the name
|
|
888
|
+
this.position.getBarPositionX() + this.position.spacings.labelToAxis, this.position.getStackedHeaderPositionY(), "SCENARIO_HEADER", this.i18n.get(scenario) || scenario, this.styles, HookLocation.Bottom, HookLocation.Left).draw();
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
/** The scenarios of a sum row of a chart with plain bars that have a value, in the order of the `scenarios` prop. */
|
|
892
|
+
sumPoints(row) {
|
|
893
|
+
const points = [row.scenario1, row.scenario2, row.scenario3, row.scenario4].filter((point) => point != null && !point.isEmpty() && Number.isFinite(point.value));
|
|
894
|
+
const order = this.scenarioPropOrder;
|
|
895
|
+
const place = (point) => {
|
|
896
|
+
const index = order.indexOf(point.scenario);
|
|
897
|
+
return index === -1 ? order.length : index;
|
|
898
|
+
};
|
|
899
|
+
return points.sort((a, b) => place(a) - place(b));
|
|
900
|
+
}
|
|
901
|
+
/** How wide the numbers of a sum row of a chart with plain bars are, from the axis on. */
|
|
902
|
+
sumValuesWidth(row) {
|
|
903
|
+
const font = this.styles.fontStyles.size;
|
|
904
|
+
const labelToBar = font * this.styles.scaleFactors.labelToBar;
|
|
905
|
+
const points = this.sumPoints(row);
|
|
906
|
+
if (points.length === 0)
|
|
907
|
+
return 0;
|
|
908
|
+
const numbers = points.map((point) => this.boldWidth(this.sumText(point.value)));
|
|
909
|
+
if (points.length === 1)
|
|
910
|
+
return labelToBar + numbers[0];
|
|
911
|
+
return labelToBar + numbers.reduce((sum, width) => sum + font * 0.9 + width, 0) + (points.length - 1) * font * 1.4;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* The sum row of a chart with plain bars: bold numbers at the axis, no bar. One
|
|
915
|
+
* scenario is a plain number. Several get a small square each, in the look of the scenario.
|
|
916
|
+
*/
|
|
917
|
+
drawSumValue(row, index, group) {
|
|
918
|
+
const points = this.sumPoints(row);
|
|
919
|
+
if (points.length === 0)
|
|
920
|
+
return;
|
|
921
|
+
const font = this.styles.fontStyles.size;
|
|
922
|
+
const y = this.position.getBarCenterPositionY(index);
|
|
923
|
+
const sumGroup = group.group().id(`sum_${index}`);
|
|
924
|
+
const number = (point, x, hook) => {
|
|
925
|
+
new Label(sumGroup, x, y, `SUM_${point.scenario}`, point.value, this.styles, HookLocation.Center, hook)
|
|
926
|
+
.setEmphasis(true, false)
|
|
927
|
+
.setPoint(point.key)
|
|
928
|
+
.draw(this.dataFormatDigits, this.measureScale);
|
|
929
|
+
};
|
|
930
|
+
if (points.length === 1) {
|
|
931
|
+
// like the label of a bar without length: a negative number stands left of the axis
|
|
932
|
+
const negative = points[0].value < 0;
|
|
933
|
+
number(points[0], this.position.getLabelPositionX(negative ? -0.000001 : 0), negative ? HookLocation.Right : HookLocation.Left);
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
const key = font * 0.6;
|
|
937
|
+
const barStyles = this.scenarioStyles().barStyles;
|
|
938
|
+
let x = this.position.getLabelPositionX(0);
|
|
939
|
+
for (const point of points) {
|
|
940
|
+
const style = barStyles.getSVGStyle(point.getScenario());
|
|
941
|
+
sumGroup
|
|
942
|
+
.rect(key, key)
|
|
943
|
+
.fill({ color: (style === null || style === void 0 ? void 0 : style.fill) || "#000000" })
|
|
944
|
+
.stroke({ color: (style === null || style === void 0 ? void 0 : style.stroke) || "#000000", width: 1 })
|
|
945
|
+
.cy(y)
|
|
946
|
+
.x(x);
|
|
947
|
+
number(point, x + key + font * 0.3, HookLocation.Left);
|
|
948
|
+
x += font * 0.9 + this.boldWidth(this.sumText(point.value)) + font * 1.4;
|
|
949
|
+
}
|
|
830
950
|
}
|
|
831
951
|
drawVarianceScenario(dataModel, svgContainer) {
|
|
832
952
|
const barsGroup = svgContainer.group().id("Scenarios");
|
|
833
953
|
const scenarioFactory = new ScenarioFactory(this.position, barsGroup, this.scenarioStyles(), dataModel.length, this.i18n);
|
|
834
954
|
scenarioFactory.setOverflowConfig(this.overflowConfig);
|
|
835
955
|
dataModel.forEach((elements, index) => {
|
|
956
|
+
// the variance of a sum row is a bar like the others, with a bold number
|
|
836
957
|
if (elements["type"] === VarianceType.RELATIVE) {
|
|
837
|
-
scenarioFactory.createRelativeVarianceBar(elements["scenario1"], index)
|
|
958
|
+
const needle = scenarioFactory.createRelativeVarianceBar(elements["scenario1"], index);
|
|
959
|
+
if (elements.sum)
|
|
960
|
+
needle.emphasize();
|
|
961
|
+
needle.draw(this.dataFormatDigits);
|
|
838
962
|
}
|
|
839
963
|
else {
|
|
840
|
-
scenarioFactory.createVarianceBar(elements["scenario1"], index)
|
|
964
|
+
const bar = scenarioFactory.createVarianceBar(elements["scenario1"], index);
|
|
965
|
+
if (elements.sum)
|
|
966
|
+
bar.emphasize();
|
|
967
|
+
bar.draw(this.dataFormatDigits, this.measureScale);
|
|
841
968
|
}
|
|
842
969
|
});
|
|
843
970
|
return barsGroup;
|
|
@@ -865,14 +992,27 @@ export class StructureChart {
|
|
|
865
992
|
return;
|
|
866
993
|
const stackGroup = svgContainer.group().id("StackedScenarios");
|
|
867
994
|
const isScenarioStack = this.stackConfig.by === StackBy.SCENARIOS;
|
|
995
|
+
const relative = this.stackConfig.mode === StackMode.RELATIVE;
|
|
996
|
+
const valueLabels = relative && this.stackConfig.labels === "value";
|
|
997
|
+
const label = valueLabels ? "value" : this.stackConfig.percentMark ? "plain-share" : "share";
|
|
868
998
|
dataModel.forEach((elements, index) => {
|
|
869
999
|
const groups = this.stackGroups(elements);
|
|
1000
|
+
if (this.isTextSum(elements)) {
|
|
1001
|
+
this.drawSumText(elements, index, stackGroup);
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
const options = {
|
|
1005
|
+
label,
|
|
1006
|
+
total: valueLabels,
|
|
1007
|
+
bold: !!elements.sum,
|
|
1008
|
+
italic: this.isItalic(elements.category),
|
|
1009
|
+
};
|
|
870
1010
|
groups.forEach((group, groupIdx) => {
|
|
871
1011
|
let lastBar = null;
|
|
872
1012
|
group.segments.forEach((seg, segIdx) => {
|
|
873
1013
|
const bar = new StackedBar(this.position, this.styles, seg.dataPoint, index, group.layout.segments[segIdx], seg.scenario,
|
|
874
1014
|
// 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);
|
|
1015
|
+
groups.length > 1 ? groups.length - 1 - groupIdx : segIdx, this.stackConfig.mode, group.layout, isScenarioStack, stackGroup, options);
|
|
876
1016
|
bar.draw(this.dataFormatDigits, this.measureScale);
|
|
877
1017
|
lastBar = bar;
|
|
878
1018
|
});
|
|
@@ -890,9 +1030,10 @@ export class StructureChart {
|
|
|
890
1030
|
* The headers take their place from the first row.
|
|
891
1031
|
*/
|
|
892
1032
|
drawStackedHeaders(dataModel, svgContainer) {
|
|
1033
|
+
var _a;
|
|
893
1034
|
if (!this.stackConfig || dataModel.length === 0)
|
|
894
1035
|
return;
|
|
895
|
-
const firstRow = dataModel[0];
|
|
1036
|
+
const firstRow = (_a = dataModel.find((row) => !this.isTextSum(row))) !== null && _a !== void 0 ? _a : dataModel[0];
|
|
896
1037
|
// Dimension stacking without segments draws one plain bar: nothing to name
|
|
897
1038
|
if (this.stackConfig.by !== StackBy.SCENARIOS && (!firstRow.stackSegments || firstRow.stackSegments.length === 0)) {
|
|
898
1039
|
return;
|
|
@@ -917,6 +1058,11 @@ export class StructureChart {
|
|
|
917
1058
|
}
|
|
918
1059
|
// Single group: one header per segment, centered on the segment
|
|
919
1060
|
const [group] = groups;
|
|
1061
|
+
const totalDrawn = this.stackConfig.mode !== StackMode.RELATIVE || this.stackConfig.labels === "value";
|
|
1062
|
+
if (this.stackConfig.totalTitle && totalDrawn) {
|
|
1063
|
+
// above the total of the first bar, which starts behind the bar
|
|
1064
|
+
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();
|
|
1065
|
+
}
|
|
920
1066
|
group.segments.forEach((seg, segIdx) => {
|
|
921
1067
|
const { start, end } = group.layout.segments[segIdx];
|
|
922
1068
|
drawHeader(axisX + ((start + end) / 2) * seg.dataPoint.pixelPerUnit, this.i18n.get(seg.scenario) || seg.scenario);
|
|
@@ -969,7 +1115,7 @@ export class StructureChart {
|
|
|
969
1115
|
return width;
|
|
970
1116
|
}
|
|
971
1117
|
calculateRightSideWidth(varianceType) {
|
|
972
|
-
var _a, _b, _c;
|
|
1118
|
+
var _a, _b, _c, _d, _e, _f;
|
|
973
1119
|
// Stacked charts: width based on the positive side of the stacks, not individual max
|
|
974
1120
|
if (this.isStacked()) {
|
|
975
1121
|
const labelToBar = this.styles.fontStyles.size * this.styles.scaleFactors.labelToBar;
|
|
@@ -981,28 +1127,42 @@ export class StructureChart {
|
|
|
981
1127
|
this.position.getCategoryToAxisSpaceing() +
|
|
982
1128
|
this.calculateCategoryWidth());
|
|
983
1129
|
}
|
|
984
|
-
|
|
1130
|
+
if ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.totalTitle) {
|
|
1131
|
+
const title = CalculateSize(this.stackConfig.totalTitle, {
|
|
1132
|
+
font: this.styles.fontStyles.font,
|
|
1133
|
+
fontSize: this.styles.fontStyles.size + "px",
|
|
1134
|
+
}).width;
|
|
1135
|
+
width = Math.max(width, this.stackSides.positive * this.pixelPerUnit + labelToBar + title);
|
|
1136
|
+
}
|
|
1137
|
+
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
1138
|
}
|
|
986
1139
|
let width = 0;
|
|
987
1140
|
const labelToBar = this.styles.fontStyles.size * this.styles.scaleFactors.labelToBar;
|
|
988
1141
|
if (VarianceType.NONE === varianceType &&
|
|
989
|
-
((
|
|
1142
|
+
((_d = this.highestDataPointValue) === null || _d === void 0 ? void 0 : _d.value) != null &&
|
|
990
1143
|
this.highestDataPointValue.value >= 0) {
|
|
991
1144
|
const sizeLabelValue = this.calculateTextBoxWidth(this.highestDataPointValue.value);
|
|
992
1145
|
width = Math.abs(this.highestDataPointValue.getScaledUnit()) + labelToBar + sizeLabelValue;
|
|
993
1146
|
}
|
|
994
1147
|
else if (VarianceType.RELATIVE === varianceType &&
|
|
995
|
-
((
|
|
1148
|
+
((_e = this.highestVarianceDataPointValue) === null || _e === void 0 ? void 0 : _e.value) != null &&
|
|
996
1149
|
this.highestVarianceDataPointValue.value >= 0) {
|
|
997
1150
|
const sizeLabelValue = this.calculatePercentageTextBoxWidth(this.highestVarianceDataPointValue.value);
|
|
998
1151
|
width = Math.abs(this.highestVarianceDataPointValue.getScaledPercentUnit()) + labelToBar + sizeLabelValue;
|
|
999
1152
|
}
|
|
1000
1153
|
else if (VarianceType.ABSOLUTE === varianceType &&
|
|
1001
|
-
((
|
|
1154
|
+
((_f = this.highestVarianceDataPointValue) === null || _f === void 0 ? void 0 : _f.value) != null &&
|
|
1002
1155
|
this.highestVarianceDataPointValue.value >= 0) {
|
|
1003
1156
|
const sizeLabelValue = this.calculateTextBoxWidth(this.highestVarianceDataPointValue.value);
|
|
1004
1157
|
width = Math.abs(this.highestVarianceDataPointValue.getScaledUnit()) + labelToBar + sizeLabelValue;
|
|
1005
1158
|
}
|
|
1159
|
+
width = Math.max(width, this.referenceLineExtent());
|
|
1160
|
+
// the bold number of a sum row stands at the axis
|
|
1161
|
+
for (const row of this.dataModel) {
|
|
1162
|
+
if (row.sum && VarianceType.NONE === varianceType) {
|
|
1163
|
+
width = Math.max(width, this.sumRowExtent(row));
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1006
1166
|
if (width <= this.titleLength) {
|
|
1007
1167
|
return this.titleLength;
|
|
1008
1168
|
}
|
|
@@ -1013,6 +1173,7 @@ export class StructureChart {
|
|
|
1013
1173
|
* at the stack dimension level and attach stackSegments to each dataModel row.
|
|
1014
1174
|
*/
|
|
1015
1175
|
populateDimensionStackSegments(scenarios, _stackConfig, level) {
|
|
1176
|
+
var _a;
|
|
1016
1177
|
if (this.rowCount === 0 || !this.fromExpr || scenarios.length === 0)
|
|
1017
1178
|
return;
|
|
1018
1179
|
// Derive stack column from level: stack by the next-lower hierarchy dimension
|
|
@@ -1031,6 +1192,7 @@ export class StructureChart {
|
|
|
1031
1192
|
let result;
|
|
1032
1193
|
try {
|
|
1033
1194
|
result = this.db.exec(`SELECT [${displayColumn}] as displayGroup, [${stackColumn}] as stackLabel, ` +
|
|
1195
|
+
`MIN([${stackColumn}Index]) as stackIndex, ` +
|
|
1034
1196
|
`SUM([${scenario}] * transformOperator(operation)) as stackValue ` +
|
|
1035
1197
|
`FROM ${this.fromExpr} GROUP BY [${displayColumn}], [${stackColumn}]`);
|
|
1036
1198
|
}
|
|
@@ -1038,19 +1200,46 @@ export class StructureChart {
|
|
|
1038
1200
|
this.failQuery(e);
|
|
1039
1201
|
return;
|
|
1040
1202
|
}
|
|
1203
|
+
// The series of the chart: by the index column of the stack dimension, else in the order of the rows
|
|
1204
|
+
const series = new Map();
|
|
1205
|
+
for (const row of result) {
|
|
1206
|
+
if (row.stackValue == null || row.stackLabel == null)
|
|
1207
|
+
continue;
|
|
1208
|
+
const label = row.stackLabel;
|
|
1209
|
+
const index = Number.isFinite(Number(row.stackIndex)) && row.stackIndex != null ? Number(row.stackIndex) : NaN;
|
|
1210
|
+
const known = series.get(label);
|
|
1211
|
+
if (known === undefined || (Number.isNaN(known) && !Number.isNaN(index)) || index < known) {
|
|
1212
|
+
series.set(label, index);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
const order = [...series.keys()];
|
|
1216
|
+
if ([...series.values()].every((index) => !Number.isNaN(index))) {
|
|
1217
|
+
order.sort((a, b) => series.get(a) - series.get(b));
|
|
1218
|
+
}
|
|
1041
1219
|
// Group results by displayGroup
|
|
1042
1220
|
const groupMap = new Map();
|
|
1043
1221
|
for (const row of result) {
|
|
1044
1222
|
const key = row.displayGroup;
|
|
1045
1223
|
if (!groupMap.has(key))
|
|
1046
|
-
groupMap.set(key,
|
|
1224
|
+
groupMap.set(key, new Map());
|
|
1047
1225
|
if (row.stackValue != null && row.stackLabel != null) {
|
|
1048
|
-
groupMap.get(key).
|
|
1226
|
+
groupMap.get(key).set(row.stackLabel, row.stackValue);
|
|
1049
1227
|
}
|
|
1050
1228
|
}
|
|
1051
|
-
//
|
|
1229
|
+
// the REST row stands for the rows the limit cut off: it gets their sum per series
|
|
1230
|
+
const shown = new Set(this.dataModel.map((row) => row.category));
|
|
1231
|
+
const rest = new Map();
|
|
1232
|
+
for (const [category, values] of groupMap) {
|
|
1233
|
+
if (shown.has(category))
|
|
1234
|
+
continue;
|
|
1235
|
+
for (const [label, value] of values) {
|
|
1236
|
+
rest.set(label, ((_a = rest.get(label)) !== null && _a !== void 0 ? _a : 0) + value);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
// Attach stackSegments to each dataModel row: every row has every series, so place and colour stay
|
|
1052
1240
|
for (const row of this.dataModel) {
|
|
1053
|
-
const
|
|
1241
|
+
const values = row.id === REST_ROW_ID && !groupMap.has(row.category) ? rest : groupMap.get(row.category);
|
|
1242
|
+
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
1243
|
if (segments && segments.length > 0) {
|
|
1055
1244
|
row.stackSegments = segments.map((seg) => {
|
|
1056
1245
|
const dataPoint = new DataPoint(seg.value, scenario, this.pixelPerUnit, Scenario.NEU);
|
|
@@ -1063,6 +1252,339 @@ export class StructureChart {
|
|
|
1063
1252
|
}
|
|
1064
1253
|
}
|
|
1065
1254
|
}
|
|
1255
|
+
/** A sum row that is printed as numbers: every stack but a 100 % stack, where it is a bar of shares. */
|
|
1256
|
+
isTextSum(row) {
|
|
1257
|
+
return !!row.sum && !!this.stackConfig && this.stackConfig.mode !== StackMode.RELATIVE;
|
|
1258
|
+
}
|
|
1259
|
+
isItalic(category) {
|
|
1260
|
+
return this.italicCategories.includes(category);
|
|
1261
|
+
}
|
|
1262
|
+
hasPercentMark() {
|
|
1263
|
+
var _a;
|
|
1264
|
+
return (!!((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.percentMark) &&
|
|
1265
|
+
this.stackConfig.mode === StackMode.RELATIVE &&
|
|
1266
|
+
this.variancesDataModel.length === 0 &&
|
|
1267
|
+
this.isStacked());
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Adds the sum rows: one below the rows of each `rowGroup`, which keeps the rows of a
|
|
1271
|
+
* group together, and one over all rows at the end.
|
|
1272
|
+
*/
|
|
1273
|
+
addSumRows(scenarios) {
|
|
1274
|
+
if (this.sums === "none" || this.dataModel.length === 0)
|
|
1275
|
+
return;
|
|
1276
|
+
// a cell without a value is left out, and a sum over no value stays NaN
|
|
1277
|
+
const add = (sum, value) => Number.isFinite(value) ? (Number.isFinite(sum) ? sum : 0) + value : sum;
|
|
1278
|
+
const sumRow = (members, category, sum) => {
|
|
1279
|
+
var _a, _b;
|
|
1280
|
+
const point = (key, index) => new DataPoint(scenarios[index] === undefined
|
|
1281
|
+
? undefined
|
|
1282
|
+
: members.reduce((total, row) => add(total, row[key].value), NaN), scenarios[index], this.pixelPerUnit, Scenario.NEU);
|
|
1283
|
+
const row = {
|
|
1284
|
+
id: 10000 + this.dataModel.length,
|
|
1285
|
+
category,
|
|
1286
|
+
type: VarianceType.NONE,
|
|
1287
|
+
scenario1: point("scenario1", 0),
|
|
1288
|
+
scenario2: point("scenario2", 1),
|
|
1289
|
+
scenario3: point("scenario3", 2),
|
|
1290
|
+
scenario4: point("scenario4", 3),
|
|
1291
|
+
sum,
|
|
1292
|
+
};
|
|
1293
|
+
if (members.some((member) => member.stackSegments)) {
|
|
1294
|
+
const values = new Map();
|
|
1295
|
+
for (const member of members) {
|
|
1296
|
+
for (const segment of (_a = member.stackSegments) !== null && _a !== void 0 ? _a : []) {
|
|
1297
|
+
values.set(segment.label, add((_b = values.get(segment.label)) !== null && _b !== void 0 ? _b : NaN, segment.value));
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
row.stackSegments = [...values].map(([label, value]) => {
|
|
1301
|
+
const dataPoint = new DataPoint(value, scenarios[0], this.pixelPerUnit, Scenario.NEU);
|
|
1302
|
+
return { label, value, dataPoint };
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
return row;
|
|
1306
|
+
};
|
|
1307
|
+
const rows = this.dataModel;
|
|
1308
|
+
const result = [];
|
|
1309
|
+
const groups = this.sums === "total" ? new Map() : this.rowGroups();
|
|
1310
|
+
if (groups.size > 0) {
|
|
1311
|
+
const names = [...new Set([...groups.values()].sort((a, b) => a.index - b.index).map((group) => group.name))];
|
|
1312
|
+
for (const name of names) {
|
|
1313
|
+
const members = rows.filter((row) => { var _a; return ((_a = groups.get(row.category)) === null || _a === void 0 ? void 0 : _a.name) === name; });
|
|
1314
|
+
if (members.length === 0)
|
|
1315
|
+
continue;
|
|
1316
|
+
const sum = sumRow(members, name, "group");
|
|
1317
|
+
sum.gapAfter = true;
|
|
1318
|
+
result.push(...members, sum);
|
|
1319
|
+
}
|
|
1320
|
+
// a row without a group, like REST, comes last
|
|
1321
|
+
result.push(...rows.filter((row) => !groups.has(row.category)));
|
|
1322
|
+
}
|
|
1323
|
+
else {
|
|
1324
|
+
result.push(...rows);
|
|
1325
|
+
}
|
|
1326
|
+
if (this.sums === "total" || this.sums === "all") {
|
|
1327
|
+
result.push(sumRow(rows, this.sumTitle || "SUM_TOTAL", "total"));
|
|
1328
|
+
}
|
|
1329
|
+
this.dataModel = result;
|
|
1330
|
+
}
|
|
1331
|
+
/** The `rowGroup` of each row of a chart at level `category`, with the place of the group. */
|
|
1332
|
+
rowGroups() {
|
|
1333
|
+
const groups = new Map();
|
|
1334
|
+
if (this.level !== AggregationLevel.CATEGORY && this.level !== AggregationLevel.CATEGORYINDEX)
|
|
1335
|
+
return groups;
|
|
1336
|
+
let result;
|
|
1337
|
+
try {
|
|
1338
|
+
result = this.db.exec(`SELECT [category] as category, [rowGroup] as rowGroup, MIN([rowGroupIndex]) as rowGroupIndex ` +
|
|
1339
|
+
`FROM ${this.fromExpr} GROUP BY [category], [rowGroup]`);
|
|
1340
|
+
}
|
|
1341
|
+
catch (e) {
|
|
1342
|
+
this.failQuery(e);
|
|
1343
|
+
return groups;
|
|
1344
|
+
}
|
|
1345
|
+
const first = new Map();
|
|
1346
|
+
result.forEach((row, position) => {
|
|
1347
|
+
var _a;
|
|
1348
|
+
if (row.rowGroup == null || row.rowGroup === "")
|
|
1349
|
+
return;
|
|
1350
|
+
const name = String(row.rowGroup);
|
|
1351
|
+
const given = Number(row.rowGroupIndex);
|
|
1352
|
+
// without an index column the groups keep the order of the rows
|
|
1353
|
+
const index = row.rowGroupIndex != null && Number.isFinite(given) ? given : ((_a = first.get(name)) !== null && _a !== void 0 ? _a : position);
|
|
1354
|
+
if (!first.has(name))
|
|
1355
|
+
first.set(name, index);
|
|
1356
|
+
groups.set(String(row.category), { name, index: Math.min(index, first.get(name)) });
|
|
1357
|
+
});
|
|
1358
|
+
return groups;
|
|
1359
|
+
}
|
|
1360
|
+
/** Rows of different height (`stack.thickness: total`) and the room below a group. Null: all rows are equal. */
|
|
1361
|
+
rowLayout() {
|
|
1362
|
+
var _a;
|
|
1363
|
+
const rows = this.dataModel;
|
|
1364
|
+
// the component does not set it together with variances: a variance bar has one thickness
|
|
1365
|
+
const byTotal = ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.thickness) === "total";
|
|
1366
|
+
// the name of the scenario stands below the last bar: it needs room when a sum row follows
|
|
1367
|
+
const lastBar = rows.reduce((last, row, index) => (row.sum ? last : index), -1);
|
|
1368
|
+
const nameBelow = !this.stackConfig && !this.scenarioLabelTop && lastBar >= 0 && lastBar < rows.length - 1;
|
|
1369
|
+
if (!byTotal && !nameBelow && !rows.some((row) => row.gapAfter))
|
|
1370
|
+
return null;
|
|
1371
|
+
const totals = rows.map((row) => {
|
|
1372
|
+
if (!byTotal || row.sum)
|
|
1373
|
+
return null;
|
|
1374
|
+
const groups = this.stackGroups(row);
|
|
1375
|
+
const front = groups[groups.length - 1];
|
|
1376
|
+
return front ? Math.abs(front.layout.total) : 0;
|
|
1377
|
+
});
|
|
1378
|
+
const weights = this.rowWeights(totals);
|
|
1379
|
+
const start = [];
|
|
1380
|
+
const weight = [];
|
|
1381
|
+
let at = 0;
|
|
1382
|
+
rows.forEach((row, index) => {
|
|
1383
|
+
const rowWeight = weights[index];
|
|
1384
|
+
start.push(at);
|
|
1385
|
+
weight.push(rowWeight);
|
|
1386
|
+
at += rowWeight + (row.gapAfter && index < rows.length - 1 ? 0.5 : 0) + (nameBelow && index === lastBar ? 1 : 0);
|
|
1387
|
+
});
|
|
1388
|
+
return { start, weight, total: at, nameBelowBar: nameBelow };
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* The height of each row in units of a normal row: by its total, with the same height
|
|
1392
|
+
* over all. A row is at least one line of text high, and the others give up that room.
|
|
1393
|
+
* Null stands for a row that keeps the normal height.
|
|
1394
|
+
*/
|
|
1395
|
+
rowWeights(totals) {
|
|
1396
|
+
const minimum = Math.min(1, 1 / this.styles.scaleFactors.categoryWidth);
|
|
1397
|
+
const weights = totals.map((total) => (total === null ? 1 : 0));
|
|
1398
|
+
const open = new Set(totals.flatMap((total, index) => (total === null ? [] : [index])));
|
|
1399
|
+
let room = open.size;
|
|
1400
|
+
// a row below the minimum is set to it, then the rest is shared again
|
|
1401
|
+
for (;;) {
|
|
1402
|
+
const sum = [...open].reduce((all, index) => all + totals[index], 0);
|
|
1403
|
+
if (sum <= 0) {
|
|
1404
|
+
open.forEach((index) => (weights[index] = room / open.size));
|
|
1405
|
+
break;
|
|
1406
|
+
}
|
|
1407
|
+
const small = [...open].filter((index) => (totals[index] / sum) * room < minimum);
|
|
1408
|
+
if (small.length === 0 || small.length === open.size) {
|
|
1409
|
+
open.forEach((index) => (weights[index] = (totals[index] / sum) * room));
|
|
1410
|
+
break;
|
|
1411
|
+
}
|
|
1412
|
+
for (const index of small) {
|
|
1413
|
+
weights[index] = minimum;
|
|
1414
|
+
room -= minimum;
|
|
1415
|
+
open.delete(index);
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
return weights;
|
|
1419
|
+
}
|
|
1420
|
+
boldWidth(text) {
|
|
1421
|
+
return CalculateSize(text, {
|
|
1422
|
+
font: this.styles.fontStyles.font,
|
|
1423
|
+
fontSize: this.styles.fontStyles.size + "px",
|
|
1424
|
+
fontWeight: "bold",
|
|
1425
|
+
}).width;
|
|
1426
|
+
}
|
|
1427
|
+
sumText(value) {
|
|
1428
|
+
const digits = this.dataFormatDigits.decimal;
|
|
1429
|
+
return Formatter.number(value, "decimal", digits, digits, this.measureScale.value, this.styles.locale);
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* Where the numbers of the text sum rows stand, as distance from the axis: one column
|
|
1433
|
+
* per series, the same for all sum rows, then the total. Null without such a row.
|
|
1434
|
+
*/
|
|
1435
|
+
sumColumns() {
|
|
1436
|
+
const rows = this.dataModel.filter((row) => this.isTextSum(row));
|
|
1437
|
+
if (rows.length === 0)
|
|
1438
|
+
return null;
|
|
1439
|
+
const font = this.styles.fontStyles.size;
|
|
1440
|
+
const key = font * 0.6;
|
|
1441
|
+
const keyGap = font * 0.3;
|
|
1442
|
+
const columnGap = font * 1.4;
|
|
1443
|
+
const widths = [];
|
|
1444
|
+
let totalWidth = 0;
|
|
1445
|
+
for (const row of rows) {
|
|
1446
|
+
const groups = this.stackGroups(row);
|
|
1447
|
+
const front = groups[groups.length - 1];
|
|
1448
|
+
if (!front)
|
|
1449
|
+
continue;
|
|
1450
|
+
front.segments.forEach((segment, index) => {
|
|
1451
|
+
var _a;
|
|
1452
|
+
const width = Number.isFinite(segment.value) ? this.boldWidth(this.sumText(segment.value)) : 0;
|
|
1453
|
+
widths[index] = Math.max((_a = widths[index]) !== null && _a !== void 0 ? _a : 0, width);
|
|
1454
|
+
});
|
|
1455
|
+
totalWidth = Math.max(totalWidth, this.boldWidth(this.sumText(front.layout.total)));
|
|
1456
|
+
}
|
|
1457
|
+
const x = [];
|
|
1458
|
+
let at = font * 0.3;
|
|
1459
|
+
// one series: its sum is the total
|
|
1460
|
+
for (const width of widths.length > 1 ? widths : []) {
|
|
1461
|
+
x.push(at);
|
|
1462
|
+
at += key + keyGap + width + columnGap;
|
|
1463
|
+
}
|
|
1464
|
+
return { x, totalX: at, width: at + totalWidth, key, keyGap };
|
|
1465
|
+
}
|
|
1466
|
+
/** A sum row of a stack as numbers: per series a small square in its colour and the sum, then the total. */
|
|
1467
|
+
drawSumText(row, index, group) {
|
|
1468
|
+
const columns = this.sumColumns();
|
|
1469
|
+
const groups = this.stackGroups(row);
|
|
1470
|
+
const front = groups[groups.length - 1];
|
|
1471
|
+
if (!columns || !front)
|
|
1472
|
+
return;
|
|
1473
|
+
const axisX = this.position.getBarPositionX();
|
|
1474
|
+
const y = this.position.getBarCenterPositionY(index);
|
|
1475
|
+
const colors = this.styles.stackColors;
|
|
1476
|
+
const sumGroup = group.group().id(`stacked_sum_${index}`);
|
|
1477
|
+
front.segments.forEach((segment, segmentIndex) => {
|
|
1478
|
+
if (!Number.isFinite(segment.value) || columns.x[segmentIndex] === undefined)
|
|
1479
|
+
return;
|
|
1480
|
+
const x = axisX + columns.x[segmentIndex];
|
|
1481
|
+
sumGroup
|
|
1482
|
+
.rect(columns.key, columns.key)
|
|
1483
|
+
.fill({ color: colors[segmentIndex % colors.length] })
|
|
1484
|
+
.cy(y)
|
|
1485
|
+
.x(x);
|
|
1486
|
+
new Label(sumGroup, x + columns.key + columns.keyGap, y, `SUM_SEG_${segmentIndex}`, segment.value, this.styles, HookLocation.Center, HookLocation.Left)
|
|
1487
|
+
.setEmphasis(true, false)
|
|
1488
|
+
.setPoint(segment.dataPoint.key)
|
|
1489
|
+
.draw(this.dataFormatDigits, this.measureScale);
|
|
1490
|
+
});
|
|
1491
|
+
new Label(sumGroup, axisX + columns.totalX, y, "SUM_TOTAL", front.layout.total, this.styles, HookLocation.Center, HookLocation.Left)
|
|
1492
|
+
.setEmphasis(true, false)
|
|
1493
|
+
.setPoint(levelKey(this.level, row.category), "total")
|
|
1494
|
+
.draw(this.dataFormatDigits, this.measureScale);
|
|
1495
|
+
}
|
|
1496
|
+
/** How far a sum row reaches right of the axis, in px. */
|
|
1497
|
+
sumRowExtent(row) {
|
|
1498
|
+
var _a, _b, _c;
|
|
1499
|
+
const labelToBar = this.styles.fontStyles.size * this.styles.scaleFactors.labelToBar;
|
|
1500
|
+
if (this.variancesDataModel.length > 0)
|
|
1501
|
+
return 0;
|
|
1502
|
+
if (this.isTextSum(row))
|
|
1503
|
+
return (_b = (_a = this.sumColumns()) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0;
|
|
1504
|
+
if (this.stackConfig) {
|
|
1505
|
+
const total = this.stackConfig.labels === "value" ? labelToBar + this.boldWidth(this.sumText((_c = row.scenario1.value) !== null && _c !== void 0 ? _c : 0)) : 0;
|
|
1506
|
+
return this.stackSides.positive * this.pixelPerUnit + total;
|
|
1507
|
+
}
|
|
1508
|
+
return this.sumValuesWidth(row);
|
|
1509
|
+
}
|
|
1510
|
+
/** Where the reference line stands, as distance from the axis in px. Null without a line in this chart. */
|
|
1511
|
+
referenceLineOffset() {
|
|
1512
|
+
var _a;
|
|
1513
|
+
const line = this.referenceLine;
|
|
1514
|
+
if (!line || !Number.isFinite(line.value) || this.variancesDataModel.length > 0)
|
|
1515
|
+
return null;
|
|
1516
|
+
const relative = ((_a = this.stackConfig) === null || _a === void 0 ? void 0 : _a.mode) === StackMode.RELATIVE;
|
|
1517
|
+
return (relative ? line.value / 100 : line.value) * this.pixelPerUnit;
|
|
1518
|
+
}
|
|
1519
|
+
/** The room right of the axis that the reference line and its label need. */
|
|
1520
|
+
referenceLineExtent() {
|
|
1521
|
+
var _a;
|
|
1522
|
+
const offset = this.referenceLineOffset();
|
|
1523
|
+
if (offset === null || offset <= 0)
|
|
1524
|
+
return 0;
|
|
1525
|
+
const label = ((_a = this.referenceLine) === null || _a === void 0 ? void 0 : _a.label)
|
|
1526
|
+
? CalculateSize(this.referenceLine.label, {
|
|
1527
|
+
font: this.styles.fontStyles.font,
|
|
1528
|
+
fontSize: this.styles.fontStyles.size + "px",
|
|
1529
|
+
}).width
|
|
1530
|
+
: 0;
|
|
1531
|
+
return offset + Math.max(label / 2, this.styles.fontStyles.size * 0.25);
|
|
1532
|
+
}
|
|
1533
|
+
/** The end of a 100 % stack: a line along the bar ends and one `100%` below it. */
|
|
1534
|
+
drawPercentMark(svgContainer) {
|
|
1535
|
+
if (!this.hasPercentMark())
|
|
1536
|
+
return;
|
|
1537
|
+
const group = svgContainer.group().id("PercentMark");
|
|
1538
|
+
const font = this.styles.fontStyles.size;
|
|
1539
|
+
const x = this.position.getBarPositionX() + this.stackSides.positive * this.pixelPerUnit;
|
|
1540
|
+
const top = this.position.getAxisPositionY();
|
|
1541
|
+
const bottom = this.position.getAxisEndPositionY();
|
|
1542
|
+
const tip = bottom + font * 0.15;
|
|
1543
|
+
const base = tip + font * 0.5;
|
|
1544
|
+
group.line(x, top, x, bottom).stroke({ color: "#000000", width: this.position.spacings.axisLineWidth });
|
|
1545
|
+
group.polygon(`${x},${tip} ${x - font * 0.25},${base} ${x + font * 0.25},${base}`).fill("#000000");
|
|
1546
|
+
new Label(group, x, base + font * 0.1, "PERCENT_MARK", "100%", this.styles, HookLocation.Top, HookLocation.Center)
|
|
1547
|
+
.setEmphasis(true, false)
|
|
1548
|
+
.draw();
|
|
1549
|
+
}
|
|
1550
|
+
/** A line across the bars at one value, with its label above the first bar. In a 100 % stack the value is a percent. */
|
|
1551
|
+
drawReferenceLine(svgContainer) {
|
|
1552
|
+
const line = this.referenceLine;
|
|
1553
|
+
const offset = this.referenceLineOffset();
|
|
1554
|
+
if (!line || offset === null || this.dataModel.length === 0)
|
|
1555
|
+
return;
|
|
1556
|
+
const group = svgContainer.group().id("ReferenceLine");
|
|
1557
|
+
const font = this.styles.fontStyles.size;
|
|
1558
|
+
const color = `var(--bn-annotation-color, ${SYSTEM_STYLE_DEFAULTS["bn-annotation"].color})`;
|
|
1559
|
+
const x = this.position.getBarPositionX() + offset;
|
|
1560
|
+
const top = this.position.getStackedHeaderPositionY() - (this.stackConfig || this.scenarioLabelTop ? font * 1.1 : 0);
|
|
1561
|
+
// down to the last row from the top that reaches the line, or over all rows when the first one does not
|
|
1562
|
+
const reaches = (row) => {
|
|
1563
|
+
var _a, _b;
|
|
1564
|
+
if (row.sum)
|
|
1565
|
+
return false;
|
|
1566
|
+
if (!this.stackConfig)
|
|
1567
|
+
return ((_a = row.scenario1.value) !== null && _a !== void 0 ? _a : 0) >= line.value;
|
|
1568
|
+
const first = (_b = this.stackGroups(row)[0]) === null || _b === void 0 ? void 0 : _b.layout.segments[0];
|
|
1569
|
+
return first != null && first.end * this.pixelPerUnit >= x - this.position.getBarPositionX();
|
|
1570
|
+
};
|
|
1571
|
+
let lastBar = this.dataModel.findIndex((row) => !reaches(row)) - 1;
|
|
1572
|
+
if (lastBar < 0)
|
|
1573
|
+
lastBar = this.dataModel.reduce((last, row, index) => (row.sum ? last : index), 0);
|
|
1574
|
+
const bottom = this.position.getBarCenterPositionY(lastBar) + this.position.getBarWidthAt(lastBar) / 2;
|
|
1575
|
+
group
|
|
1576
|
+
.line(x, top, x, bottom)
|
|
1577
|
+
.attr("style", `stroke: ${color}; stroke-width: ${Math.max(1, this.position.spacings.axisLineWidth)}px`);
|
|
1578
|
+
group
|
|
1579
|
+
.polygon(`${x},${top + font * 0.5} ${x - font * 0.25},${top} ${x + font * 0.25},${top}`)
|
|
1580
|
+
.attr("style", `fill: ${color}`);
|
|
1581
|
+
if (line.label) {
|
|
1582
|
+
group
|
|
1583
|
+
.plain(line.label)
|
|
1584
|
+
.attr({ x, y: top - font * 0.3, "text-anchor": "middle" })
|
|
1585
|
+
.attr("style", `fill: ${color}`);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1066
1588
|
hasNoData() {
|
|
1067
1589
|
return this.dataModel.length === 0;
|
|
1068
1590
|
}
|