UNPKG

@formant/ava-react

Version:

React components of AVA.

117 lines (116 loc) 7.75 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); } import { useState, useEffect } from 'react'; import { seedToken } from "../../theme"; import { scaleLinear } from "./scaleLinear"; // adjust to draw line width var SCALE_ADJUST = 2; var Line = /*#__PURE__*/function () { function Line(size, data) { _classCallCheck(this, Line); _defineProperty(this, "data", []); _defineProperty(this, "size", seedToken.fontSizeBase); _defineProperty(this, "height", this.size); _defineProperty(this, "width", this.getWidth()); _defineProperty(this, "xScale", void 0); _defineProperty(this, "yScale", void 0); _defineProperty(this, "points", []); this.size = size; this.data = data; this.compute(); } _createClass(Line, [{ key: "getWidth", value: function getWidth() { var _this$data; return Math.max(this.size * 2, ((_this$data = this.data) === null || _this$data === void 0 ? void 0 : _this$data.length) * 2); } }, { key: "compute", value: function compute() { var _this$data2, _this = this; if (!this.data) return; this.height = this.size; this.width = this.getWidth(); this.xScale = scaleLinear([0, this.width], [0, ((_this$data2 = this.data) === null || _this$data2 === void 0 ? void 0 : _this$data2.length) - 1]); var _ref = [Math.min.apply(Math, _toConsumableArray(this.data)), Math.max.apply(Math, _toConsumableArray(this.data))], min = _ref[0], max = _ref[1]; this.yScale = scaleLinear([SCALE_ADJUST, this.height - SCALE_ADJUST], [min, max]); this.points = this.data.map(function (item, index) { var _this$xScale, _this$yScale; return [(_this$xScale = _this.xScale) === null || _this$xScale === void 0 ? void 0 : _this$xScale.call(_this, index), _this.height - ((_this$yScale = _this.yScale) === null || _this$yScale === void 0 ? void 0 : _this$yScale.call(_this, item))]; }); } }, { key: "getLinePath", value: function getLinePath() { var _this$data3; if (!((_this$data3 = this.data) !== null && _this$data3 !== void 0 && _this$data3.length) || !this.xScale || !this.yScale) return null; var path = this.points.reduce(function (prev, _ref2, index) { var _ref3 = _slicedToArray(_ref2, 2), x = _ref3[0], y = _ref3[1]; if (index === 0) return "M".concat(x, " ").concat(y); return "".concat(prev, " L ").concat(x, " ").concat(y); }, ''); return path; } }, { key: "getPolygonPath", value: function getPolygonPath() { var _this$data4; if (!((_this$data4 = this.data) !== null && _this$data4 !== void 0 && _this$data4.length) || !this.xScale || !this.yScale) return null; var polygonPoints = _toConsumableArray(this.points); var last = this.points[this.points.length - 1]; polygonPoints.push([last[0], this.height]); polygonPoints.push([0, this.height]); var startPoint = this.points[0]; polygonPoints.push(startPoint); var path = polygonPoints.reduce(function (prev, _ref4) { var _ref5 = _slicedToArray(_ref4, 2), x = _ref5[0], y = _ref5[1]; return "".concat(prev, " ").concat(x, ",").concat(y); }, ''); return path; } }, { key: "getContainer", value: function getContainer() { return [this.width, this.height]; } }]); return Line; }(); export var useLineCompute = function useLineCompute(size, data) { var _useState = useState(new Line(size, data)), _useState2 = _slicedToArray(_useState, 2), line = _useState2[0], setLine = _useState2[1]; useEffect(function () { setLine(new Line(size, data)); }, [size, data]); return { width: line.getContainer()[0], height: line.getContainer()[1], linePath: line.getLinePath(), polygonPath: line.getPolygonPath() }; };