Bar and line combo
Bars and a line on independent axes, for volume against a rate or count.
Preview
Customers and churn rate by plan
Usage
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 },
];useSecondaryAxison a line measure gives the line its own scale, on the right.showValueLabelslabels the bars andshowValueLabelsLinethe 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.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
dimensionrequired | Dimension | The field whose values label the axis or the segments. Its name is the row key. | |
measuresrequired | Measure[] | The numeric fields to plot, one series each. Their inputs set the formatting. | |
resultsrequired | DataResponse | Your query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array. | |
lineMeasures | Measure[] | Measures drawn as lines over the bars. Set useSecondaryAxis on a measure to give it the right-hand axis. | |
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. |
showValueLabels | boolean | true | Print each value on the chart. |
showValueLabelsLine | boolean | true | Print the line’s values too. |
xAxisLabel | string | A title under the x-axis. | |
yAxisLabel | string | A title beside the y-axis. | |
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. | |
yAxisRangeMax | number | Fix the highest value on the y-axis. When unset, it fits the data. | |
reverseXAxis | boolean | false | Reverse the order of the x-axis. |
xAxisMaxItems | number | Show at most this many x values. The rest are added up into one “Other” value. | |
yAxisSecondaryLabel | string | A title beside the right-hand axis. | |
yAxisSecondaryMin | number | Fix the lowest value on the right-hand axis. | |
yAxisSecondaryMax | number | Fix the highest value on the right-hand 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.BarLineChartClickArg = { dimensionValue: string | undefined; dimensionTimeRange: TimeRange; } | |
onLineClicked | ( | Called with the clicked point’s dimension value and date range.BarLineChartClickArg = { dimensionValue: string | undefined; dimensionTimeRange: 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. |
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
Set in a field’s inputs, beside the common options:
| Option | Type | Set on | Description |
|---|---|---|---|
color | string | measures | A colour for this measure’s bars, in place of the theme’s. |
showValueAsPercentage | boolean | measures | Label each value with its share of the total. The tooltip shows both. Default: false. |
lineColor | string | lineMeasures | A colour for this measure’s line, in place of the theme’s. |
dashedLine | boolean | lineMeasures | Draw the line dashed. Default: false. |
connectGaps | boolean | lineMeasures | Draw a missing value as zero, so the line carries on. When off, the line breaks at the gap. Default: false. |
fillUnderLine | boolean | lineMeasures | Shade the area under the line. |
useSecondaryAxis | boolean | lineMeasures | Plot this measure against the right-hand axis, with its own scale. |