Pie chart
Share of a total across a small number of categories.
Preview
Revenue by industry
Usage
import { ChartsProvider, PieChart, dimension, measure, results } from 'em-kit';
const industry = dimension({ name: 'industry', title: 'Industry' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 440 }}>
<PieChart
title="Revenue by industry"
dimension={industry}
measure={revenue}
results={results(rows)}
/>
</div>
</ChartsProvider>
);
const rows = [
{ industry: 'Health', revenue: 239005 },
{ industry: 'Media', revenue: 150918 },
{ industry: 'Retail', revenue: 180795 },
{ industry: 'Tech', revenue: 266759 },
];Install with npm install em-kit. Needs <ChartsProvider> once and a parent with a height: Quick start.
Examples
Percentages
Each slice labelled with its share of the total. showValueAsPercentage on the measure works out the shares from your rows; no extra query. decimalPlaces rounds them.
Show full code (28 lines)
import { ChartsProvider, PieChart, dimension, measure, results } from 'em-kit';
const industry = dimension({ name: 'industry', title: 'Industry' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', decimalPlaces: 1, showValueAsPercentage: true },
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 440 }}>
<PieChart
title="Revenue by industry"
dimension={industry}
measure={revenue}
results={results(rows)}
/>
</div>
</ChartsProvider>
);
const rows = [
{ industry: 'Health', revenue: 239005 },
{ industry: 'Media', revenue: 150918 },
{ industry: 'Retail', revenue: 180795 },
{ industry: 'Tech', revenue: 266759 },
];const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', decimalPlaces: 1, showValueAsPercentage: true },
});
<PieChart
title="Revenue by industry"
dimension={industry}
measure={revenue}
results={results(rows)}
/>Long tail as “Other”
At most three slices: the first two rows, and the rest added up into one “Other” slice. maxLegendItems keeps your rows’ order, so sort the query largest first.
Show full code (29 lines)
import { ChartsProvider, PieChart, dimension, measure, results } from 'em-kit';
const industry = dimension({ name: 'industry', title: 'Industry' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 440 }}>
<PieChart
title="Revenue by industry"
dimension={industry}
measure={revenue}
results={results(rows)}
maxLegendItems={3}
/>
</div>
</ChartsProvider>
);
const rows = [
{ industry: 'Tech', revenue: 266759 },
{ industry: 'Health', revenue: 239005 },
{ industry: 'Retail', revenue: 180795 },
{ industry: 'Media', revenue: 150918 },
];const industry = dimension({ name: 'industry', title: 'Industry' });
const revenue = measure({
name: 'revenue',
title: 'Revenue',
inputs: { currency: 'USD', abbreviateLargeNumber: true },
});
<PieChart
title="Revenue by industry"
dimension={industry}
measure={revenue}
results={results(rows)}
maxLegendItems={3}
/>Props
Not listed: showValueLabelsAsPercentage, which the type accepts and the component does not read. 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. | |
measurerequired | Measure | The numeric field to plot. Its inputs (prefix, suffix, decimalPlaces) set the formatting. | |
resultsrequired | DataResponse | Your query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array. | |
maxLegendItems | number | 10 | Show at most this many segments. The rest are added up into one “Other” segment. |
showLegend | boolean | true | Show the legend. Entries wrap onto more lines when they do not fit. |
showTooltips | boolean | true | Show a formatted tooltip on hover. |
showValueLabels | boolean | true | Print each value on the chart. |
onSegmentClick | ( | Called with the clicked segment’s dimension value. Use it as a filter. A click on the “Other” segment reports the first of the rows it adds up, so ignore that one.PieChartClickArg = { 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 |
|---|---|
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 |
|---|---|---|---|
showValueAsPercentage | boolean | measure | Label each value with its share of the total. The tooltip shows both. Default: false. |