Socket
Socket
Sign inDemoInstall

react-minimal-pie-chart

Package Overview
Dependencies
8
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-minimal-pie-chart

Lightweight but versatile SVG pie/donut charts for React


Version published
Weekly downloads
84K
decreased by-9.03%
Maintainers
1
Install size
322 kB
Created
Weekly downloads
 

Changelog

Source

7.1.0

New Features

  • Add onBlur, onFocus, onKeyDown callbacks
  • Add segmentsTabIndex to append a tabindex prop to segment paths

Bugfixes

  • Fix regression bug consisting of stroke-dasharray and stroke-dashoffset attributes being evaluated and appended even when there is no animation

Readme

Source

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' },
  ]}
/>;

Why?

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

Size
by Bundlefobia
Benchmark Size *Loading time
on a slow 3g *
react-minimal-pie-chart
v7.0.0
Bundle size: React minimal pie chart2.3 KB46 ms
rechart
v1.8.5
Bundle size: Recharts92.6 KB1900 ms
victory-pie
v34
Bundle size: Victory pie50.5 KB1100 ms

* Benchmark carried out with size-limit with a "real-world" setup: see benchmark repo.

Note: what matter here are not absolute values but the relation between magnitudes.

Features

  • < 3kB gzipped
  • Versatile: Pie, Donut, Loading, Completion charts (see Demo)
  • Customizable chart labels
  • Customizable CSS animations
  • Typescript types included
  • No dependencies (except for React's prop-types)

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

Options

PropertyTypeDescriptionDefault
data (required)ArraySource data which each element is a segment-
lineWidthNumber (%)Line width of each segment. Percentage of chart's radio (100 === full pie)100
startAngleNumberStart angle of first segment0
lengthAngleNumberTotal angle taken by the chart (can be negative to make the chart clockwise!)360
totalValueNumberTotal value represented by the full chart-
paddingAngleNumberAngle between two segments-
roundedBoolRound line caps of each segmentfalse
segmentsStyleObjectStyle object assigned to each segment-
segmentsTabIndexNumbertabindex attribute assigned to segments-
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 labelfalse
labelPositionNumber (%)Label position from origin. Percentage of chart's radio (50 === middle point)50
labelStyleObjectStyle object assigned to each label-
animateBoolAnimate segments on component mountfalse
animationDurationNumberAnimation duration in ms500
animationEasingStringA CSS easing function"ease-out"
revealNumber (%)Turn on CSS animation and reveal just a percentage of each segment-
backgroundStringSegments' background color-
injectSvgFunctionInject <svg> element with the output of the provided function (eg. gradients)-
radiusNumber (%)Radius of the pie. Percentage of component's width50
cxNumber (%)x-coordinate of center. Percentage of component's width50
cyNumber (%)y-coordinate of center. Percentage of component's height50
viewBoxSizeArray of NumbersWidth and Height of SVG viewBox attribute[100, 100]
onBlurFunctiononBlur event handler for each segment: (event, data, dataIndex) => void-
onClickFunctiononClick event handler for each segment: (event, data, dataIndex) => void-
onFocusFunctiononFocus event handler for each segment: (event, data, dataIndex) => void-
onKeyDownFunctiononKeyDown event handler for each segment: (event, data, dataIndex) => void-
onMouseOutFunctiononMouseOut event handler for each segment: (event, data, dataIndex) => void-
onMouseOverFunctiononMouseOver event handler for each segment: (event, data, dataIndex) => void-

About props.data

props.data expects the following array of entries:

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

Each data entry optionally accepts:

  • 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:

type 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.

Here is an updated browsers support list 🔍.

Please consider that a Math.sign polyfill is needed to support legacy browsers.

Misc

How svg arc paths work?

How SVG animations work?

This library uses the stroke-dasharray + stroke-dashoffset animation strategy described here.

Todo's

  • 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
  • Handle prop-types / TS types duplication

Contributors

Thanks to you all (emoji key):


Andrea Carraro

💻 📖 🚇 ⚠️ 👀

Stephane Rufer

🐛 💻

Jørgen Aaberg

💻

Tobiah Rex

🐛

Edward Xiao

🐛

David Konsumer

💻 📖 💡 🤔

Ori

🤔

Emmanouil Konstantinidis

🐛

yuruc

💻

luca-esse

🐛

Oscar Mendoza

🐛 💻

damien-git

🐛 🤔

Vianney Stroebel

🐛 🤔

Maxime Zielony

🐛 💻

Raz Kedem

🐛

Blocksmith

🐛

Keywords

FAQs

Last updated on 06 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc