Components · Bars

Bar and line combo

Bars and a line on independent axes, for volume against a rate or count.

Open in playground

Preview

Customers and churn rate by plan

Usage

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

const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { abbreviateLargeNumber: true },
});
const churnRate = measure({
  name: 'churn_rate',
  title: 'Churn rate',
  inputs: { suffix: '%', decimalPlaces: 1, useSecondaryAxis: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarLineChart
        title="Customers and churn rate by plan"
        dimension={plan}
        measures={[customers]}
        lineMeasures={[churnRate]}
        results={results(rows)}
        showValueLabels={false}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { plan: 'Starter', customers: 1283, churn_rate: 4.8 },
  { plan: 'Growth', customers: 1085, churn_rate: 2.7 },
  { plan: 'Enterprise', customers: 207, churn_rate: 1.1 },
];
  • useSecondaryAxis on a line measure gives the line its own scale, on the right.
  • showValueLabels labels the bars and showValueLabelsLine the line, both on by default. Here the bars are plain, so the line never runs through a bar’s label.

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

Examples

Axis titles

A caption on each value axis, so it’s clear which scale belongs to which series.

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

const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { abbreviateLargeNumber: true },
});
const churnRate = measure({
  name: 'churn_rate',
  title: 'Churn rate',
  inputs: { suffix: '%', decimalPlaces: 1, useSecondaryAxis: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 360 }}>
      <BarLineChart
        title="Customers and churn rate by plan"
        dimension={plan}
        measures={[customers]}
        lineMeasures={[churnRate]}
        results={results(rows)}
        showValueLabels={false}
        yAxisLabel="Customers"
        yAxisSecondaryLabel="Churn rate"
        yAxisSecondaryMin={0}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { plan: 'Starter', customers: 1283, churn_rate: 4.8 },
  { plan: 'Growth', customers: 1085, churn_rate: 2.7 },
  { plan: 'Enterprise', customers: 207, churn_rate: 1.1 },
];
<BarLineChart
  title="Customers and churn rate by plan"
  dimension={plan}
  measures={[customers]}
  lineMeasures={[churnRate]}
  results={results(rows)}
  showValueLabels={false}
  yAxisLabel="Customers"
  yAxisSecondaryLabel="Churn rate"
  yAxisSecondaryMin={0}
/>

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.
lineMeasuresMeasure[]Measures drawn as lines over the bars. Set useSecondaryAxis on a measure to give it the right-hand axis.
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.
showValueLabelsLinebooleantruePrint the line’s values too.
xAxisLabelstringA title under the x-axis.
yAxisLabelstringA title beside the y-axis.
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.
yAxisRangeMaxnumberFix the highest value on the y-axis. When unset, it fits the data.
reverseXAxisbooleanfalseReverse the order of the x-axis.
xAxisMaxItemsnumberShow at most this many x values. The rest are added up into one “Other” value.
yAxisSecondaryLabelstringA title beside the right-hand axis.
yAxisSecondaryMinnumberFix the lowest value on the right-hand axis.
yAxisSecondaryMaxnumberFix the highest value on the right-hand 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: BarLineChartClickArg) => voidCalled with the clicked bar’s dimension value and, on a time axis, its date range. Use it as a filter.BarLineChartClickArg = { dimensionValue: string | undefined; dimensionTimeRange: TimeRange; }
onLineClicked(args: BarLineChartClickArg) => voidCalled with the clicked point’s dimension value and date range.BarLineChartClickArg = { dimensionValue: string | undefined; dimensionTimeRange: 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.

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.
lineColorstringlineMeasuresA colour for this measure’s line, in place of the theme’s.
dashedLinebooleanlineMeasuresDraw the line dashed. Default: false.
connectGapsbooleanlineMeasuresDraw a missing value as zero, so the line carries on. When off, the line breaks at the gap. Default: false.
fillUnderLinebooleanlineMeasuresShade the area under the line.
useSecondaryAxisbooleanlineMeasuresPlot this measure against the right-hand axis, with its own scale.