Components · Stacked

Stacked horizontal bar chart

The composition of each bar by a second dimension, laid out horizontally for long labels.

Open in playground

Preview

Customers by region and plan

Usage

tsx
import {
  ChartsProvider,
  BarChartStackedHorizontal,
  dimension,
  measure,
  results,
} from 'em-kit';

const region = dimension({ name: 'region', title: 'Region' });
const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { abbreviateLargeNumber: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartStackedHorizontal
        title="Customers by region and plan"
        yAxis={region}
        groupBy={plan}
        measure={customers}
        results={results(rows)}
        axisOrder={[
          'North America',
          'Europe',
          'Asia Pacific',
          'Latin America',
          'Middle East',
        ]}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { region: 'North America', plan: 'Starter', customers: 500 },
  { region: 'North America', plan: 'Growth', customers: 401 },
  { region: 'North America', plan: 'Enterprise', customers: 84 },
  { region: 'Europe', plan: 'Starter', customers: 377 },
  { region: 'Europe', plan: 'Growth', customers: 278 },
  { region: 'Europe', plan: 'Enterprise', customers: 54 },
  { region: 'Asia Pacific', plan: 'Starter', customers: 204 },
  { region: 'Asia Pacific', plan: 'Growth', customers: 215 },
  { region: 'Asia Pacific', plan: 'Enterprise', customers: 32 },
  { region: 'Latin America', plan: 'Starter', customers: 110 },
  { region: 'Latin America', plan: 'Growth', customers: 108 },
  { region: 'Latin America', plan: 'Enterprise', customers: 22 },
  { region: 'Middle East', plan: 'Starter', customers: 91 },
  { region: 'Middle East', plan: 'Growth', customers: 83 },
  { region: 'Middle East', plan: 'Enterprise', customers: 14 },
];
  • The yAxis values are drawn A to Z, whatever the order of your rows, unless you pass axisOrder.

Install with npm install em-kit. Needs <ChartsProvider> once and a parent with a height: Quick start.

Examples

Totals

Each stack’s total printed at its end, in compact numbers.

tsxDiffers from Usage
Show full code (55 lines)
import {
  ChartsProvider,
  BarChartStackedHorizontal,
  dimension,
  measure,
  results,
} from 'em-kit';

const region = dimension({ name: 'region', title: 'Region' });
const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { abbreviateLargeNumber: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartStackedHorizontal
        title="Customers by region and plan"
        yAxis={region}
        groupBy={plan}
        measure={customers}
        results={results(rows)}
        axisOrder={[
          'North America',
          'Europe',
          'Asia Pacific',
          'Latin America',
          'Middle East',
        ]}
        showTotalLabels
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { region: 'North America', plan: 'Starter', customers: 500 },
  { region: 'North America', plan: 'Growth', customers: 401 },
  { region: 'North America', plan: 'Enterprise', customers: 84 },
  { region: 'Europe', plan: 'Starter', customers: 377 },
  { region: 'Europe', plan: 'Growth', customers: 278 },
  { region: 'Europe', plan: 'Enterprise', customers: 54 },
  { region: 'Asia Pacific', plan: 'Starter', customers: 204 },
  { region: 'Asia Pacific', plan: 'Growth', customers: 215 },
  { region: 'Asia Pacific', plan: 'Enterprise', customers: 32 },
  { region: 'Latin America', plan: 'Starter', customers: 110 },
  { region: 'Latin America', plan: 'Growth', customers: 108 },
  { region: 'Latin America', plan: 'Enterprise', customers: 22 },
  { region: 'Middle East', plan: 'Starter', customers: 91 },
  { region: 'Middle East', plan: 'Growth', customers: 83 },
  { region: 'Middle East', plan: 'Enterprise', customers: 14 },
];
<BarChartStackedHorizontal
  title="Customers by region and plan"
  yAxis={region}
  groupBy={plan}
  measure={customers}
  results={results(rows)}
  axisOrder={[
    'North America',
    'Europe',
    'Asia Pacific',
    'Latin America',
    'Middle East',
  ]}
  showTotalLabels
/>

Props

Keyboard and screen readers: Accessibility.

PropTypeDefaultDescription
yAxisrequiredDimensionThe dimension down the y-axis of a horizontal chart.
groupByrequiredDimensionA second dimension that splits each axis value into one series per value.
measurerequiredMeasureThe numeric field to plot. Its inputs (prefix, suffix, decimalPlaces) set the formatting.
resultsrequiredDataResponseYour query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array.
resultsAxisOrderDataResponseA separate result listing the axis values in the order to draw them, for example largest first. Reported back through setAxisOrderAndCacheKey.
axisOrderstring[]The axis values in the order to draw them. Values missing from the list are left out.
axisOrderCacheKeystringAny string identifying the resultsAxisOrder query. Passed back as the second argument of setAxisOrderAndCacheKey, which is only called when this is set.
setAxisOrderAndCacheKey(values: string[], cacheKey: string) => voidCalled with the values from resultsAxisOrder and axisOrderCacheKey once that result has loaded. Store the values and pass them as axisOrder.
resultsGroupOrderDataResponseA separate result listing the groupBy values in the order you want, for example largest first. Reported back through setGroupOrderAndCacheKey.
resultsGroupOtherDataResponseTotals per axis value across every group. The chart subtracts the groups in results and draws what is left as an “Other” series.
groupOrderCacheKeystringAny string identifying the resultsGroupOrder query. Passed back as the second argument of setGroupOrderAndCacheKey, which is only called when this is set.
setGroupOrderAndCacheKey(values: string[], cacheKey: string) => voidCalled with the values from resultsGroupOrder and groupOrderCacheKey once that result has loaded. Store them to query only the top groups.
showLegendbooleantrueShow the legend. A chart with one series shows none; entries wrap onto more lines when they do not fit.
showLogarithmicScalebooleanfalseUse a logarithmic value axis, for values that span several orders of magnitude.
showTooltipsbooleantrueShow a formatted tooltip on hover.
showTotalLabelsbooleanfalsePrint the total at the end of each stack.
showValueLabelsbooleanfalsePrint each value on the chart.
xAxisLabelstringA title under the x-axis.
yAxisLabelstringA title beside the y-axis.
granularityGranularityOnly 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(granularity: Granularity) => voidCalled 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(args: BarChartStackedOptionsClickArg) => voidCalled 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; }
componentNamestringA name for this component, included in the interaction events it dispatches on the DOM, for your analytics.
trackingIdstringAn id included in the same events, to tell two instances apart.
titlestringThe card’s title. A "key|fallback" string is looked up in your translations.
descriptionstringSmaller text under the title. Translates like title.
tooltipstringText for an info icon beside the title.
hideMenubooleanfalseHide the card’s header: the title, description and menu.
menuOptions("csv" | "xlsx" | "png" | "maximize")[]["csv", "xlsx", "png", "maximize"]Which entries the card menu offers. Pass [] for none.
reverseYAxisbooleanfalseReverse the order of the y-axis.
xAxisRangeMaxnumberFix the highest value on the x-axis. When unset, it fits the data.
xAxisRangeMinnumberFix the lowest value on the x-axis. When unset, a horizontal bar 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.

TypeDefinition
Granularity"second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year"
TimeRange{ from: Date | undefined; to: Date | undefined; relativeTimeString?: string; } | undefinedWhat a click’s TimeRange holds: Events.

Field options

BarChartStackedHorizontal reads only the common options in a field’s inputs.