Skip to main content
Version: 1.1.0

Pie / Donut Chart

A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. You can also make a variation of pieChart ( donutChart) from pieChart API.

Live Editor

Usage

import { PieChart } from "@k2/rv-viz";

2 . Add data or URL to PieChart component.

<PieChart data={PieChartData} />
// or
<PieChart url="server/api/data" />

Example

PieChart: Demo

import { PieChart } from "@k2/rv-viz";
// Mandatory
const data = [
{
label: "Global Admin",
value: 1,
},
{
label: "Custom Admin",
value: 7,
},
{
label: "Users",
value: 8,
},
{
label: "External Users",
value: 10,
},
];
function RenderChart() {
return (
<PieChart
data={data}
radial={{
anglePadding: 2,
}}
/>
);
}

DonutChart: Demo

import { PieChart } from "@k2/rv-viz";
// Mandatory
const data = [
{
label: "Global Admin",
value: 1,
},
{
label: "Custom Admin",
value: 7,
},
{
label: "Users",
value: 8,
},
{
label: "External Users",
value: 10,
},
];
function RenderChart() {
return (
<PieChart
data={data}
radial={{
innerRadius: 0.8,
}}
/>
);
}

Click here for more variations of pie chart.

Props

Note: External props are mostly derived from react-vis & K2. All external props are linked. You can click highlighted props to get more details from official site.

PropertyTypeRequiredDefault valueDescription
dataPieChartData[]yesData for the chart.
radialRadialPropsnoNoneAdjust radial properties.
titleJSX.Element | stringnoNoneTitle of the chart.
animatebooleannotrueEnable the animation.
colors{[theme-name: e.g "light" or "dark"] : string[] | GradientColor[]noNoneColors of chart in different theme modes.
labelboolean | LabelComponent notrueEnable or customize the labels
labelTickWidthnumberno1.25Lenth of label tick.
centerLabelboolean | CenterLabelComponent notrueEnable or customize the Center label.
legendsboolean | LegendsPropsnotrueEnable or customize the legends.
tooltipboolean | TooltipPropsnotrueEnable or customize the tooltip.
widthnumberno300Width of chart.
heightnumberno300Height of chart.
onClick(event: { data:TransformedData}) => voidnoNoneCallback for Slice click.
styleCSSPropertiesnoNoneStyles applied to the root element of chart.
classNamestringnoNoneClass applied to the root element of chart.
classesClassesnoNoneClasses of chart.

PieChart is wrapped with fetchData HoC, to perform data handling tasks, which adds support for some additional props.

Custom Types

PieChartData

Here is the format of the data accepted by PieChart.

export type PieChartData = { label: string; value: number };

TransformedData

export type TransformedData = PieChartData & {
radius0: number;
radius: number;
angle0: number;
angle: number;
};

RadialProps

This is a prop to adjust anglePadding and innerRadius of the chart.

export type RadialProps = {
innerRadius?: number;
anglePadding?: number;
};

LabelComponent

This is a type of custom label component. You can create a custom label component by this.

export type LabelComponent = (props: {
data: PieChartData;
total: number;
}) => JSX.Element;
};

CenterLabelComponent

This is type of custom centerlabel component. You can create a custom centerlabel component by this.

export type CenterLabelComponent = (props: {
data: PieChartData[];
total: number;
}) => JSX.Element;

GradientColor

Type of GradientColor object.

type GradientColor = {
name: string,
startingColor: string,
endingColor: string,
startingColorStopOpacity?: number,
endingColorStopOpacity?: number,
x1?: string,
x2?: string,
y1?: string,
y2?: string,
};

Classes

Classes enable customization and styling of chart elements through their classes.

ClassNameTypeRequiredDefault valueDescription
rootstringnoNoneApplied to root element of chart.
headerstringnoNoneApplied to header of chart.
titlestringnoNoneApplied to title of chart.
legendsstringnoNoneApplied to legends of chart.
tooltipstringnoNoneApplied to tooltip of chart.
xyplotstringnoNoneApplied to svg component of chart.
arcstringnoNoneApplied to arc path of chart.
labelstringnoNoneApplied to label of chart.
centerlabelstringnoNoneApplied to centerLabel of chart.

Please see Class Specificity for correctly defining it.