Simple-As-Pie is a simple and lightweight open-source React.js module for displaying SVG pie charts with a dynamic data set.
View Demo
·
Report Bug
·
Request Feature
Table of Contents
For one of my own projects I needed to create multiple pie charts to display within a single SVG element, within a React app.
Whilst I found a few examples that I could use, they were a bit limited in that they would only show one sgement at once.
Rather than spending yet more time searching for a solution that met my needs, I instead decided to create my own.
Within your React app's directory, install the plugin via npm or yarn
-
npm
npm install simple-as-pie
-
yarn
yarn add simple-as-pie
Import the module into your component
import { PieChart, DataSet } from 'simple-as-pie';
Construct the data for the segments
const segmentData: DataSet[] = [
{
value: 24,
colour: "red",
events: {
onClick: myOnClickHandler
}
},
{
value: 76,
colour: "blue"
}
];
Create an SVG component, and call the component within it
<svg width="200px" height="200px">
<PieChart x={100} y={100} data={segmentData} radius={75} />
</svg>
This library exports two members:
- The "DataSet" interface
- The "PieChart" component
The DataSet represents the data in the pie chart, and has three properties:
- value - a number representing the value of the piece of data
- colour - a string representation of what the colour for that segment should be in the chart
- events - an object that stores event handlers for the supported events. Currently these are Mouse, Pointer, Keyboard, Focus and Touch events.
Since DataSet is an interface it can be extended to be used in other ways within your project.
The PieChart component accepts the following properties:
- data - an array of DataSet objects
- x - a number for the x co-ordinate of the centre of the Pie Chart
- y - a number for the y co-ordinate of the centre of the Pie Chart
- radius - a number representing the radius of the Pie Chart
- backgroundColour - an optional string representation for the background colour of the chart
There are no styles or animations provided out of the box, but the following classes are on the various elements:
- The Pie Chart component - a g (group) element with the class "SimplePieChart"
- The background - a circle with the class "SimplePieChart-background-segment"
- Each segment of the chart - a circle if there is only one, paths otherwise. They have the class "SimplePieChart-segment"
There is an interactive demo available here
- [x] Enable background colour
- [x] Switch to paths rather than circle elements
- [x] Enable some functions
- [ ] Enable all appropriate functions
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Made with contrib.rocks.
Distributed under the MIT License. See LICENSE.txt
for more information.
Project Link: https://github.com/Arad1el/simple-as-pie/
- The initial version of this project was inspired be the logic of Lea Verou
- Further development leans on the logic of David Gilbertson
- This readme template was adapted from Othneil Drew's template