Skip to content

Commit 239cf05

Browse files
Component | Stacked Bar: Add stacked spacing option
1 parent 81d8dde commit 239cf05

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

packages/ts/src/components/stacked-bar/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { ColorAccessor, StringAccessor } from 'types/accessor'
55
import { Orientation } from 'types/position'
66

77
export interface StackedBarConfigInterface<Datum> extends XYComponentConfigInterface<Datum> {
8+
/** Spacing in pixels between stacked items in a single bar. Default: `0` */
9+
stackSpacing?: number;
810
/** Bar color accessor function. Default: `d => d.color` */
911
color?: ColorAccessor<Datum>;
1012
/** Force set bar width in pixels. Default: `undefined` */
@@ -39,6 +41,7 @@ export const StackedBarDefaultConfig: StackedBarConfigInterface<unknown> = {
3941
dataStep: undefined,
4042
barPadding: 0.0,
4143
roundedCorners: 2,
44+
stackSpacing: 0,
4245
cursor: null,
4346
barMinHeight1Px: false,
4447
barMinHeightZeroValue: null,

packages/ts/src/components/stacked-bar/index.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,33 +220,50 @@ export class StackedBar<Datum> extends XYComponentCore<Datum, StackedBarConfigIn
220220
const x = -barWidth / 2
221221
const width = barWidth
222222

223+
// --- Spacing between stacked items ---
224+
const stackSpacing = config.stackSpacing || 0
225+
let hWithSpacing = h
226+
let yWithSpacing = y
227+
// Only apply spacing between inner segments (not at base or top)
228+
const isInnerStack = accessorIndex > 0 && accessorIndex < this.getAccessors().length
229+
if (!isEntering && stackSpacing > 0 && isInnerStack) {
230+
if (this.isVertical()) {
231+
hWithSpacing = Math.max(0, h - stackSpacing)
232+
yWithSpacing = y + (isNegative ? -stackSpacing / 2 : stackSpacing / 2)
233+
} else {
234+
hWithSpacing = Math.max(0, h - stackSpacing)
235+
yWithSpacing = y + (isNegative ? -stackSpacing / 2 : stackSpacing / 2)
236+
}
237+
}
238+
223239
const cornerRadius = config.roundedCorners
224240
? isNumber(config.roundedCorners) ? +config.roundedCorners : width / 2
225241
: 0
226-
const cornerRadiusClamped = clamp(cornerRadius, 0, Math.min(height, width) / 2)
242+
const cornerRadiusClamped = clamp(cornerRadius, 0, Math.min(hWithSpacing, width) / 2)
227243
const isNorthDirected = this.yScale.range()[0] > this.yScale.range()[1]
228244

245+
const allCornersRounded = stackSpacing > 0 && !!config.roundedCorners
229246
return roundedRectPath({
230-
x: this.isVertical() ? x : y - h,
231-
y: this.isVertical() ? y + (isNorthDirected ? 0 : -h) : x,
232-
w: this.isVertical() ? width : h,
233-
h: this.isVertical() ? h : width,
234-
tl: isEnding && (this.isVertical()
247+
x: this.isVertical() ? x : yWithSpacing - hWithSpacing,
248+
y: this.isVertical() ? yWithSpacing + (isNorthDirected ? 0 : -hWithSpacing) : x,
249+
w: this.isVertical() ? width : hWithSpacing,
250+
h: this.isVertical() ? hWithSpacing : width,
251+
tl: allCornersRounded || (isEnding && (this.isVertical()
235252
? (!isNegative && isNorthDirected) || (isNegative && !isNorthDirected)
236253
: isNegative
237-
),
238-
tr: isEnding && (this.isVertical()
254+
)),
255+
tr: allCornersRounded || (isEnding && (this.isVertical()
239256
? (!isNegative && isNorthDirected) || (isNegative && !isNorthDirected)
240257
: !isNegative
241-
),
242-
br: isEnding && (this.isVertical()
258+
)),
259+
br: allCornersRounded || (isEnding && (this.isVertical()
243260
? (isNegative && isNorthDirected) || (!isNegative && !isNorthDirected)
244261
: !isNegative
245-
),
246-
bl: isEnding && (this.isVertical()
262+
)),
263+
bl: allCornersRounded || (isEnding && (this.isVertical()
247264
? (isNegative && isNorthDirected) || (!isNegative && !isNorthDirected)
248265
: isNegative
249-
),
266+
)),
250267
r: cornerRadiusClamped,
251268
})
252269
}

0 commit comments

Comments
 (0)