Stacked bar chart
Show the composition of each bar by a second dimension.
Preview
Revenue by month and plan
Usage
import {
ChartsProvider,
BarChartStacked,
dimension,
measure,
results,
} from 'em-kit';
const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const plan = dimension({ name: 'plan', title: 'Plan' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 360 }}>
<BarChartStacked
title="Revenue by month and plan"
xAxis={startDate}
groupBy={plan}
measure={revenue}
results={results(rows)}
/>
</div>
</ChartsProvider>
);
const rows = [
{ startDate: '2026-03-01T00:00:00.000', plan: 'Starter', revenue: 23494 },
{ startDate: '2026-03-01T00:00:00.000', plan: 'Growth', revenue: 62545 },
{ startDate: '2026-03-01T00:00:00.000', plan: 'Enterprise', revenue: 55581 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Starter', revenue: 21223 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Growth', revenue: 57533 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Enterprise', revenue: 59682 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Starter', revenue: 22081 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Growth', revenue: 61318 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Enterprise', revenue: 65483 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Starter', revenue: 21648 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Growth', revenue: 66775 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Enterprise', revenue: 72202 },
];- Rows are long format: one per bar and segment, with the
groupByvalue on each. - The
xAxisvalues are drawn A to Z, whatever the order of your rows, unless you passaxisOrder.
Install with npm install em-kit. Needs <ChartsProvider> once and a parent with a height: Quick start.
Examples
Totals
Each stack’s total printed above it, in compact numbers. showTotalLabels adds the totals; the segments keep their own tooltips.
Show full code (50 lines)
import {
ChartsProvider,
BarChartStacked,
dimension,
measure,
results,
} from 'em-kit';
const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const plan = dimension({ name: 'plan', title: 'Plan' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 360 }}>
<BarChartStacked
title="Revenue by month and plan"
xAxis={startDate}
groupBy={plan}
measure={revenue}
results={results(rows)}
showTotalLabels
/>
</div>
</ChartsProvider>
);
const rows = [
{ startDate: '2026-03-01T00:00:00.000', plan: 'Starter', revenue: 23494 },
{ startDate: '2026-03-01T00:00:00.000', plan: 'Growth', revenue: 62545 },
{ startDate: '2026-03-01T00:00:00.000', plan: 'Enterprise', revenue: 55581 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Starter', revenue: 21223 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Growth', revenue: 57533 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Enterprise', revenue: 59682 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Starter', revenue: 22081 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Growth', revenue: 61318 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Enterprise', revenue: 65483 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Starter', revenue: 21648 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Growth', revenue: 66775 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Enterprise', revenue: 72202 },
];const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const plan = dimension({ name: 'plan', title: 'Plan' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
<BarChartStacked
title="Revenue by month and plan"
xAxis={startDate}
groupBy={plan}
measure={revenue}
results={results(rows)}
showTotalLabels
/>Segment values
Every segment labelled, for reading the mix as well as the total.
Show full code (51 lines)
import {
ChartsProvider,
BarChartStacked,
dimension,
measure,
results,
} from 'em-kit';
const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const plan = dimension({ name: 'plan', title: 'Plan' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 360 }}>
<BarChartStacked
title="Revenue by month and plan"
xAxis={startDate}
groupBy={plan}
measure={revenue}
results={results(rows)}
showValueLabels
showTotalLabels
/>
</div>
</ChartsProvider>
);
const rows = [
{ startDate: '2026-03-01T00:00:00.000', plan: 'Starter', revenue: 23494 },
{ startDate: '2026-03-01T00:00:00.000', plan: 'Growth', revenue: 62545 },
{ startDate: '2026-03-01T00:00:00.000', plan: 'Enterprise', revenue: 55581 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Starter', revenue: 21223 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Growth', revenue: 57533 },
{ startDate: '2026-04-01T00:00:00.000', plan: 'Enterprise', revenue: 59682 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Starter', revenue: 22081 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Growth', revenue: 61318 },
{ startDate: '2026-05-01T00:00:00.000', plan: 'Enterprise', revenue: 65483 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Starter', revenue: 21648 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Growth', revenue: 66775 },
{ startDate: '2026-06-01T00:00:00.000', plan: 'Enterprise', revenue: 72202 },
];const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const plan = dimension({ name: 'plan', title: 'Plan' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
<BarChartStacked
title="Revenue by month and plan"
xAxis={startDate}
groupBy={plan}
measure={revenue}
results={results(rows)}
showValueLabels
showTotalLabels
/>Props
Not listed: maxLegendItems, which the type accepts and the component does not read. Keyboard and screen readers: Accessibility.
| Prop | Type | Default | Description |
|---|---|---|---|
xAxisrequired | Dimension | The dimension along the x-axis. A time dimension gets gap filling and date labels. | |
groupByrequired | Dimension | A second dimension that splits each axis value into one series per value. | |
measurerequired | Measure | The numeric field to plot. Its inputs (prefix, suffix, decimalPlaces) set the formatting. | |
resultsrequired | DataResponse | Your query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array. | |
resultsAxisOrder | DataResponse | A separate result listing the axis values in the order to draw them, for example largest first. Reported back through setAxisOrderAndCacheKey. | |
axisOrder | string[] | The axis values in the order to draw them. Values missing from the list are left out. | |
axisOrderCacheKey | string | Any string identifying the resultsAxisOrder query. Passed back as the second argument of setAxisOrderAndCacheKey, which is only called when this is set. | |
setAxisOrderAndCacheKey | ( | Called with the values from resultsAxisOrder and axisOrderCacheKey once that result has loaded. Store the values and pass them as axisOrder. | |
resultsGroupOrder | DataResponse | A separate result listing the groupBy values in the order you want, for example largest first. Reported back through setGroupOrderAndCacheKey. | |
resultsGroupOther | DataResponse | Totals per axis value across every group. The chart subtracts the groups in results and draws what is left as an “Other” series. | |
groupOrderCacheKey | string | Any string identifying the resultsGroupOrder query. Passed back as the second argument of setGroupOrderAndCacheKey, which is only called when this is set. | |
setGroupOrderAndCacheKey | ( | Called with the values from resultsGroupOrder and groupOrderCacheKey once that result has loaded. Store them to query only the top groups. | |
showLegend | boolean | true | Show the legend. A chart with one series shows none; entries wrap onto more lines when they do not fit. |
showLogarithmicScale | boolean | false | Use a logarithmic value axis, for values that span several orders of magnitude. |
showTooltips | boolean | true | Show a formatted tooltip on hover. |
showTotalLabels | boolean | false | Print the total at the end of each stack. |
showValueLabels | boolean | false | Print each value on the chart. |
xAxisLabel | string | A title under the x-axis. | |
yAxisLabel | string | A title beside the y-axis. | |
granularity | Granularity | Only sets the size of the date range a click reports (dimensionTimeRange). The axis labels and the gap filling come from the time field, dimension({ granularity }), and this prop follows the field when left out. One of "second", "minute", "hour", "day", "week", "month", "quarter", "year". | |
setGranularity | ( | Called with the granularity the reader picks in the card. The picker only shows when the time field sets showGranularityDropdown: true in its inputs. Store the value, set it as the field’s granularity and pass rows at that granularity. | |
onBarClicked | ( | Called with the clicked bar’s dimension value and, on a time axis, its date range. Use it as a filter.BarChartStackedOptionsClickArg = { dimensionValue: string | number | undefined; dimensionTimeRange: TimeRange; measureValues?: Record<string, unknown> | undefined; groupingDimensionValue: string | undefined; groupingDimensionTimeRange: TimeRange; } | |
componentName | string | A name for this component, included in the interaction events it dispatches on the DOM, for your analytics. | |
trackingId | string | An id included in the same events, to tell two instances apart. | |
title | string | The card’s title. A "key|fallback" string is looked up in your translations. | |
description | string | Smaller text under the title. Translates like title. | |
tooltip | string | Text for an info icon beside the title. | |
hideMenu | boolean | false | Hide the card’s header: the title, description and menu. |
menuOptions | ( | ["csv", | Which entries the card menu offers. Pass [] for none. |
reverseXAxis | boolean | false | Reverse the order of the x-axis. |
yAxisRangeMax | number | Fix the highest value on the y-axis. When unset, it fits the data. | |
yAxisRangeMin | number | Fix the lowest value on the y-axis. When unset, a bar, line or area chart’s axis includes zero, and a scatter or bubble chart’s fits the data. |
A blank default means the prop is unset unless you pass it.
| Type | Definition |
|---|---|
Granularity | "second" | |
TimeRange | { from: Date |What a click’s TimeRange holds: Events. |
Field options
BarChartStacked reads only the common options in a field’s inputs.