UNPKG

recharts

Version:
330 lines (257 loc) 15.6 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } import _isNil from "lodash/isNil"; function _extends() { _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; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(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 _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 _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 _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React, { Component } from 'react'; import reduceCSSCalc from 'reduce-css-calc'; import classNames from 'classnames'; import { isNumber, isNumOrStr } from '../util/DataUtils'; import { Global } from '../util/Global'; import { filterProps } from '../util/types'; import { getStringSize } from '../util/DOMUtils'; var BREAKING_SPACES = /[ \f\n\r\t\v\u2028\u2029]+/; var calculateWordWidths = function calculateWordWidths(props) { try { var words = []; if (!_isNil(props.children)) { if (props.breakAll) { words = props.children.toString().split(''); } else { words = props.children.toString().split(BREAKING_SPACES); } } var wordsWithComputedWidth = words.map(function (word) { return { word: word, width: getStringSize(word, props.style).width }; }); var spaceWidth = props.breakAll ? 0 : getStringSize("\xA0", props.style).width; return { wordsWithComputedWidth: wordsWithComputedWidth, spaceWidth: spaceWidth }; } catch (e) { return null; } }; var calculateWordsByLines = function calculateWordsByLines(props, initialWordsWithComputedWith, spaceWidth, lineWidth, scaleToFit) { var shouldLimitLines = isNumber(props.maxLines); var text = props.children; var calculate = function calculate() { var words = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return words.reduce(function (result, _ref) { var word = _ref.word, width = _ref.width; var currentLine = result[result.length - 1]; if (currentLine && (lineWidth == null || scaleToFit || currentLine.width + width + spaceWidth < lineWidth)) { // Word can be added to an existing line currentLine.words.push(word); currentLine.width += width + spaceWidth; } else { // Add first word to line or word is too long to scaleToFit on existing line var newLine = { words: [word], width: width }; result.push(newLine); } return result; }, []); }; var originalResult = calculate(initialWordsWithComputedWith); var findLongestLine = function findLongestLine(words) { return words.reduce(function (a, b) { return a.width > b.width ? a : b; }); }; if (!shouldLimitLines) { return originalResult; } var suffix = '…'; var checkOverflow = function checkOverflow(index) { var tempText = text.slice(0, index); var words = calculateWordWidths(_objectSpread(_objectSpread({}, props), {}, { children: tempText + suffix })).wordsWithComputedWidth; var result = calculate(words); var doesOverflow = result.length > props.maxLines || findLongestLine(result).width > lineWidth; return [doesOverflow, result]; }; var start = 0; var end = text.length - 1; var iterations = 0; var trimmedResult; while (start <= end && iterations <= text.length - 1) { var middle = Math.floor((start + end) / 2); var prev = middle - 1; var _checkOverflow = checkOverflow(prev), _checkOverflow2 = _slicedToArray(_checkOverflow, 2), doesPrevOverflow = _checkOverflow2[0], result = _checkOverflow2[1]; var _checkOverflow3 = checkOverflow(middle), _checkOverflow4 = _slicedToArray(_checkOverflow3, 1), doesMiddleOverflow = _checkOverflow4[0]; if (!doesPrevOverflow && !doesMiddleOverflow) { start = middle + 1; } if (doesPrevOverflow && doesMiddleOverflow) { end = middle - 1; } if (!doesPrevOverflow && doesMiddleOverflow) { trimmedResult = result; break; } iterations++; } // Fallback to originalResult (result without trimming) if we cannot find the // where to trim. This should not happen :tm: return trimmedResult || originalResult; }; var getWordsWithoutCalculate = function getWordsWithoutCalculate(children) { var words = !_isNil(children) ? children.toString().split(BREAKING_SPACES) : []; return [{ words: words }]; }; var getWordsByLines = function getWordsByLines(props, needCalculate) { // Only perform calculations if using features that require them (multiline, scaleToFit) if ((props.width || props.scaleToFit) && !Global.isSsr) { var wordsWithComputedWidth, spaceWidth; if (needCalculate) { var wordWidths = calculateWordWidths(props); if (wordWidths) { var wcw = wordWidths.wordsWithComputedWidth, sw = wordWidths.spaceWidth; wordsWithComputedWidth = wcw; spaceWidth = sw; } else { return getWordsWithoutCalculate(props.children); } return calculateWordsByLines(props, wordsWithComputedWidth, spaceWidth, props.width, props.scaleToFit); } } return getWordsWithoutCalculate(props.children); }; export var Text = /*#__PURE__*/function (_Component) { _inherits(Text, _Component); var _super = _createSuper(Text); function Text() { var _this; _classCallCheck(this, Text); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _this.state = {}; return _this; } _createClass(Text, [{ key: "render", value: function render() { var _this$props = this.props, dx = _this$props.dx, dy = _this$props.dy, textAnchor = _this$props.textAnchor, verticalAnchor = _this$props.verticalAnchor, scaleToFit = _this$props.scaleToFit, angle = _this$props.angle, lineHeight = _this$props.lineHeight, capHeight = _this$props.capHeight, className = _this$props.className, breakAll = _this$props.breakAll, textProps = _objectWithoutProperties(_this$props, ["dx", "dy", "textAnchor", "verticalAnchor", "scaleToFit", "angle", "lineHeight", "capHeight", "className", "breakAll"]); var wordsByLines = this.state.wordsByLines; if (!isNumOrStr(textProps.x) || !isNumOrStr(textProps.y)) { return null; } var x = textProps.x + (isNumber(dx) ? dx : 0); var y = textProps.y + (isNumber(dy) ? dy : 0); var startDy; switch (verticalAnchor) { case 'start': startDy = reduceCSSCalc("calc(".concat(capHeight, ")")); break; case 'middle': startDy = reduceCSSCalc("calc(".concat((wordsByLines.length - 1) / 2, " * -").concat(lineHeight, " + (").concat(capHeight, " / 2))")); break; default: startDy = reduceCSSCalc("calc(".concat(wordsByLines.length - 1, " * -").concat(lineHeight, ")")); break; } var transforms = []; if (scaleToFit) { var lineWidth = wordsByLines[0].width; var width = this.props.width; transforms.push("scale(".concat((isNumber(width) ? width / lineWidth : 1) / lineWidth, ")")); } if (angle) { transforms.push("rotate(".concat(angle, ", ").concat(x, ", ").concat(y, ")")); } if (transforms.length) { textProps.transform = transforms.join(' '); } return /*#__PURE__*/React.createElement("text", _extends({}, filterProps(textProps, true), { x: x, y: y, className: classNames('recharts-text', className), textAnchor: textAnchor }), wordsByLines.map(function (line, index) { return ( /*#__PURE__*/ // eslint-disable-next-line react/no-array-index-key React.createElement("tspan", { x: x, dy: index === 0 ? startDy : lineHeight, key: index }, line.words.join(breakAll ? '' : ' ')) ); })); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps, prevState) { if (nextProps.width !== prevState.prevWidth || nextProps.scaleToFit !== prevState.prevScaleToFit || nextProps.children !== prevState.prevChildren || nextProps.style !== prevState.prevStyle || nextProps.breakAll !== prevState.prevBreakAll) { var needCalculate = nextProps.children !== prevState.prevChildren || nextProps.style !== prevState.prevStyle || nextProps.breakAll !== prevState.prevBreakAll; return { prevWidth: nextProps.width, prevScaleToFit: nextProps.scaleToFit, prevChildren: nextProps.children, prevStyle: nextProps.style, wordsByLines: getWordsByLines(nextProps, needCalculate) }; } return null; } }]); return Text; }(Component); Text.defaultProps = { x: 0, y: 0, lineHeight: '1em', capHeight: '0.71em', // Magic number from d3 scaleToFit: false, textAnchor: 'start', verticalAnchor: 'end' // Maintain compat with existing charts / default SVG behavior };