recharts
Version:
React charts
354 lines (340 loc) • 14.4 kB
JavaScript
var _excluded = ["color"];
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return 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); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import * as React from 'react';
import { clsx } from '../util/clsx';
import { Surface } from '../container/Surface';
import { getSymbolHalfWidth, Symbols } from '../shape/Symbols';
import { adaptEventsOfChild } from '../util/types';
import { resolveDefaultProps } from '../util/resolveDefaultProps';
import { useBackwardsCompatibleTheme } from '../theme/useBackwardsCompatibleTheme';
import { getFillUrl } from '../fill/fillDefinitions';
import { useUniqueId } from '../util/useUniqueId';
var SIZE = 32;
/**
* Fill definitions in an icon paint the whole icon, so they get its size.
*/
var ICON_BOUNDS = {
width: SIZE,
height: SIZE
};
/**
* @public
*/
/**
* @public
*/
/**
* @public
*/
/**
* @public
*/
/**
* The dots that a graphical item draws at its data points, as the legend icon needs them.
*
* @since 3.11
* @public
*/
/**
* @public
*/
export var defaultLegendContentDefaultProps = {
align: 'center',
iconSize: 14,
inactiveColor: '#ccc',
layout: 'horizontal',
verticalAlign: 'middle',
labelStyle: {}
};
function getStrokeDasharray(input) {
if (typeof input === 'object' && input !== null && 'strokeDasharray' in input && input.strokeDasharray != null) {
return String(input.strokeDasharray);
}
return undefined;
}
/**
* Diameter of the symbol in the `line` legend icon, in the 32x32 icon coordinates.
*/
var LINE_ICON_SYMBOL_SIZE = 20;
/**
* Stroke width of a dot symbol in the legend icon, in the 32x32 icon coordinates.
* The icon is scaled down, so the stroke width of the dots in the chart would be too thin to see.
*/
var ICON_SYMBOL_STROKE_WIDTH = 3;
/**
* Colors of a dot symbol in the legend icon.
* The icon follows the dots. An inactive item draws the stroke with the inactive color,
* and the fill too when it comes from the item: equal to the item color or to the stroke.
* Other fills, such as the white fill of the default Line dots, stay.
*/
function getIconDotColors(dot, data, inactiveColor) {
var fill = dot.fill,
stroke = dot.stroke;
if (!data.inactive) {
return {
fill,
stroke
};
}
var hasColor = color => color != null && color !== 'none';
var isItemFill = hasColor(fill) && (fill === data.color || fill === stroke);
return {
fill: isItemFill ? inactiveColor : fill,
stroke: hasColor(stroke) ? inactiveColor : stroke
};
}
/**
* The `line` legend icon for a graphical item that draws one of the predefined symbols at its data points:
* a line with that symbol in the middle, in place of the default circle.
* The symbol has the same colors as the dots in the chart, and the line reaches the edge of the symbol.
*/
function LineWithSymbolIcon(_ref) {
var _dotColors$fill, _dotColors$stroke;
var dot = _ref.dot,
color = _ref.color,
dotColors = _ref.dotColors;
var halfSize = SIZE / 2;
var symbol = dot.symbol;
var halfWidth = getSymbolHalfWidth(symbol, LINE_ICON_SYMBOL_SIZE, 'diameter');
return /*#__PURE__*/React.createElement("g", {
className: "recharts-legend-icon"
}, /*#__PURE__*/React.createElement("path", {
strokeWidth: 4,
fill: "none",
stroke: color,
d: "M0,".concat(halfSize, "H").concat(halfSize - halfWidth, "M").concat(halfSize + halfWidth, ",").concat(halfSize, "H").concat(SIZE)
}), /*#__PURE__*/React.createElement(Symbols, {
type: symbol,
cx: halfSize,
cy: halfSize,
size: LINE_ICON_SYMBOL_SIZE,
sizeType: "diameter",
fill: (_dotColors$fill = dotColors.fill) !== null && _dotColors$fill !== void 0 ? _dotColors$fill : 'none',
stroke: (_dotColors$stroke = dotColors.stroke) !== null && _dotColors$stroke !== void 0 ? _dotColors$stroke : color,
strokeWidth: ICON_SYMBOL_STROKE_WIDTH,
strokeLinejoin: "round"
}));
}
/**
* The legend icon that is the dot symbol itself, with the same colors as the dots in the chart.
*/
function DotSymbolIcon(_ref2) {
var _dotColors$fill2;
var dot = _ref2.dot,
color = _ref2.color,
dotColors = _ref2.dotColors;
var halfSize = SIZE / 2;
var hasStroke = dotColors.stroke != null && dotColors.stroke !== 'none';
var strokeProps = hasStroke ? {
stroke: dotColors.stroke,
strokeWidth: ICON_SYMBOL_STROKE_WIDTH,
strokeLinejoin: 'round'
} : {};
return /*#__PURE__*/React.createElement(Symbols, _extends({
fill: (_dotColors$fill2 = dotColors.fill) !== null && _dotColors$fill2 !== void 0 ? _dotColors$fill2 : color
}, strokeProps, {
cx: halfSize,
cy: halfSize
// keep the stroke inside the icon
,
size: hasStroke ? SIZE - ICON_SYMBOL_STROKE_WIDTH : SIZE,
sizeType: "diameter",
type: dot.symbol
}));
}
function Icon(_ref3) {
var data = _ref3.data,
iconType = _ref3.iconType,
inactiveColor = _ref3.inactiveColor;
var halfSize = SIZE / 2;
var sixthSize = SIZE / 6;
var thirdSize = SIZE / 3;
var color = data.inactive ? inactiveColor : data.color;
var preferredIcon = iconType !== null && iconType !== void 0 ? iconType : data.type;
var fillId = useUniqueId('recharts-legend-fill');
var fillObject = data.inactive ? undefined : data.fill;
if (preferredIcon === 'none') {
return null;
}
/*
* Filled icons paint with the fill definition of the graphical item.
* The definition lives in this icon, so it does not depend on IDs elsewhere in the page.
* The definition gets the size of the icon, so that patterns can scale their tiles to it.
*/
var areaFill = color;
var fillDefinition = null;
if (fillObject != null) {
var FillDefinition = fillObject.definition;
areaFill = getFillUrl(fillId);
fillDefinition = /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement(FillDefinition, {
id: fillId,
color: color,
bounds: ICON_BOUNDS
}));
}
if (preferredIcon === 'plainline') {
return /*#__PURE__*/React.createElement("line", {
strokeWidth: 4,
fill: "none",
stroke: color,
strokeDasharray: getStrokeDasharray(data.payload),
x1: 0,
y1: halfSize,
x2: SIZE,
y2: halfSize,
className: "recharts-legend-icon"
});
}
var dotColors = data.dot == null ? undefined : getIconDotColors(data.dot, data, inactiveColor);
if (preferredIcon === 'line' && data.dot != null && dotColors != null) {
return /*#__PURE__*/React.createElement(LineWithSymbolIcon, {
dot: data.dot,
color: color,
dotColors: dotColors
});
}
if (preferredIcon === 'line') {
return /*#__PURE__*/React.createElement("path", {
strokeWidth: 4,
fill: "none",
stroke: color,
d: "M0,".concat(halfSize, "h").concat(thirdSize, "\n A").concat(sixthSize, ",").concat(sixthSize, ",0,1,1,").concat(2 * thirdSize, ",").concat(halfSize, "\n H").concat(SIZE, "M").concat(2 * thirdSize, ",").concat(halfSize, "\n A").concat(sixthSize, ",").concat(sixthSize, ",0,1,1,").concat(thirdSize, ",").concat(halfSize),
className: "recharts-legend-icon"
});
}
if (preferredIcon === 'rect') {
return /*#__PURE__*/React.createElement(React.Fragment, null, fillDefinition, /*#__PURE__*/React.createElement("path", {
stroke: "none",
fill: areaFill,
d: "M0,".concat(SIZE / 8, "h").concat(SIZE, "v").concat(SIZE * 3 / 4, "h").concat(-SIZE, "z"),
className: "recharts-legend-icon"
}));
}
if (/*#__PURE__*/React.isValidElement(data.legendIcon)) {
var iconProps = _objectSpread({}, data);
delete iconProps.legendIcon;
// the fill object is for the built-in icons; it is not a valid SVG paint
delete iconProps.fill;
return /*#__PURE__*/React.cloneElement(data.legendIcon, iconProps);
}
if (data.dot != null && dotColors != null && preferredIcon === data.dot.symbol) {
return /*#__PURE__*/React.createElement(DotSymbolIcon, {
dot: data.dot,
color: color,
dotColors: dotColors
});
}
return /*#__PURE__*/React.createElement(React.Fragment, null, fillDefinition, /*#__PURE__*/React.createElement(Symbols, {
fill: areaFill,
cx: halfSize,
cy: halfSize,
size: SIZE,
sizeType: "diameter",
type: preferredIcon
}));
}
function Items(props) {
var payload = props.payload,
iconSize = props.iconSize,
layout = props.layout,
formatter = props.formatter,
inactiveColor = props.inactiveColor,
iconType = props.iconType,
labelStyle = props.labelStyle,
typographyColor = props.typographyColor;
var viewBox = {
x: 0,
y: 0,
width: SIZE,
height: SIZE
};
var itemStyle = {
display: layout === 'horizontal' ? 'inline-block' : 'block',
marginRight: 10,
whiteSpace: 'nowrap'
};
var svgStyle = {
display: 'inline-block',
verticalAlign: 'middle',
marginRight: 4
};
return payload.map((entry, i) => {
var _ref4, _finalLabelStyle$colo, _finalLabelStyle$whit, _finalLabelStyle$over;
var finalFormatter = entry.formatter || formatter;
var className = clsx({
'recharts-legend-item': true,
["legend-item-".concat(i)]: true,
inactive: entry.inactive
});
if (entry.type === 'none') {
return null;
}
var finalLabelStyle = typeof labelStyle === 'object' ? _objectSpread({}, labelStyle) : {};
finalLabelStyle.color = entry.inactive ? inactiveColor : (_ref4 = (_finalLabelStyle$colo = finalLabelStyle.color) !== null && _finalLabelStyle$colo !== void 0 ? _finalLabelStyle$colo : entry.color) !== null && _ref4 !== void 0 ? _ref4 : typographyColor;
(_finalLabelStyle$whit = finalLabelStyle.whiteSpace) !== null && _finalLabelStyle$whit !== void 0 ? _finalLabelStyle$whit : finalLabelStyle.whiteSpace = 'normal';
(_finalLabelStyle$over = finalLabelStyle.overflowWrap) !== null && _finalLabelStyle$over !== void 0 ? _finalLabelStyle$over : finalLabelStyle.overflowWrap = 'break-word';
var finalValue = finalFormatter ? finalFormatter(entry.value, entry, i) : entry.value;
return /*#__PURE__*/React.createElement("li", _extends({
className: className,
style: itemStyle,
key: "legend-item-".concat(i)
}, adaptEventsOfChild(props, entry, i)), /*#__PURE__*/React.createElement(Surface, {
width: iconSize,
height: iconSize,
viewBox: viewBox,
style: svgStyle,
"aria-label": entry.value == null ? 'legend icon' : "".concat(entry.value, " legend icon")
}, /*#__PURE__*/React.createElement(Icon, {
data: entry,
iconType: iconType,
inactiveColor: inactiveColor
})), /*#__PURE__*/React.createElement("span", {
className: "recharts-legend-item-text",
style: finalLabelStyle
}, finalValue));
});
}
/**
* This component is by default rendered inside the {@link Legend} component. You would not use it directly.
*
* You can use this component to customize the content of the legend,
* or you can provide your own completely independent content.
* @public
*/
export var DefaultLegendContent = outsideProps => {
var _theme$typography, _theme$legend;
var theme = useBackwardsCompatibleTheme(rechartsTheme => ({
typography: rechartsTheme.typography,
legend: rechartsTheme.legend
}), {}, undefined);
var props = resolveDefaultProps(outsideProps, defaultLegendContentDefaultProps);
var payload = props.payload,
layout = props.layout,
align = props.align;
var _ref5 = (_theme$typography = theme === null || theme === void 0 ? void 0 : theme.typography) !== null && _theme$typography !== void 0 ? _theme$typography : {},
typographyColor = _ref5.color,
typographyStyle = _objectWithoutProperties(_ref5, _excluded);
if (!payload || !payload.length) {
return null;
}
var finalStyle = {
padding: 0,
margin: 0,
textAlign: layout === 'horizontal' ? align : 'left'
};
return /*#__PURE__*/React.createElement("ul", {
className: "recharts-default-legend",
style: finalStyle
}, /*#__PURE__*/React.createElement(Items, _extends({}, props, {
labelStyle: _objectSpread(_objectSpread(_objectSpread({}, typographyStyle), theme === null || theme === void 0 || (_theme$legend = theme.legend) === null || _theme$legend === void 0 ? void 0 : _theme$legend.labelStyle), props.labelStyle),
payload: payload,
typographyColor: typographyColor
})));
};