Repository navigation
Description
What problem does this feature solve?
Version checked: ECharts 6.1.0. Component: cartesian series-bar (stacked).
Summary
When bars are stacked, the only way to visually separate adjacent segments is to give every bar an itemStyle.borderColor that matches the chart background. That is a workaround, not a gap, and it has side effects that make it unsuitable for accessible charts. A first-class stackGap option would put a gap between stacked segments only, leaving the outer edges (baseline, top of stack, left and right) untouched.
Why separation between segments matters (accessibility)
Stacked segments are usually told apart by colour alone. That breaks down for readers with colour vision deficiency (CVD) or low vision, and for anyone viewing a greyscale print or a low-contrast display. In the figure below, the same data is shown with segments touching (left) and with a gap (right), under simulated normal vision, protanopia, deuteranopia, tritanopia and greyscale.
What to look for:
- Deuteranopia and protanopia: the red, green, orange and brown segments collapse into a narrow band of olive and brown tones. Without a gap the boundaries between neighbouring segments are hard to find. With the gap, each segment reads as a separate block.
- Greyscale: the orange and green segments become the same grey. Without a gap they merge into one block. With the gap they are two blocks.
A gap helps because it means each segment is distinguished from the background rather than only from its neighbour. This is relevant to WCAG 2.2 SC 1.4.11 Non-text Contrast, which asks for a 3:1 contrast ratio between graphical objects that are needed to understand content and their adjacent colours. If segments touch, every pair of neighbouring segments is "adjacent" to the other, so each pair needs enough contrast against each other, which is difficult to satisfy across a multi-series palette. With a gap, the adjacent colour is the background. (SC 1.4.11 does not require a gap. This is the argument for why a gap makes compliance achievable.)
Published guidance recommends this separation:
- The data.europa.eu Data Visualisation Guide advises "a visible outline between the bars in a stack", saying it improves legibility, especially for people with colour blindness.
- The Morningstar design system builds 3px gaps between all adjacent plots (for example in stacked bars) into its chart elements, so that colour-blind and low-vision users can tell plots apart.
- Storytelling with Data recommends white divider lines between categories as a way to separate sections without relying on colour alone.
Why a border is not a substitute
The current workaround is:
itemStyle: { borderColor: '#ffffff', borderWidth: 1 }This has real limitations, most seriously for thin segments.
1. It hides thin segments. The border is a stroke centred on the edge of each rectangle, so it removes about half its width from each side of the segment. A segment whose height is less than the border width is covered completely. Small segments are exactly the ones readers most need to be able to find (for example a 0.5% allocation in a 100% stacked chart). In the figure below the purple segment has a value of 0.5. It is visible with no border (left), disappears with a 1px border (centre), and stays visible with the proposed 1px gap (right). A wider border hides progressively larger segments.
2. It is coupled to the background colour. borderColor must equal the background, so it breaks on gradients, images, theme changes, and hover or emphasis states unless the application keeps them in sync. transparent does not work, because the stroke does not erase the fill beneath it.
3. It affects every edge. Bars become slightly narrower and shorter, and stop short of the baseline and the top of the stack. The gap is not confined to the boundary between segments.
4. Zero-height segments can leave a line. A zero or near-zero segment can still draw its border across the segment below it.
Other approaches are also unsatisfactory. A transparent spacer series puts the gap in data units rather than pixels, distorts axis totals and tooltips, and has to be hidden from the legend. A custom series with renderItem loses stacking, labels, emphasis and barMaxWidth handling, so it is not practical as a general solution.
Intended geometry
What does the proposed API look like?
A new option on bar series, in pixels, default 0 (no change to existing charts):
option = {
xAxis: { type: 'category', data: ['A', 'B', 'C'] },
yAxis: { type: 'value' },
series: [
{ type: 'bar', stack: 'total', stackGap: 1, data: [20, 15, 25] },
{ type: 'bar', stack: 'total', stackGap: 1, data: [20, 25, 10] },
{ type: 'bar', stack: 'total', stackGap: 1, data: [20, 20, 25] }
]
};Proposed behaviour
stackGapis the width of the visible gap in pixels between two touching segments of the same stack. Each segment is inset bystackGap / 2at the edge it shares with a neighbour.- Only interior edges are inset. The edge at the baseline, the outermost edge of the stack, and the left and right edges are unchanged.
- No gap is added next to a neighbour that is zero,
nullor missing, because there is no shared edge. - Positive and negative stacks are handled separately: a gap is applied between same-sign neighbours only. The edge next to the baseline is an outer edge.
- A non-zero segment is never reduced below 1px (or
barMinHeight, if set). If the segment is too thin to give upstackGap / 2at both ends, the inset is reduced for that segment rather than hiding it. This is the key difference from the border workaround. - The gap is purely visual. Data values, axis scale, tooltips and labels are unchanged. The gap area is not part of either segment's hit area.
- Applies to both vertical and horizontal bars. Polar bars could be a follow-up.
- Works with
itemStyle.borderRadius, rounding the corners of each inset segment.
I am happy to work on a PR for this if the API is acceptable. The figures were produced with a custom series mock-up (not the proposed implementation) and with Machado, Oliveira and Fernandes (2009) colour vision deficiency simulation matrices.
Activity
ahuja2666 commentedon Nov 20, 2026
Hi, I would like to work on this. I will prototype the proposed
stackGapoption for Cartesian bar series and add regression coverage for orientation, stacked signs, zero or missing segments, and thin bars. I will follow up if the layout constraints call for a narrower initial scope.Cabinet20 commentedon Nov 23, 2026
I'm more than happy for you to work on it. Also happy to help in any way I can.
ahuja2666 commentedon Nov 24, 2026
Thanks for the encouragement! I opened PR #21852 with the implementation and regression tests. The documentation is in apache/echarts-doc#545. Both are open for review.