Components · Bars

Horizontal bar chart

Bars laid out horizontally, for long category labels or ranked lists.

Open in playground

Preview

Customers by region

Usage

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

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

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartHorizontal
        title="Customers by region"
        dimension={region}
        measures={[customers]}
        results={results(rows)}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { region: 'North America', customers: 985 },
  { region: 'Europe', customers: 709 },
  { region: 'Asia Pacific', customers: 451 },
  { region: 'Latin America', customers: 240 },
  { region: 'Middle East', customers: 188 },
];

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

Examples

Truncated labels

Long category names cut to eight characters on the axis; the tooltip shows them in full. maxCharacters is a field option on the dimension.

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

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

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarChartHorizontal
        title="Customers by region"
        dimension={region}
        measures={[customers]}
        results={results(rows)}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { region: 'North America', customers: 985 },
  { region: 'Europe', customers: 709 },
  { region: 'Asia Pacific', customers: 451 },
  { region: 'Latin America', customers: 240 },
  { region: 'Middle East', customers: 188 },
];
const region = dimension({
  name: 'region',
  title: 'Region',
  inputs: { maxCharacters: 8 },
});

<BarChartHorizontal
  title="Customers by region"
  dimension={region}
  measures={[customers]}
  results={results(rows)}
/>

Props

Keyboard and screen readers: Accessibility.

PropTypeDefaultDescription
dimensionrequiredDimensionThe field whose values label the axis or the segments. Its name is the row key.
measuresrequiredMeasure[]The numeric fields to plot, one series each. Their inputs set the formatting.
resultsrequiredDataResponseYour query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array.
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.
showValueLabelsbooleantruePrint 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: BarChartOptionsClickArg) => voidCalled with the clicked bar’s dimension value and, on a time axis, its date range. Use it as a filter.BarChartOptionsClickArg = { dimensionValue: string | number | undefined; dimensionTimeRange: TimeRange; measureValues?: Record<string, unknown> | undefined; }
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.
yAxisMaxItemsnumberShow at most this many y values. The rest are added up into one “Other” value.

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

Set in a field’s inputs, beside the common options:

OptionTypeSet onDescription
colorstringmeasuresA colour for this measure’s bars, in place of the theme’s.
showValueAsPercentagebooleanmeasuresLabel each value with its share of the total. The tooltip shows both. Default: false.