UNPKG

recharts

Version:
553 lines (551 loc) 25.2 kB
import _get from "lodash/get"; import _isFunction from "lodash/isFunction"; var _excluded = ["viewBox"], _excluded2 = ["viewBox"], _excluded3 = ["ticks"]; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } 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, _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 _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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /** * @fileOverview Cartesian Axis */ import React, { Component } from 'react'; import classNames from 'classnames'; import { shallowEqual } from '../util/ShallowEqual'; import { getStringSize } from '../util/DOMUtils'; import { Layer } from '../container/Layer'; import { Text } from '../component/Text'; import { Label } from '../component/Label'; import { Global } from '../util/Global'; import { isNumber, mathSign } from '../util/DataUtils'; import { adaptEventsOfChild } from '../util/types'; import { filterProps } from '../util/ReactUtils'; export var CartesianAxis = /*#__PURE__*/function (_Component) { _inherits(CartesianAxis, _Component); var _super = _createSuper(CartesianAxis); function CartesianAxis(props) { var _this; _classCallCheck(this, CartesianAxis); _this = _super.call(this, props); _this.state = { fontSize: '', letterSpacing: '' }; return _this; } // todo Array<Tick> _createClass(CartesianAxis, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(_ref, nextState) { var viewBox = _ref.viewBox, restProps = _objectWithoutProperties(_ref, _excluded); // props.viewBox is sometimes generated every time - // check that specially as object equality is likely to fail var _this$props = this.props, viewBoxOld = _this$props.viewBox, restPropsOld = _objectWithoutProperties(_this$props, _excluded2); return !shallowEqual(viewBox, viewBoxOld) || !shallowEqual(restProps, restPropsOld) || !shallowEqual(nextState, this.state); } }, { key: "componentDidMount", value: function componentDidMount() { var htmlLayer = this.layerReference; if (!htmlLayer) return; var tick = htmlLayer.getElementsByClassName('recharts-cartesian-axis-tick-value')[0]; if (tick) { this.setState({ fontSize: window.getComputedStyle(tick).fontSize, letterSpacing: window.getComputedStyle(tick).letterSpacing }); } } /** * Calculate the coordinates of endpoints in ticks * @param {Object} data The data of a simple tick * @return {Object} (x1, y1): The coordinate of endpoint close to tick text * (x2, y2): The coordinate of endpoint close to axis */ }, { key: "getTickLineCoord", value: function getTickLineCoord(data) { var _this$props2 = this.props, x = _this$props2.x, y = _this$props2.y, width = _this$props2.width, height = _this$props2.height, orientation = _this$props2.orientation, tickSize = _this$props2.tickSize, mirror = _this$props2.mirror, tickMargin = _this$props2.tickMargin; var x1, x2, y1, y2, tx, ty; var sign = mirror ? -1 : 1; var finalTickSize = data.tickSize || tickSize; var tickCoord = isNumber(data.tickCoord) ? data.tickCoord : data.coordinate; switch (orientation) { case 'top': x1 = x2 = data.coordinate; y2 = y + +!mirror * height; y1 = y2 - sign * finalTickSize; ty = y1 - sign * tickMargin; tx = tickCoord; break; case 'left': y1 = y2 = data.coordinate; x2 = x + +!mirror * width; x1 = x2 - sign * finalTickSize; tx = x1 - sign * tickMargin; ty = tickCoord; break; case 'right': y1 = y2 = data.coordinate; x2 = x + +mirror * width; x1 = x2 + sign * finalTickSize; tx = x1 + sign * tickMargin; ty = tickCoord; break; default: x1 = x2 = data.coordinate; y2 = y + +mirror * height; y1 = y2 + sign * finalTickSize; ty = y1 + sign * tickMargin; tx = tickCoord; break; } return { line: { x1: x1, y1: y1, x2: x2, y2: y2 }, tick: { x: tx, y: ty } }; } }, { key: "getTickTextAnchor", value: function getTickTextAnchor() { var _this$props3 = this.props, orientation = _this$props3.orientation, mirror = _this$props3.mirror; var textAnchor; switch (orientation) { case 'left': textAnchor = mirror ? 'start' : 'end'; break; case 'right': textAnchor = mirror ? 'end' : 'start'; break; default: textAnchor = 'middle'; break; } return textAnchor; } }, { key: "getTickVerticalAnchor", value: function getTickVerticalAnchor() { var _this$props4 = this.props, orientation = _this$props4.orientation, mirror = _this$props4.mirror; var verticalAnchor = 'end'; switch (orientation) { case 'left': case 'right': verticalAnchor = 'middle'; break; case 'top': verticalAnchor = mirror ? 'start' : 'end'; break; default: verticalAnchor = mirror ? 'end' : 'start'; break; } return verticalAnchor; } }, { key: "renderAxisLine", value: function renderAxisLine() { var _this$props5 = this.props, x = _this$props5.x, y = _this$props5.y, width = _this$props5.width, height = _this$props5.height, orientation = _this$props5.orientation, mirror = _this$props5.mirror, axisLine = _this$props5.axisLine; var props = _objectSpread(_objectSpread(_objectSpread({}, filterProps(this.props)), filterProps(axisLine)), {}, { fill: 'none' }); if (orientation === 'top' || orientation === 'bottom') { var needHeight = +(orientation === 'top' && !mirror || orientation === 'bottom' && mirror); props = _objectSpread(_objectSpread({}, props), {}, { x1: x, y1: y + needHeight * height, x2: x + width, y2: y + needHeight * height }); } else { var needWidth = +(orientation === 'left' && !mirror || orientation === 'right' && mirror); props = _objectSpread(_objectSpread({}, props), {}, { x1: x + needWidth * width, y1: y, x2: x + needWidth * width, y2: y + height }); } return /*#__PURE__*/React.createElement("line", _extends({}, props, { className: classNames('recharts-cartesian-axis-line', _get(axisLine, 'className')) })); } }, { key: "renderTicks", value: /** * render the ticks * @param {Array} ticks The ticks to actually render (overrides what was passed in props) * @param {string} fontSize Fontsize to consider for tick spacing * @param {string} letterSpacing Letterspacing to consider for tick spacing * @return {ReactComponent} renderedTicks */ function renderTicks(ticks, fontSize, letterSpacing) { var _this2 = this; var _this$props6 = this.props, tickLine = _this$props6.tickLine, stroke = _this$props6.stroke, tick = _this$props6.tick, tickFormatter = _this$props6.tickFormatter, unit = _this$props6.unit; var finalTicks = CartesianAxis.getTicks(_objectSpread(_objectSpread({}, this.props), {}, { ticks: ticks }), fontSize, letterSpacing); var textAnchor = this.getTickTextAnchor(); var verticalAnchor = this.getTickVerticalAnchor(); var axisProps = filterProps(this.props); var customTickProps = filterProps(tick); var tickLineProps = _objectSpread(_objectSpread({}, axisProps), {}, { fill: 'none' }, filterProps(tickLine)); var items = finalTicks.map(function (entry, i) { var _this2$getTickLineCoo = _this2.getTickLineCoord(entry), lineCoord = _this2$getTickLineCoo.line, tickCoord = _this2$getTickLineCoo.tick; var tickProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({ textAnchor: textAnchor, verticalAnchor: verticalAnchor }, axisProps), {}, { stroke: 'none', fill: stroke }, customTickProps), tickCoord), {}, { index: i, payload: entry, visibleTicksCount: finalTicks.length, tickFormatter: tickFormatter }); return /*#__PURE__*/React.createElement(Layer, _extends({ className: "recharts-cartesian-axis-tick", key: "tick-".concat(i) // eslint-disable-line react/no-array-index-key }, adaptEventsOfChild(_this2.props, entry, i)), tickLine && /*#__PURE__*/React.createElement("line", _extends({}, tickLineProps, lineCoord, { className: classNames('recharts-cartesian-axis-tick-line', _get(tickLine, 'className')) })), tick && CartesianAxis.renderTickItem(tick, tickProps, "".concat(_isFunction(tickFormatter) ? tickFormatter(entry.value, i) : entry.value).concat(unit || ''))); }); return /*#__PURE__*/React.createElement("g", { className: "recharts-cartesian-axis-ticks" }, items); } }, { key: "render", value: function render() { var _this3 = this; var _this$props7 = this.props, axisLine = _this$props7.axisLine, width = _this$props7.width, height = _this$props7.height, ticksGenerator = _this$props7.ticksGenerator, className = _this$props7.className, hide = _this$props7.hide; if (hide) { return null; } var _this$props8 = this.props, ticks = _this$props8.ticks, noTicksProps = _objectWithoutProperties(_this$props8, _excluded3); var finalTicks = ticks; if (_isFunction(ticksGenerator)) { finalTicks = ticks && ticks.length > 0 ? ticksGenerator(this.props) : ticksGenerator(noTicksProps); } if (width <= 0 || height <= 0 || !finalTicks || !finalTicks.length) { return null; } return /*#__PURE__*/React.createElement(Layer, { className: classNames('recharts-cartesian-axis', className), ref: function ref(_ref2) { _this3.layerReference = _ref2; } }, axisLine && this.renderAxisLine(), this.renderTicks(finalTicks, this.state.fontSize, this.state.letterSpacing), Label.renderCallByParent(this.props)); } }], [{ key: "getTicks", value: function getTicks(props, fontSize, letterSpacing) { var tick = props.tick, ticks = props.ticks, viewBox = props.viewBox, minTickGap = props.minTickGap, orientation = props.orientation, interval = props.interval, tickFormatter = props.tickFormatter, unit = props.unit; if (!ticks || !ticks.length || !tick) { return []; } if (isNumber(interval) || Global.isSsr) { return CartesianAxis.getNumberIntervalTicks(ticks, typeof interval === 'number' && isNumber(interval) ? interval : 0); } if (interval === 'preserveStartEnd') { return CartesianAxis.getTicksStart({ ticks: ticks, tickFormatter: tickFormatter, viewBox: viewBox, orientation: orientation, minTickGap: minTickGap, unit: unit, fontSize: fontSize, letterSpacing: letterSpacing }, true); } if (interval === 'preserveStart') { return CartesianAxis.getTicksStart({ ticks: ticks, tickFormatter: tickFormatter, viewBox: viewBox, orientation: orientation, minTickGap: minTickGap, unit: unit, fontSize: fontSize, letterSpacing: letterSpacing }); } return CartesianAxis.getTicksEnd({ ticks: ticks, tickFormatter: tickFormatter, viewBox: viewBox, orientation: orientation, minTickGap: minTickGap, unit: unit, fontSize: fontSize, letterSpacing: letterSpacing }); } }, { key: "getNumberIntervalTicks", value: function getNumberIntervalTicks(ticks, interval) { return ticks.filter(function (entry, i) { return i % (interval + 1) === 0; }); } }, { key: "getTicksStart", value: function getTicksStart(_ref3, preserveEnd) { var ticks = _ref3.ticks, tickFormatter = _ref3.tickFormatter, viewBox = _ref3.viewBox, orientation = _ref3.orientation, minTickGap = _ref3.minTickGap, unit = _ref3.unit, fontSize = _ref3.fontSize, letterSpacing = _ref3.letterSpacing; var x = viewBox.x, y = viewBox.y, width = viewBox.width, height = viewBox.height; var sizeKey = orientation === 'top' || orientation === 'bottom' ? 'width' : 'height'; var result = (ticks || []).slice(); // we need add the width of 'unit' only when sizeKey === 'width' var unitSize = unit && sizeKey === 'width' ? getStringSize(unit, { fontSize: fontSize, letterSpacing: letterSpacing })[sizeKey] : 0; var len = result.length; var sign = len >= 2 ? mathSign(result[1].coordinate - result[0].coordinate) : 1; var start, end; if (sign === 1) { start = sizeKey === 'width' ? x : y; end = sizeKey === 'width' ? x + width : y + height; } else { start = sizeKey === 'width' ? x + width : y + height; end = sizeKey === 'width' ? x : y; } if (preserveEnd) { // Try to guarantee the tail to be displayed var tail = ticks[len - 1]; var tailContent = _isFunction(tickFormatter) ? tickFormatter(tail.value, len - 1) : tail.value; var tailSize = getStringSize(tailContent, { fontSize: fontSize, letterSpacing: letterSpacing })[sizeKey] + unitSize; var tailGap = sign * (tail.coordinate + sign * tailSize / 2 - end); result[len - 1] = tail = _objectSpread(_objectSpread({}, tail), {}, { tickCoord: tailGap > 0 ? tail.coordinate - tailGap * sign : tail.coordinate }); var isTailShow = sign * (tail.tickCoord - sign * tailSize / 2 - start) >= 0 && sign * (tail.tickCoord + sign * tailSize / 2 - end) <= 0; if (isTailShow) { end = tail.tickCoord - sign * (tailSize / 2 + minTickGap); result[len - 1] = _objectSpread(_objectSpread({}, tail), {}, { isShow: true }); } } var count = preserveEnd ? len - 1 : len; for (var i = 0; i < count; i++) { var entry = result[i]; var content = _isFunction(tickFormatter) ? tickFormatter(entry.value, i) : entry.value; var size = getStringSize(content, { fontSize: fontSize, letterSpacing: letterSpacing })[sizeKey] + unitSize; if (i === 0) { var gap = sign * (entry.coordinate - sign * size / 2 - start); result[i] = entry = _objectSpread(_objectSpread({}, entry), {}, { tickCoord: gap < 0 ? entry.coordinate - gap * sign : entry.coordinate }); } else { result[i] = entry = _objectSpread(_objectSpread({}, entry), {}, { tickCoord: entry.coordinate }); } var isShow = sign * (entry.tickCoord - sign * size / 2 - start) >= 0 && sign * (entry.tickCoord + sign * size / 2 - end) <= 0; if (isShow) { start = entry.tickCoord + sign * (size / 2 + minTickGap); result[i] = _objectSpread(_objectSpread({}, entry), {}, { isShow: true }); } } return result.filter(function (entry) { return entry.isShow; }); } }, { key: "getTicksEnd", value: function getTicksEnd(_ref4) { var ticks = _ref4.ticks, tickFormatter = _ref4.tickFormatter, viewBox = _ref4.viewBox, orientation = _ref4.orientation, minTickGap = _ref4.minTickGap, unit = _ref4.unit, fontSize = _ref4.fontSize, letterSpacing = _ref4.letterSpacing; var x = viewBox.x, y = viewBox.y, width = viewBox.width, height = viewBox.height; var sizeKey = orientation === 'top' || orientation === 'bottom' ? 'width' : 'height'; // we need add the width of 'unit' only when sizeKey === 'width' var unitSize = unit && sizeKey === 'width' ? getStringSize(unit, { fontSize: fontSize, letterSpacing: letterSpacing })[sizeKey] : 0; var result = (ticks || []).slice(); var len = result.length; var sign = len >= 2 ? mathSign(result[1].coordinate - result[0].coordinate) : 1; var start, end; if (sign === 1) { start = sizeKey === 'width' ? x : y; end = sizeKey === 'width' ? x + width : y + height; } else { start = sizeKey === 'width' ? x + width : y + height; end = sizeKey === 'width' ? x : y; } for (var i = len - 1; i >= 0; i--) { var entry = result[i]; var content = _isFunction(tickFormatter) ? tickFormatter(entry.value, len - i - 1) : entry.value; var size = getStringSize(content, { fontSize: fontSize, letterSpacing: letterSpacing })[sizeKey] + unitSize; if (i === len - 1) { var gap = sign * (entry.coordinate + sign * size / 2 - end); result[i] = entry = _objectSpread(_objectSpread({}, entry), {}, { tickCoord: gap > 0 ? entry.coordinate - gap * sign : entry.coordinate }); } else { result[i] = entry = _objectSpread(_objectSpread({}, entry), {}, { tickCoord: entry.coordinate }); } var isShow = sign * (entry.tickCoord - sign * size / 2 - start) >= 0 && sign * (entry.tickCoord + sign * size / 2 - end) <= 0; if (isShow) { end = entry.tickCoord - sign * (size / 2 + minTickGap); result[i] = _objectSpread(_objectSpread({}, entry), {}, { isShow: true }); } } return result.filter(function (entry) { return entry.isShow; }); } }, { key: "renderTickItem", value: function renderTickItem(option, props, value) { var tickItem; if ( /*#__PURE__*/React.isValidElement(option)) { tickItem = /*#__PURE__*/React.cloneElement(option, props); } else if (_isFunction(option)) { tickItem = option(props); } else { tickItem = /*#__PURE__*/React.createElement(Text, _extends({}, props, { className: "recharts-cartesian-axis-tick-value" }), value); } return tickItem; } }]); return CartesianAxis; }(Component); _defineProperty(CartesianAxis, "displayName", 'CartesianAxis'); _defineProperty(CartesianAxis, "defaultProps", { x: 0, y: 0, width: 0, height: 0, viewBox: { x: 0, y: 0, width: 0, height: 0 }, // The orientation of axis orientation: 'bottom', // The ticks ticks: [], stroke: '#666', tickLine: true, axisLine: true, tick: true, mirror: false, minTickGap: 5, // The width or height of tick tickSize: 6, tickMargin: 2, interval: 'preserveEnd' });