Components · Bars

Grouped bar chart

Side-by-side bars for each value of a second dimension.

Open in playground

Preview

Revenue by industry and billing

Usage

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

const industry = dimension({ name: 'industry', title: 'Industry' });
const billing = dimension({ name: 'billing', title: 'Billing' });
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', abbreviateLargeNumber: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartGrouped
        title="Revenue by industry and billing"
        xAxis={industry}
        groupBy={billing}
        measure={revenue}
        results={results(rows)}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { industry: 'Health', billing: 'Monthly', revenue: 62141 },
  { industry: 'Health', billing: 'Annual', revenue: 176864 },
  { industry: 'Media', billing: 'Monthly', revenue: 99606 },
  { industry: 'Media', billing: 'Annual', revenue: 51312 },
  { industry: 'Retail', billing: 'Monthly', revenue: 104861 },
  { industry: 'Retail', billing: 'Annual', revenue: 75934 },
  { industry: 'Tech', billing: 'Monthly', revenue: 114706 },
  { industry: 'Tech', billing: 'Annual', revenue: 152053 },
];
  • The xAxis 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

Value labels

Every bar labelled with its compact value.

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

const industry = dimension({ name: 'industry', title: 'Industry' });
const billing = dimension({ name: 'billing', title: 'Billing' });
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', abbreviateLargeNumber: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartGrouped
        title="Revenue by industry and billing"
        xAxis={industry}
        groupBy={billing}
        measure={revenue}
        results={results(rows)}
        showValueLabels
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { industry: 'Health', billing: 'Monthly', revenue: 62141 },
  { industry: 'Health', billing: 'Annual', revenue: 176864 },
  { industry: 'Media', billing: 'Monthly', revenue: 99606 },
  { industry: 'Media', billing: 'Annual', revenue: 51312 },
  { industry: 'Retail', billing: 'Monthly', revenue: 104861 },
  { industry: 'Retail', billing: 'Annual', revenue: 75934 },
  { industry: 'Tech', billing: 'Monthly', revenue: 114706 },
  { industry: 'Tech', billing: 'Annual', revenue: 152053 },
];
const industry = dimension({ name: 'industry', title: 'Industry' });
const billing = dimension({ name: 'billing', title: 'Billing' });
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', abbreviateLargeNumber: true },
});

<BarChartGrouped
  title="Revenue by industry and billing"
  xAxis={industry}
  groupBy={billing}
  measure={revenue}
  results={results(rows)}
  showValueLabels
/>

Axis titles

A caption on each axis.

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

const industry = dimension({ name: 'industry', title: 'Industry' });
const billing = dimension({ name: 'billing', title: 'Billing' });
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', abbreviateLargeNumber: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartGrouped
        title="Revenue by industry and billing"
        xAxis={industry}
        groupBy={billing}
        measure={revenue}
        results={results(rows)}
        xAxisLabel="Industry"
        yAxisLabel="Revenue"
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { industry: 'Health', billing: 'Monthly', revenue: 62141 },
  { industry: 'Health', billing: 'Annual', revenue: 176864 },
  { industry: 'Media', billing: 'Monthly', revenue: 99606 },
  { industry: 'Media', billing: 'Annual', revenue: 51312 },
  { industry: 'Retail', billing: 'Monthly', revenue: 104861 },
  { industry: 'Retail', billing: 'Annual', revenue: 75934 },
  { industry: 'Tech', billing: 'Monthly', revenue: 114706 },
  { industry: 'Tech', billing: 'Annual', revenue: 152053 },
];
const industry = dimension({ name: 'industry', title: 'Industry' });
const billing = dimension({ name: 'billing', title: 'Billing' });
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', abbreviateLargeNumber: true },
});

<BarChartGrouped
  title="Revenue by industry and billing"
  xAxis={industry}
  groupBy={billing}
  measure={revenue}
  results={results(rows)}
  xAxisLabel="Industry"
  yAxisLabel="Revenue"
/>

Props

Keyboard and screen readers: Accessibility.

PropTypeDefaultDescription
xAxisrequiredDimensionThe dimension along the x-axis. A time dimension gets gap filling and date labels.
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.
reverseXAxisbooleanfalseReverse the order of the x-axis.
yAxisRangeMaxnumberFix the highest value on the y-axis. When unset, it fits the data.
yAxisRangeMinnumberFix 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.

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

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