UNPKG

recharts

Version:
261 lines (249 loc) • 13.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEntryColor = getEntryColor; exports.getEntryShapeColors = getEntryShapeColors; exports.getEntryStyleOverrides = getEntryStyleOverrides; exports.getEntrySvgProps = getEntrySvgProps; exports.getOwnStyles = getOwnStyles; exports.getUnthemedColors = getUnthemedColors; exports.hasOwnColors = hasOwnColors; exports.omitEntrySvgProps = omitEntrySvgProps; exports.useUnthemedColors = useUnthemedColors; exports.withoutColors = withoutColors; var _react = require("react"); var _RechartsThemeContext = require("./RechartsThemeContext"); var _svgPropertiesNoEvents = require("../util/svgPropertiesNoEvents"); var _fillDefinitions = require("../fill/fillDefinitions"); var _excluded = ["fill", "stroke"], _excluded2 = ["id", "ref", "key"], _excluded3 = ["fill"]; /** * @fileOverview * Users can style graphical items with explicit props (`<Bar fill="red" />`), * and graphical items that render one shape per data point (Bar, Pie, Scatter, ...) * also let users style individual shapes by putting presentation properties * directly in the data array. * * The theme colors are designed as a pair: the `fill` and `stroke` are chosen to look good together. * If we mixed a user-provided `fill` with a theme-provided `stroke`, the result would be a shape with two * unrelated colors. So as soon as a graphical item or a data entry defines its own * `fill` or `stroke`, it drops the theme `fill` and `stroke` together, * and takes its colors only from its own styles and the explicit props of the graphical item. * * Other style properties (opacities, `strokeWidth`, `strokeDasharray`) do not * bring a color of their own, so they merge with the theme field by field. * The `label` styles of the theme keep applying too, and so do the `active` styles without their colors, * see `activeStyles.ts`. */ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } 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(r) { if (Array.isArray(r)) return r; } 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 _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; } /** * Explicit `fill` and `stroke` props of a graphical item, both keys present (possibly as `undefined`). * Spreading this object over themed props removes the theme colors * while keeping the colors the user set on the graphical item. */ /** * Returns true if the graphical item props or the data entry define their own `fill` or `stroke`. * Such items and entries drop the theme `fill` and `stroke`, and keep the rest of the theme. * A fill object counts by its `color` only, so a fill object without a color keeps the theme colors. * @param entry props of graphical item, data entry, or anything else */ function hasOwnColors(entry) { if (entry == null || typeof entry !== 'object') { return false; } return (0, _fillDefinitions.readEntryFill)(entry).color != null || Reflect.get(entry, 'stroke') != null; } var withoutColorsCache = new WeakMap(); /** * Returns a copy of the style without its `fill` and `stroke`. * Every other key, `active` and `label` included, stays. * The result is cached, so the same style always returns the same object. That keeps theme selectors stable. * @param style theme style, for example an entry of `graphicalItems` */ function withoutColors(style) { if (style == null) { return undefined; } var cached = withoutColorsCache.get(style); if (cached != null) { return cached; } var fill = style.fill, stroke = style.stroke, rest = _objectWithoutProperties(style, _excluded); withoutColorsCache.set(style, rest); return rest; } /** * Picks the themeable style properties that the data entry defines, and leaves out the undefined ones. * @param entry data entry */ function getOwnStyles(entry) { var result = {}; if (entry == null) { return result; } var entryFill = (0, _fillDefinitions.readEntryFill)(entry).color; if (entryFill != null) { result.fill = entryFill; } if (entry.fillOpacity != null) { result.fillOpacity = entry.fillOpacity; } if (entry.stroke != null) { result.stroke = entry.stroke; } if (entry.strokeOpacity != null) { result.strokeOpacity = entry.strokeOpacity; } if (entry.strokeWidth != null) { result.strokeWidth = entry.strokeWidth; } if (entry.strokeDasharray != null) { result.strokeDasharray = entry.strokeDasharray; } return result; } /** * Picks the `fill` and `stroke` from explicit props, including the undefined ones. * @param explicitProps props as provided by the user, before merging with theme or defaults */ function getUnthemedColors(explicitProps) { return { fill: explicitProps.fill, stroke: explicitProps.stroke }; } /** * Returns referentially stable {@link getUnthemedColors} if a theme is active, or undefined without a theme. * Without a theme, graphical items keep their legacy behaviour and there is nothing to override. * @param explicitProps props as provided by the user, before merging with theme or defaults */ function useUnthemedColors(explicitProps) { var hasTheme = (0, _RechartsThemeContext.useRechartsTheme)() != null; var fill = explicitProps.fill, stroke = explicitProps.stroke; return (0, _react.useMemo)(() => hasTheme ? getUnthemedColors({ fill, stroke }) : undefined, [hasTheme, fill, stroke]); } /** * Returns the color overrides for a single data entry. * If the entry has its own colors, returns the unthemed colors which remove the theme colors from this entry. * Otherwise returns undefined, and the entry keeps the theme colors. * Either way, the entry keeps the other theme styles. * * @param entry data entry * @param unthemedColors result of {@link getUnthemedColors}, or undefined if there is no active theme */ function getEntryStyleOverrides(entry, unthemedColors) { if (unthemedColors == null || !hasOwnColors(entry)) { return undefined; } return unthemedColors; } /** * Picks the valid SVG properties of a data entry, without `id` so that rendered elements do not get duplicate DOM ids, * and without the React `ref` and `key`. * The raw entry, `id` included, stays available as `payload`. * Graphical items spread these after their own props, so the data entry wins. * * @param entry data entry */ function getEntrySvgProps(entry) { var _svgPropertiesNoEvent; var _ref = (_svgPropertiesNoEvent = (0, _svgPropertiesNoEvents.svgPropertiesNoEventsFromUnknown)(entry)) !== null && _svgPropertiesNoEvent !== void 0 ? _svgPropertiesNoEvent : {}, id = _ref.id, ref = _ref.ref, key = _ref.key, props = _objectWithoutProperties(_ref, _excluded2); if (props.fill != null && typeof props.fill !== 'string') { // a fill object paints through its definition; the shape gets its color, or keeps the fill of the graphical item var fill = props.fill, propsWithoutFill = _objectWithoutProperties(props, _excluded3); var _readEntryFill = (0, _fillDefinitions.readEntryFill)(entry), color = _readEntryFill.color; return color == null ? propsWithoutFill : _objectSpread(_objectSpread({}, propsWithoutFill), {}, { fill: color }); } return props; } function getStringProp(obj, key) { if (obj == null || typeof obj !== 'object') { return undefined; } if (key === 'fill') { return (0, _fillDefinitions.readEntryFill)(obj).color; } var value = Reflect.get(obj, key); return typeof value === 'string' ? value : undefined; } /** * The fill and stroke a graphical item renders a data entry with. * An inline `style.fill` or `style.stroke` of the entry wins over its `fill` or `stroke`, the same as in the browser. * Follows the order in which graphical items spread the styles: the entry colors win over the graphical item colors, * and with a theme, an entry with its own colors drops the theme colors. * * @param entry data entry, with the props of its `Cell` if there is one * @param fill fill of the graphical item, theme included * @param stroke stroke of the graphical item, theme included * @param unthemedColors result of {@link useUnthemedColors}, or undefined without a theme */ function getEntryShapeColors(entry, fill, stroke, unthemedColors) { var _getEntryStyleOverrid, _ref2, _getStringProp, _ref3, _getStringProp2; var itemStyles = (_getEntryStyleOverrid = getEntryStyleOverrides(entry, unthemedColors)) !== null && _getEntryStyleOverrid !== void 0 ? _getEntryStyleOverrid : { fill, stroke }; // An inline style wins over the presentation attribute, so the shape paints with `style.fill` and `style.stroke`. var entryStyle = entry != null && typeof entry === 'object' && 'style' in entry ? entry.style : undefined; return { fill: (_ref2 = (_getStringProp = getStringProp(entryStyle, 'fill')) !== null && _getStringProp !== void 0 ? _getStringProp : getStringProp(entry, 'fill')) !== null && _ref2 !== void 0 ? _ref2 : itemStyles.fill, stroke: (_ref3 = (_getStringProp2 = getStringProp(entryStyle, 'stroke')) !== null && _getStringProp2 !== void 0 ? _getStringProp2 : getStringProp(entry, 'stroke')) !== null && _ref3 !== void 0 ? _ref3 : itemStyles.stroke }; } /** * The color a graphical item renders a data entry with: its fill, or its stroke when it has no fill. * Tooltip and Legend use this so that they show the same color as the shape. * * @param entry data entry, with the props of its `Cell` if there is one * @param fill fill of the graphical item, theme included * @param stroke stroke of the graphical item, theme included * @param unthemedColors result of {@link useUnthemedColors}, or undefined without a theme * @see getEntryShapeColors */ function getEntryColor(entry, fill, stroke, unthemedColors) { var _colors$fill; var colors = getEntryShapeColors(entry, fill, stroke, unthemedColors); return (_colors$fill = colors.fill) !== null && _colors$fill !== void 0 ? _colors$fill : colors.stroke; } /** * Removes the SVG properties of a data entry (`stroke`, `opacity`, `style`, `data-*`, `id`, ...), and keeps `name`. * Backgrounds are not the graphical item, so they do not take the styles of the data entry. * Custom background shapes still receive the other keys of the entry, and the entry itself as `payload`. * * @param entry data entry, or the item computed from it */ function omitEntrySvgProps(entry) { return Object.fromEntries(Object.entries(entry).filter(_ref4 => { var _ref5 = _slicedToArray(_ref4, 1), key = _ref5[0]; return key === 'name' || !(0, _svgPropertiesNoEvents.isSvgElementPropKey)(key) && !(0, _svgPropertiesNoEvents.isDataAttribute)(key); })); }