recharts
Version:
React charts
469 lines (467 loc) • 17.9 kB
JavaScript
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); }
var _excluded = ["offset"];
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 _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 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(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); }
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); }
import React, { cloneElement, isValidElement, createElement } from 'react';
import isNil from 'lodash/isNil';
import isFunction from 'lodash/isFunction';
import isObject from 'lodash/isObject';
import clsx from 'clsx';
import { Text } from './Text';
import { findAllByType, filterProps } from '../util/ReactUtils';
import { isNumOrStr, isNumber, isPercent, getPercentValue, uniqueId, mathSign } from '../util/DataUtils';
import { polarToCartesian } from '../util/PolarUtils';
var getLabel = function getLabel(props) {
var value = props.value,
formatter = props.formatter;
var label = isNil(props.children) ? value : props.children;
if (isFunction(formatter)) {
return formatter(label);
}
return label;
};
var getDeltaAngle = function getDeltaAngle(startAngle, endAngle) {
var sign = mathSign(endAngle - startAngle);
var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360);
return sign * deltaAngle;
};
var renderRadialLabel = function renderRadialLabel(labelProps, label, attrs) {
var position = labelProps.position,
viewBox = labelProps.viewBox,
offset = labelProps.offset,
className = labelProps.className;
var _ref = viewBox,
cx = _ref.cx,
cy = _ref.cy,
innerRadius = _ref.innerRadius,
outerRadius = _ref.outerRadius,
startAngle = _ref.startAngle,
endAngle = _ref.endAngle,
clockWise = _ref.clockWise;
var radius = (innerRadius + outerRadius) / 2;
var deltaAngle = getDeltaAngle(startAngle, endAngle);
var sign = deltaAngle >= 0 ? 1 : -1;
var labelAngle, direction;
if (position === 'insideStart') {
labelAngle = startAngle + sign * offset;
direction = clockWise;
} else if (position === 'insideEnd') {
labelAngle = endAngle - sign * offset;
direction = !clockWise;
} else if (position === 'end') {
labelAngle = endAngle + sign * offset;
direction = clockWise;
}
direction = deltaAngle <= 0 ? direction : !direction;
var startPoint = polarToCartesian(cx, cy, radius, labelAngle);
var endPoint = polarToCartesian(cx, cy, radius, labelAngle + (direction ? 1 : -1) * 359);
var path = "M".concat(startPoint.x, ",").concat(startPoint.y, "\n A").concat(radius, ",").concat(radius, ",0,1,").concat(direction ? 0 : 1, ",\n ").concat(endPoint.x, ",").concat(endPoint.y);
var id = isNil(labelProps.id) ? uniqueId('recharts-radial-line-') : labelProps.id;
return /*#__PURE__*/React.createElement("text", _extends({}, attrs, {
dominantBaseline: "central",
className: clsx('recharts-radial-bar-label', className)
}), /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("path", {
id: id,
d: path
})), /*#__PURE__*/React.createElement("textPath", {
xlinkHref: "#".concat(id)
}, label));
};
var getAttrsOfPolarLabel = function getAttrsOfPolarLabel(props) {
var viewBox = props.viewBox,
offset = props.offset,
position = props.position;
var _ref2 = viewBox,
cx = _ref2.cx,
cy = _ref2.cy,
innerRadius = _ref2.innerRadius,
outerRadius = _ref2.outerRadius,
startAngle = _ref2.startAngle,
endAngle = _ref2.endAngle;
var midAngle = (startAngle + endAngle) / 2;
if (position === 'outside') {
var _polarToCartesian = polarToCartesian(cx, cy, outerRadius + offset, midAngle),
_x = _polarToCartesian.x,
_y = _polarToCartesian.y;
return {
x: _x,
y: _y,
textAnchor: _x >= cx ? 'start' : 'end',
verticalAnchor: 'middle'
};
}
if (position === 'center') {
return {
x: cx,
y: cy,
textAnchor: 'middle',
verticalAnchor: 'middle'
};
}
if (position === 'centerTop') {
return {
x: cx,
y: cy,
textAnchor: 'middle',
verticalAnchor: 'start'
};
}
if (position === 'centerBottom') {
return {
x: cx,
y: cy,
textAnchor: 'middle',
verticalAnchor: 'end'
};
}
var r = (innerRadius + outerRadius) / 2;
var _polarToCartesian2 = polarToCartesian(cx, cy, r, midAngle),
x = _polarToCartesian2.x,
y = _polarToCartesian2.y;
return {
x: x,
y: y,
textAnchor: 'middle',
verticalAnchor: 'middle'
};
};
var getAttrsOfCartesianLabel = function getAttrsOfCartesianLabel(props) {
var viewBox = props.viewBox,
parentViewBox = props.parentViewBox,
offset = props.offset,
position = props.position;
var _ref3 = viewBox,
x = _ref3.x,
y = _ref3.y,
width = _ref3.width,
height = _ref3.height;
// Define vertical offsets and position inverts based on the value being positive or negative
var verticalSign = height >= 0 ? 1 : -1;
var verticalOffset = verticalSign * offset;
var verticalEnd = verticalSign > 0 ? 'end' : 'start';
var verticalStart = verticalSign > 0 ? 'start' : 'end';
// Define horizontal offsets and position inverts based on the value being positive or negative
var horizontalSign = width >= 0 ? 1 : -1;
var horizontalOffset = horizontalSign * offset;
var horizontalEnd = horizontalSign > 0 ? 'end' : 'start';
var horizontalStart = horizontalSign > 0 ? 'start' : 'end';
if (position === 'top') {
var attrs = {
x: x + width / 2,
y: y - verticalSign * offset,
textAnchor: 'middle',
verticalAnchor: verticalEnd
};
return _objectSpread(_objectSpread({}, attrs), parentViewBox ? {
height: Math.max(y - parentViewBox.y, 0),
width: width
} : {});
}
if (position === 'bottom') {
var _attrs = {
x: x + width / 2,
y: y + height + verticalOffset,
textAnchor: 'middle',
verticalAnchor: verticalStart
};
return _objectSpread(_objectSpread({}, _attrs), parentViewBox ? {
height: Math.max(parentViewBox.y + parentViewBox.height - (y + height), 0),
width: width
} : {});
}
if (position === 'left') {
var _attrs2 = {
x: x - horizontalOffset,
y: y + height / 2,
textAnchor: horizontalEnd,
verticalAnchor: 'middle'
};
return _objectSpread(_objectSpread({}, _attrs2), parentViewBox ? {
width: Math.max(_attrs2.x - parentViewBox.x, 0),
height: height
} : {});
}
if (position === 'right') {
var _attrs3 = {
x: x + width + horizontalOffset,
y: y + height / 2,
textAnchor: horizontalStart,
verticalAnchor: 'middle'
};
return _objectSpread(_objectSpread({}, _attrs3), parentViewBox ? {
width: Math.max(parentViewBox.x + parentViewBox.width - _attrs3.x, 0),
height: height
} : {});
}
var sizeAttrs = parentViewBox ? {
width: width,
height: height
} : {};
if (position === 'insideLeft') {
return _objectSpread({
x: x + horizontalOffset,
y: y + height / 2,
textAnchor: horizontalStart,
verticalAnchor: 'middle'
}, sizeAttrs);
}
if (position === 'insideRight') {
return _objectSpread({
x: x + width - horizontalOffset,
y: y + height / 2,
textAnchor: horizontalEnd,
verticalAnchor: 'middle'
}, sizeAttrs);
}
if (position === 'insideTop') {
return _objectSpread({
x: x + width / 2,
y: y + verticalOffset,
textAnchor: 'middle',
verticalAnchor: verticalStart
}, sizeAttrs);
}
if (position === 'insideBottom') {
return _objectSpread({
x: x + width / 2,
y: y + height - verticalOffset,
textAnchor: 'middle',
verticalAnchor: verticalEnd
}, sizeAttrs);
}
if (position === 'insideTopLeft') {
return _objectSpread({
x: x + horizontalOffset,
y: y + verticalOffset,
textAnchor: horizontalStart,
verticalAnchor: verticalStart
}, sizeAttrs);
}
if (position === 'insideTopRight') {
return _objectSpread({
x: x + width - horizontalOffset,
y: y + verticalOffset,
textAnchor: horizontalEnd,
verticalAnchor: verticalStart
}, sizeAttrs);
}
if (position === 'insideBottomLeft') {
return _objectSpread({
x: x + horizontalOffset,
y: y + height - verticalOffset,
textAnchor: horizontalStart,
verticalAnchor: verticalEnd
}, sizeAttrs);
}
if (position === 'insideBottomRight') {
return _objectSpread({
x: x + width - horizontalOffset,
y: y + height - verticalOffset,
textAnchor: horizontalEnd,
verticalAnchor: verticalEnd
}, sizeAttrs);
}
if (isObject(position) && (isNumber(position.x) || isPercent(position.x)) && (isNumber(position.y) || isPercent(position.y))) {
return _objectSpread({
x: x + getPercentValue(position.x, width),
y: y + getPercentValue(position.y, height),
textAnchor: 'end',
verticalAnchor: 'end'
}, sizeAttrs);
}
return _objectSpread({
x: x + width / 2,
y: y + height / 2,
textAnchor: 'middle',
verticalAnchor: 'middle'
}, sizeAttrs);
};
var isPolar = function isPolar(viewBox) {
return 'cx' in viewBox && isNumber(viewBox.cx);
};
export function Label(_ref4) {
var _ref4$offset = _ref4.offset,
offset = _ref4$offset === void 0 ? 5 : _ref4$offset,
restProps = _objectWithoutProperties(_ref4, _excluded);
var props = _objectSpread({
offset: offset
}, restProps);
var viewBox = props.viewBox,
position = props.position,
value = props.value,
children = props.children,
content = props.content,
_props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
textBreakAll = props.textBreakAll;
if (!viewBox || isNil(value) && isNil(children) && ! /*#__PURE__*/isValidElement(content) && !isFunction(content)) {
return null;
}
if ( /*#__PURE__*/isValidElement(content)) {
return /*#__PURE__*/cloneElement(content, props);
}
var label;
if (isFunction(content)) {
label = /*#__PURE__*/createElement(content, props);
if ( /*#__PURE__*/isValidElement(label)) {
return label;
}
} else {
label = getLabel(props);
}
var isPolarLabel = isPolar(viewBox);
var attrs = filterProps(props, true);
if (isPolarLabel && (position === 'insideStart' || position === 'insideEnd' || position === 'end')) {
return renderRadialLabel(props, label, attrs);
}
var positionAttrs = isPolarLabel ? getAttrsOfPolarLabel(props) : getAttrsOfCartesianLabel(props);
return /*#__PURE__*/React.createElement(Text, _extends({
className: clsx('recharts-label', className)
}, attrs, positionAttrs, {
breakAll: textBreakAll
}), label);
}
Label.displayName = 'Label';
var parseViewBox = function parseViewBox(props) {
var cx = props.cx,
cy = props.cy,
angle = props.angle,
startAngle = props.startAngle,
endAngle = props.endAngle,
r = props.r,
radius = props.radius,
innerRadius = props.innerRadius,
outerRadius = props.outerRadius,
x = props.x,
y = props.y,
top = props.top,
left = props.left,
width = props.width,
height = props.height,
clockWise = props.clockWise,
labelViewBox = props.labelViewBox;
if (labelViewBox) {
return labelViewBox;
}
if (isNumber(width) && isNumber(height)) {
if (isNumber(x) && isNumber(y)) {
return {
x: x,
y: y,
width: width,
height: height
};
}
if (isNumber(top) && isNumber(left)) {
return {
x: top,
y: left,
width: width,
height: height
};
}
}
if (isNumber(x) && isNumber(y)) {
return {
x: x,
y: y,
width: 0,
height: 0
};
}
if (isNumber(cx) && isNumber(cy)) {
return {
cx: cx,
cy: cy,
startAngle: startAngle || angle || 0,
endAngle: endAngle || angle || 0,
innerRadius: innerRadius || 0,
outerRadius: outerRadius || radius || r || 0,
clockWise: clockWise
};
}
if (props.viewBox) {
return props.viewBox;
}
return {};
};
var parseLabel = function parseLabel(label, viewBox) {
if (!label) {
return null;
}
if (label === true) {
return /*#__PURE__*/React.createElement(Label, {
key: "label-implicit",
viewBox: viewBox
});
}
if (isNumOrStr(label)) {
return /*#__PURE__*/React.createElement(Label, {
key: "label-implicit",
viewBox: viewBox,
value: label
});
}
if ( /*#__PURE__*/isValidElement(label)) {
if (label.type === Label) {
return /*#__PURE__*/cloneElement(label, {
key: 'label-implicit',
viewBox: viewBox
});
}
return /*#__PURE__*/React.createElement(Label, {
key: "label-implicit",
content: label,
viewBox: viewBox
});
}
if (isFunction(label)) {
return /*#__PURE__*/React.createElement(Label, {
key: "label-implicit",
content: label,
viewBox: viewBox
});
}
if (isObject(label)) {
return /*#__PURE__*/React.createElement(Label, _extends({
viewBox: viewBox
}, label, {
key: "label-implicit"
}));
}
return null;
};
var renderCallByParent = function renderCallByParent(parentProps, viewBox) {
var checkPropsLabel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (!parentProps || !parentProps.children && checkPropsLabel && !parentProps.label) {
return null;
}
var children = parentProps.children;
var parentViewBox = parseViewBox(parentProps);
var explicitChildren = findAllByType(children, Label).map(function (child, index) {
return /*#__PURE__*/cloneElement(child, {
viewBox: viewBox || parentViewBox,
// eslint-disable-next-line react/no-array-index-key
key: "label-".concat(index)
});
});
if (!checkPropsLabel) {
return explicitChildren;
}
var implicitLabel = parseLabel(parentProps.label, viewBox || parentViewBox);
return [implicitLabel].concat(_toConsumableArray(explicitChildren));
};
Label.parseViewBox = parseViewBox;
Label.renderCallByParent = renderCallByParent;