@guardian/interactive-component-library 0.1.0-alpha.19 → 0.1.0-alpha.20
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.
|
@@ -123,9 +123,7 @@ const defaultStyles$r = {
|
|
|
123
123
|
label: label$1,
|
|
124
124
|
backgroundRect
|
|
125
125
|
};
|
|
126
|
-
function preventOverlap(labelPositions, iteration = 0, labelSize = 12, coordinate = "y") {
|
|
127
|
-
const maxIterations = 10;
|
|
128
|
-
let totalOverlap = 0;
|
|
126
|
+
function preventOverlap(labelPositions, iteration = 0, labelSize = 12, coordinate = "y", moveBothLabels = true) {
|
|
129
127
|
for (let index2 = 1; index2 < labelPositions.length; index2++) {
|
|
130
128
|
const previousElement = labelPositions[index2 - 1];
|
|
131
129
|
const element = labelPositions[index2];
|
|
@@ -133,22 +131,22 @@ function preventOverlap(labelPositions, iteration = 0, labelSize = 12, coordinat
|
|
|
133
131
|
if (overlap < 0) {
|
|
134
132
|
continue;
|
|
135
133
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
if (moveBothLabels) {
|
|
135
|
+
previousElement[coordinate] -= overlap / 2;
|
|
136
|
+
element[coordinate] += overlap / 2;
|
|
137
|
+
} else {
|
|
138
|
+
previousElement[coordinate] -= overlap;
|
|
139
|
+
}
|
|
140
|
+
return labelPositions;
|
|
142
141
|
}
|
|
143
|
-
return labelPositions;
|
|
144
142
|
}
|
|
145
143
|
function uniqueBy(array, key) {
|
|
146
144
|
return [...array.reduce((map, d2) => map.set(d2[key], d2), /* @__PURE__ */ new Map()).values()];
|
|
147
145
|
}
|
|
148
|
-
function positionLabels(labels, coordinate = "y") {
|
|
146
|
+
function positionLabels(labels, labelSize = 12, coordinate = "y", moveBothLabels = true) {
|
|
149
147
|
labels = uniqueBy(labels, "value");
|
|
150
148
|
labels.sort((a, b) => a[coordinate] - b[coordinate]);
|
|
151
|
-
return preventOverlap(labels);
|
|
149
|
+
return preventOverlap(labels, 0, labelSize, coordinate, moveBothLabels);
|
|
152
150
|
}
|
|
153
151
|
function scaleLinear(domain, range) {
|
|
154
152
|
const [domainMin, domainMax] = domain;
|
|
@@ -168,7 +166,7 @@ function StackedBar({
|
|
|
168
166
|
width,
|
|
169
167
|
height,
|
|
170
168
|
hideLabels = false,
|
|
171
|
-
labelType = LabelType.
|
|
169
|
+
labelType = LabelType.inline,
|
|
172
170
|
showBackgroundRect = false,
|
|
173
171
|
createSVG = true,
|
|
174
172
|
styles: styles2
|
|
@@ -178,7 +176,7 @@ function StackedBar({
|
|
|
178
176
|
styles2 = mergeStyles({
|
|
179
177
|
...defaultStyles$r
|
|
180
178
|
}, styles2);
|
|
181
|
-
const svgHeight = labelType === LabelType.hanging ? height + 20 : height;
|
|
179
|
+
const svgHeight = labelType === LabelType.hanging && !hideLabels ? height + 20 : height;
|
|
182
180
|
const renderLabel = (config, i) => jsx("text", {
|
|
183
181
|
ref: (element) => textElements.current[i] = element,
|
|
184
182
|
"text-rendering": "optimizeLegibility",
|
|
@@ -229,11 +227,10 @@ function StackedBar({
|
|
|
229
227
|
textAnchor: "end",
|
|
230
228
|
alignmentBaseline: "hanging"
|
|
231
229
|
};
|
|
232
|
-
console.log(labelConfig);
|
|
233
230
|
totalW += itemWidth;
|
|
234
231
|
return labelConfig;
|
|
235
232
|
});
|
|
236
|
-
return preventOverlap(labels, 0, 20, "x");
|
|
233
|
+
return preventOverlap(labels, 0, 20, "x", false);
|
|
237
234
|
}, [stack, height, width]);
|
|
238
235
|
const backgroundRect2 = jsx("g", {
|
|
239
236
|
children: jsx("rect", {
|