Components · Pies

Donut chart

A pie with the centre cut out, for a lighter visual weight.

Open in playground

Preview

Customers by plan

Usage

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

const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({ name: 'customers', title: 'Customers' });

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 440 }}>
      <DonutChart
        title="Customers by plan"
        dimension={plan}
        measure={customers}
        results={results(rows)}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { plan: 'Starter', customers: 1283 },
  { plan: 'Growth', customers: 1085 },
  { plan: 'Enterprise', customers: 207 },
];

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

Examples

Percentages

Each segment labelled with its share of the total. showValueAsPercentage is a field option on the measure.

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

const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { showValueAsPercentage: true, decimalPlaces: 1 },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 440 }}>
      <DonutChart
        title="Customers by plan"
        dimension={plan}
        measure={customers}
        results={results(rows)}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { plan: 'Starter', customers: 1283 },
  { plan: 'Growth', customers: 1085 },
  { plan: 'Enterprise', customers: 207 },
];
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { showValueAsPercentage: true, decimalPlaces: 1 },
});

<DonutChart
  title="Customers by plan"
  dimension={plan}
  measure={customers}
  results={results(rows)}
/>

Minimal

No legend and no labels: shape only, with the detail in the tooltips.

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

const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({ name: 'customers', title: 'Customers' });

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 440 }}>
      <DonutChart
        title="Customers by plan"
        dimension={plan}
        measure={customers}
        results={results(rows)}
        showLegend={false}
        showValueLabels={false}
      />
    </div>
  </ChartsProvider>
);

const rows = [
  { plan: 'Starter', customers: 1283 },
  { plan: 'Growth', customers: 1085 },
  { plan: 'Enterprise', customers: 207 },
];
const plan = dimension({ name: 'plan', title: 'Plan' });
const customers = measure({ name: 'customers', title: 'Customers' });

<DonutChart
  title="Customers by plan"
  dimension={plan}
  measure={customers}
  results={results(rows)}
  showLegend={false}
  showValueLabels={false}
/>

Props

Not listed: showValueLabelsAsPercentage, which the type accepts and the component does not read. Keyboard and screen readers: Accessibility.

PropTypeDefaultDescription
dimensionrequiredDimensionThe field whose values label the axis or the segments. Its name is the row key.
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.
maxLegendItemsnumber10Show at most this many segments. The rest are added up into one “Other” segment.
showLegendbooleantrueShow the legend. Entries wrap onto more lines when they do not fit.
showTooltipsbooleantrueShow a formatted tooltip on hover.
showValueLabelsbooleantruePrint each value on the chart.
onSegmentClick(args: PieChartClickArg) => voidCalled 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; }
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
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
showValueAsPercentagebooleanmeasureLabel each value with its share of the total. The tooltip shows both. Default: false.