UNPKG

@stokr/components-library

Version:

STOKR - Components Library

153 lines (149 loc) 6.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.DonutChart = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var d3 = _interopRequireWildcard(require("d3")); var _DonutChart2 = require("./DonutChart.styles"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class DonutChart extends _react.PureComponent { constructor() { super(...arguments); _defineProperty(this, "state", { data: [], key: 0 }); _defineProperty(this, "createChart", () => { if (!this.chartEl) return; const { data } = this.state; const width = 320; const height = 320; const pad = 10; const thickness = 14; const outerRadius = width / 2; const innerRadius = outerRadius - thickness; const padAngle = pad / (outerRadius - thickness / 2); const customColors = data.map(item => item.color); const color = customColors[0] ? d3.scaleOrdinal(customColors) : d3.scaleOrdinal(d3.schemeCategory10); const svg = d3.select(this.chartEl).append('svg').attr('viewBox', "0 0 ".concat(width, " ").concat(height)); const chart = svg.append('g').attr('transform', "translate(".concat(width / 2, ", ").concat(height / 2, ")")); const pie = d3.pie().value(d => d.value).sort(null).padAngle(padAngle); const arc = d3.arc().outerRadius(outerRadius).innerRadius(innerRadius); function arcTween(a) { const interp = d3.interpolate(this._current, a); this._current = interp(1); return time => arc(interp(time)); } function setD(d) { this._current = d; } this.drawDate = dataSet => { // Join new data const path = chart.selectAll('path').data(pie(dataSet)); // Update existing arcs path.transition().ease(d3.easeQuadOut).duration(1000).attrTween('d', arcTween); // Enter new arcs path.enter().append('path').attr('fill', (d, i) => color(i)).attr('d', arc).each(setD); }; // set init data this.drawDate(data.map(datum => _objectSpread(_objectSpread({}, datum), {}, { value: 0 }))); }); _defineProperty(this, "drawChart", () => { const { data } = this.props; this.drawDate(data); this.drawn = true; }); _defineProperty(this, "onScroll", e => { if (this.drawn) return; if (this.chartEl.getBoundingClientRect().top < window.innerHeight) { this.drawChart(); } }); } componentDidMount() { const { data } = this.props; this.setState({ data }, () => { this.createChart(); window.addEventListener('scroll', this.onScroll); this.onScroll(); }); } componentDidUpdate(prevProps) { const { key } = this.state; const { data: newData } = this.props; const { data: prevData } = prevProps; if (newData && prevData && JSON.stringify(newData) !== JSON.stringify(prevData)) { this.setState({ data: newData }, () => { this.drawn = false; window.removeEventListener('scroll', this.onScroll); this.setState({ key: key + 1 }, () => { this.createChart(); window.addEventListener('scroll', this.onScroll); this.onScroll(); }); }); } } componentWillUnmount() { window.removeEventListener('scroll', this.onScroll); } render() { const { children, center } = this.props; const { key } = this.state; return /*#__PURE__*/_react.default.createElement(_DonutChart2.Chart, { ref: el => { this.chartEl = el; }, center: center, key: key }, /*#__PURE__*/_react.default.createElement(_DonutChart2.Content, null, children)); } } exports.DonutChart = DonutChart; DonutChart.propTypes = { children: _propTypes.default.node, data: _propTypes.default.arrayOf(_propTypes.default.shape({ value: _propTypes.default.number.isRequired, name: _propTypes.default.string, color: _propTypes.default.string })).isRequired, center: _propTypes.default.bool }; DonutChart.defaultProps = { children: null, center: false }; var _default = exports.default = DonutChart;