UNPKG

r-d3

Version:

D3 data, React rendering.

123 lines (91 loc) 4.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _reactDom = require('react-dom'); 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 _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 Axis = function (_React$Component) { _inherits(Axis, _React$Component); function Axis() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Axis); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Axis.__proto__ || Object.getPrototypeOf(Axis)).call.apply(_ref, [this].concat(args))), _this), _this._renderAxis = function () { var _this$props = _this.props, orient = _this$props.orient, scale = _this$props.scale, ticks = _this$props.ticks, tickFormat = _this$props.tickFormat, tickSize = _this$props.tickSize, tickValues = _this$props.tickValues; var axis = void 0; switch (orient) { case 'bottom': axis = d3.axisBottom(scale); break; case 'left': axis = d3.axisLeft(scale); break; case 'right': axis = d3.axisRight(scale); break; case 'top': axis = d3.axisTop(scale); break; } (0, _invariant2.default)(axis, 'You must set the `orient` prop to one of: `bottom`, `left`, `right`, ' + 'or `top`'); axis.ticks(ticks).tickFormat(tickFormat).tickSize(tickSize); if (tickValues) { axis.tickValues(tickValues); } d3.select((0, _reactDom.findDOMNode)(_this)).call(axis); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Axis, [{ key: 'componentDidMount', value: function componentDidMount() { this._renderAxis(); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this._renderAxis(); } }, { key: 'render', value: function render() { var _props = this.props, children = _props.children, className = _props.className, transform = _props.transform; return React.createElement( 'g', { className: (0, _classnames2.default)('axis', className), transform: transform }, children ); } }]); return Axis; }(React.Component); Axis.defaultProps = { tickSize: 6 }; exports.default = Axis;