react-simple-pie-chart
Version:
A React component to generate simple pie charts.
147 lines (113 loc) • 5.04 kB
JavaScript
;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var size = 100;
var radCircumference = Math.PI * 2;
var center = size / 2;
var radius = center - 1; // padding to prevent clipping
/**
* @param {Object[]} slices
* @return {Object[]}
*/
function renderPaths(slices) {
var total = slices.reduce(function (totalValue, _ref) {
var value = _ref.value;
return totalValue + value;
}, 0);
var radSegment = 0;
var lastX = radius;
var lastY = 0;
return slices.map(function (_ref2, index) {
var color = _ref2.color;
var value = _ref2.value;
// Should we just draw a circle?
if (value === total) {
return _react2.default.createElement('circle', {
r: radius,
cx: center,
cy: center,
fill: color,
key: index
});
}
if (value === 0) {
return;
}
var valuePercentage = value / total;
// Should the arc go the long way round?
var longArc = valuePercentage <= 0.5 ? 0 : 1;
radSegment += valuePercentage * radCircumference;
var nextX = Math.cos(radSegment) * radius;
var nextY = Math.sin(radSegment) * radius;
// d is a string that describes the path of the slice.
// The weirdly placed minus signs [eg, (-(lastY))] are due to the fact
// that our calculations are for a graph with positive Y values going up,
// but on the screen positive Y values go down.
var d = ['M ' + center + ',' + center, 'l ' + lastX + ',' + -lastY, 'a' + radius + ',' + radius, '0', longArc + ',0', nextX - lastX + ',' + -(nextY - lastY), 'z'].join(' ');
lastX = nextX;
lastY = nextY;
return _react2.default.createElement('path', { d: d, fill: color, key: index });
});
}
/**
* Generates an SVG pie chart.
* @see {http://wiki.scribus.net/canvas/Making_a_Pie_Chart}
*/
var PieChart = (function (_React$Component) {
_inherits(PieChart, _React$Component);
function PieChart() {
_classCallCheck(this, PieChart);
return _possibleConstructorReturn(this, Object.getPrototypeOf(PieChart).apply(this, arguments));
}
_createClass(PieChart, [{
key: 'render',
/**
* @return {Object}
*/
value: function render() {
var border = this.props.borderWidth > 0 ? _react2.default.createElement('circle', {
cx: center,
cy: center,
r: radius,
stroke: this.props.borderColor,
strokeWidth: this.props.borderWidth,
fill: 'transparent'
}) : null;
return _react2.default.createElement(
'svg',
{ viewBox: '0 0 ' + size + ' ' + size },
_react2.default.createElement(
'g',
{ transform: 'rotate(-90 ' + center + ' ' + center + ')' },
renderPaths(this.props.slices)
),
border
);
}
}]);
return PieChart;
})(_react2.default.Component);
exports.default = PieChart;
PieChart.propTypes = {
slices: _propTypes2.default.arrayOf(_propTypes2.default.shape({
color: _propTypes2.default.string.isRequired, // hex color
value: _propTypes2.default.number.isRequired
})).isRequired,
borderColor: _propTypes2.default.string,
borderWidth: _propTypes2.default.number
};
PieChart.defaultProps = {
borderColor: '#FFFFFF',
borderWidth: 0
};
module.exports = exports['default'];