5.0.5 โ€ข Published 4 years ago

kien-react-minimal-pie-chart v5.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

React minimal pie chart

Build Status Npm version Coveralls Bundle size

Lightweight React SVG pie charts, with versatile options and CSS animation included. < 3kB gzipped. ๐Ÿ‘Demoย ๐Ÿ‘.

import PieChart from 'react-minimal-pie-chart';

<PieChart
  data={[
    { title: 'One', value: 10, color: '#E38627' },
    { title: 'Two', value: 15, color: '#C13C37' },
    { title: 'Three', value: 20, color: '#6A2135' },
  ]}
/>;

Installation

npm install react-minimal-pie-chart

If you don't use a package manager, react-minimal-pie-chart exposes also an UMD module ready for the browser.

https://unpkg.com/react-minimal-pie-chart/dist/index.js

Why?

Because Recharts is awesome, but when you just need a simple pie/donought chart, 3kB of code are usually enough.

Features

Options

PropertyTypeDescriptionDefault
data (required)ArrayThe source data which each element is a segment.-
cxNumberThe x-coordinate of center. The value is the percentage of the component width50
cyNumberThe y-coordinate of center. The value is the percentage of the component height50
ratioNumberThe ratio of rendered svg element1
startAngleNumberThe start angle of first sector0
lengthAngleNumberThe total angle taken by the chart (can be negative to make the chart clockwise!)360
totalValueNumberThe total value represented by the full chart-
radiusNumberThe radius of the pie. The value is the percentage of the component's width50
lineWidthNumberThe width of the line representing each sector. The value is the percentage of chart's radio (100 === full pie)100
paddingAngleNumberThe angle between two sectors-
roundedBoolRound line caps of each sectorfalse
segmentsStyleObjectStyle object assigned each segment-
backgroundStringSegments' background color-
animateBoolAnimate sectors on component mountfalse
animationDurationNumberAnimation duration in ms500
animationEasingStringAnimation CSS easing"ease-out"
revealNumberTurn on CSS animation and reveal just a percentage of each segment-
injectSvgFunctionInject <svg> element with the output of the provided function (eg. gradients)-
labelBoolean, ReactElement, FunctionIf true set, labels will be drawn automatically. If ReactElement set, the option can be the custom label element. If set a function, the function will be called to render customized label.false
labelPositionNumberLabel position from origin. The value is the percentage of chart's radio (50 === middle point)50
labelStyleObjectStyle object assigned by default to each label-
onClickFunctionCustom event handler of onClick on each sector : (event, data, dataIndex) => {}-
onMouseOverFunctionCustom event handler of onMouseOver on each sector : (event, data, dataIndex) => {}-
onMouseOutFunctionCustom event handler of onMouseOut on each sector : (event, data, dataIndex) => {}-

About props.data

props.data expects the following data:

interface dataProps: {
  title?: string | number,
  value: number,
  color: string,
  key?: string | number,
  style?: {[key: string]: string | number},
};

Each data entry can optionally accept:

  • a key property just in case items' indexes weren't enough
  • a style property targeting the corresponding chart segment

Custom labels with label prop

When label is a function or ReactElement, the provided entity will be called with the following labelProps object respectively as argument or as props:

interface labelProps: {
  key: string,
  x: number,
  y: number,
  dx: number,
  dy: number,
  textAnchor: string,
  data: {
    // props.data entry extended with:
    degrees: number,
    startOffset: number,
    percentage: number,
  }[],
  dataIndex: number,
  color: string,
  style: {[key: string]: string | number},
};

label as function

The provided function is called with labelProps as argument and is supposed to return the string, number or element rendered as label content.

<PieChart label={(labelProps: labelProps) => string | number | ReactElement} />

label as React element

The provided React element will get labelProps object as props.

<PieChart label={<CustomLabel />} />

See some examples in the demo source.

How to

User interactions with the chart

See demo and its source.

Browsers support

The main requirement of this library is an accurate rendering of SVG Stroke properties.

Not supportedPartially supported
IE โ‰ค 10IE 11
Edge (Upcoming Blink adoption should make it fully supported)

A Math.sign polyfill is needed to support legacy browsers.

Misc

How svg arc paths work?

https://codepen.io/lingtalfi/pen/yaLWJG

Size comparison

size (by Bundlefobia)size (by size-limit)
react-minimal-pie-chartBundle size: React minimal pie chart2.8 KB (v4.0.0)
rechartBundle size: Recharts93 KB (v1.5.0)
victory-pieBundle size: Victory pie54 KB (v32.2.0)

Sizes in the third column are calculated with a "real-world" setup: see source repo.

Todo's

  • Make a device/browser compatibility table
  • Consider moving storybook deployment to CI
  • Configure Babel to not inject the _extend utility in compiled artifact
  • Add .browserslistrc to get rid of some Babel helpers

Contributors

Thanks to you all (emoji key):