Chartist

Search

Find a guide, an API entry or an example.

Try

Pie chart with custom labels

Fruit slices labelled with an initial on small screens and the full name, pushed outside, on larger ones.

src/main.ts
import 'chartist/styles.css';
import {
  pieChart,
  type PieChartPresetOptions,
  type PresetResponsiveOptions
} from 'chartist';

const data = {
  labels: ['Bananas', 'Apples', 'Grapes'],
  series: [20, 15, 40]
};

const options: PieChartPresetOptions = {
  labelInterpolationFnc: value => String(value)[0]
};

const responsiveOptions: PresetResponsiveOptions<PieChartPresetOptions> = [
  [
    'screen and (min-width: 640px)',
    {
      chartPadding: 30,
      labelOffset: 100,
      labelDirection: 'explode',
      labelInterpolationFnc: value => value
    }
  ],
  [
    'screen and (min-width: 1024px)',
    {
      labelOffset: 80,
      chartPadding: 20
    }
  ]
];

pieChart('#chart', data, options, responsiveOptions);
Live
Open in the playgroundView as MarkdownSwitch to 1.x above the code to see the same chart written with the Chartist 1.x classes.