UNPKG

r-d3

Version:

D3 data, React rendering.

115 lines (80 loc) 5.46 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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; }; }(); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _d = require('d3'); var d3 = _interopRequireWildcard(_d); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _react = require('react'); var React = _interopRequireWildcard(_react); var _utils = require('./utils'); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 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 Arc = function (_React$Component) { _inherits(Arc, _React$Component); function Arc() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Arc); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Arc.__proto__ || Object.getPrototypeOf(Arc)).call.apply(_ref, [this].concat(args))), _this), _this._getChild = function () { var _this$props = _this.props, childOffset = _this$props.childOffset, children = _this$props.children, outerRadius = _this$props.outerRadius; (0, _invariant2.default)(React.Children.count(children) < 2, '`Arc` accepts at most one child.'); if (!children) { return null; } var arc = d3.arc().innerRadius(outerRadius - childOffset).outerRadius(outerRadius - childOffset); return React.cloneElement(children, _extends({}, children.props, { transform: _utils.translate.apply(undefined, _toConsumableArray(arc.centroid(_this.props))) })); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Arc, [{ key: 'render', value: function render() { var _props = this.props, children = _props.children, childOffset = _props.childOffset, className = _props.className, cornerRadius = _props.cornerRadius, endAngle = _props.endAngle, innerRadius = _props.innerRadius, outerRadius = _props.outerRadius, padAngle = _props.padAngle, padRadius = _props.padRadius, startAngle = _props.startAngle, otherProps = _objectWithoutProperties(_props, ['children', 'childOffset', 'className', 'cornerRadius', 'endAngle', 'innerRadius', 'outerRadius', 'padAngle', 'padRadius', 'startAngle']); var arc = d3.arc().innerRadius(innerRadius).outerRadius(outerRadius).startAngle(startAngle).endAngle(endAngle).cornerRadius(cornerRadius).padAngle(padAngle).padRadius(padRadius); return React.createElement( 'g', { className: (0, _classnames2.default)('arc', className) }, React.createElement('path', _extends({}, otherProps, { d: arc() })), this._getChild() ); } }]); return Arc; }(React.Component); Arc.defaultProps = { childOffset: 20, cornerRadius: 0, innerRadius: 0, padAngle: 0, padRadius: 0 }; exports.default = Arc;