recharts
Version:
React charts
253 lines (236 loc) • 11.4 kB
JavaScript
var _excluded = ["fill", "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); }
import { createContext, useContext, useMemo } from 'react';
import { useRechartsTheme } from './RechartsThemeContext';
import { isOutsidePosition } from '../cartesian/getCartesianPosition';
import { isOutsidePolarPosition } from '../polar/isOutsidePolarPosition';
/**
* The `background` prop of Label, LabelList and Text.
*/
/**
* Label props that a {@link LabelTheme} provides.
*/
/**
* Resolves the label background from the explicit prop and the theme.
*
* - `false` always wins and draws no background.
* - `undefined` uses the theme value, so a theme can turn backgrounds on by default.
* - `true` draws the background with the theme styles.
* - An object is merged key by key over the theme styles.
*
* @param explicit the `background` prop as the user set it
* @param fromTheme the `background` from the label theme
* @returns the background to draw
*/
export function resolveLabelBackground(explicit, fromTheme) {
if (explicit === false) {
return false;
}
if (explicit == null) {
return fromTheme;
}
var themeStyles = typeof fromTheme === 'object' ? fromTheme : undefined;
if (explicit === true) {
return themeStyles !== null && themeStyles !== void 0 ? themeStyles : true;
}
if (themeStyles == null) {
return explicit;
}
return _objectSpread(_objectSpread({}, themeStyles), explicit);
}
/**
* Splits the label theme style into the text color and the remaining styles.
*
* The color is returned separately so that an explicit `fill` prop can win over it.
* If it stayed in the style, it would override the `fill` prop.
*
* @param style the `style` from the label theme
* @returns the text color, and the style without colors
*/
function splitLabelStyle(style) {
if (style == null) {
return {
fill: undefined,
style: undefined
};
}
var fill = style.fill,
color = style.color,
rest = _objectWithoutProperties(style, _excluded);
var resolvedFill = fill !== null && fill !== void 0 ? fill : color;
return {
fill: typeof resolvedFill === 'string' ? resolvedFill : undefined,
style: rest
};
}
/**
* Applies a label theme to the explicit props of a label.
* Explicit props win over the theme, and the theme wins over the fallback fill.
*
* @param labelTheme the label theme, or undefined if there is none
* @param explicit the explicit `fill`, `style` and `background` props of the label
* @param fallbackFill the fill to use when neither the props nor the theme set one, for example the series color
* @returns the props to pass to the label
*/
export function applyLabelTheme(labelTheme, explicit, fallbackFill) {
var _ref, _explicit$fill;
var _splitLabelStyle = splitLabelStyle(labelTheme === null || labelTheme === void 0 ? void 0 : labelTheme.style),
themeFill = _splitLabelStyle.fill,
themeStyle = _splitLabelStyle.style;
return {
fill: (_ref = (_explicit$fill = explicit.fill) !== null && _explicit$fill !== void 0 ? _explicit$fill : themeFill) !== null && _ref !== void 0 ? _ref : fallbackFill,
style: themeStyle == null ? explicit.style : _objectSpread(_objectSpread({}, themeStyle), explicit.style),
background: resolveLabelBackground(explicit.background, labelTheme === null || labelTheme === void 0 ? void 0 : labelTheme.background)
};
}
/**
* How the data labels of a graphical item pick their automatic color in a theme.
*/
/**
* The colors a graphical item draws one shape with.
* Graphical items that draw a single color per data point, such as Line, pass it as `stroke`.
*/
/**
* Returns true if the color paints something. `none` and `transparent` leave the shape empty.
*
* @param color fill or stroke
* @returns true if the color is visible
*/
function isPainted(color) {
return color != null && color !== 'none' && color !== 'transparent';
}
var DataLabelColorsContext = /*#__PURE__*/createContext(undefined);
export var DataLabelColorsContextProvider = DataLabelColorsContext.Provider;
export function useDataLabelColors() {
return useContext(DataLabelColorsContext);
}
/**
* Data label colors of Funnel and Pie. Their LabelList entries carry the color of each shape.
*/
export var filledShapeLabelColors = {
hasFilledShapes: true
};
/**
* Returns stable data label colors for graphical items where every label takes the color of the series:
* Line, Area and Radar.
*
* @param color the color of the series, theme included
* @returns data label colors for {@link DataLabelColorsContextProvider}
*/
export function useSeriesDataLabelColors(color) {
return useMemo(() => ({
hasFilledShapes: false,
getShapeColors: () => ({
stroke: color
})
}), [color]);
}
/**
* Returns the automatic text color of a data label in a theme.
*
* - A label that sits on top of a filled shape takes the `pageBackground` color,
* like a cut-out of the shape. Each palette entry of the built-in themes has 4.5:1 against `pageBackground`.
* A shape with `fill="none"` or `transparent` is not filled, so its labels count as next to it.
* - A label next to its shape takes the color of its data point, so it reads as part of the series:
* the fill, or the stroke if the fill paints nothing.
* - A label with its own `background` or `halo` sits on that, so it takes the color of its data point too.
* - A label at an `{ x, y }` position can be anywhere, so it takes the `typography` color.
*
* @param options.position the label position
* @param options.isPolar true if the label renders in a polar view box, the same check that Label uses to place it
* @param options.hasFilledShapes see {@link DataLabelColors.hasFilledShapes}
* @param options.shapeColors the colors of the data point
* @param options.pageBackground the effective page background, see `getEffectivePageBackground`
* @param options.hasOwnBackground true if the label draws a background or a halo
* @returns the text color, or undefined for the `typography` color
*/
export function getAutomaticDataLabelFill(_ref2) {
var position = _ref2.position,
isPolar = _ref2.isPolar,
hasFilledShapes = _ref2.hasFilledShapes,
shapeColors = _ref2.shapeColors,
pageBackground = _ref2.pageBackground,
hasOwnBackground = _ref2.hasOwnBackground;
if (position != null && typeof position === 'object') {
return undefined;
}
var fill = shapeColors.fill,
stroke = shapeColors.stroke;
var isOutside = isPolar ? isOutsidePolarPosition(position) : isOutsidePosition(position);
if (hasFilledShapes && isPainted(fill) && !isOutside && !hasOwnBackground) {
// Without a page background there is no cut-out color, and the typography color is the safest choice.
return pageBackground;
}
if (isPainted(fill)) {
return fill;
}
return isPainted(stroke) ? stroke : undefined;
}
/**
* True inside a LabelList.
* Data labels take their styles from `graphicalItems[n].label` and never from the global `label` theme.
*/
var DataLabelContext = /*#__PURE__*/createContext(false);
export var DataLabelContextProvider = DataLabelContext.Provider;
/**
* Returns the global `label` theme, or undefined for data labels and without a theme.
*/
export function useGlobalLabelTheme() {
var theme = useRechartsTheme();
var isDataLabel = useContext(DataLabelContext);
if (isDataLabel) {
return undefined;
}
return theme === null || theme === void 0 ? void 0 : theme.label;
}
/**
* Returns the label theme of the data label at this index.
* Graphical items with one style per series return the same theme for every index,
* Pie and Funnel return the theme of the sector or trapezoid at this index.
*/
/**
* Graphical items provide this context so that their LabelList can find its theme.
*/
var DataLabelThemeContext = /*#__PURE__*/createContext(undefined);
export var DataLabelThemeContextProvider = DataLabelThemeContext.Provider;
export function useDataLabelThemeResolver() {
return useContext(DataLabelThemeContext);
}
/**
* Returns a stable resolver that gives every data label of a graphical item the same label theme.
*
* @param graphicalItemThemeSelector selects the theme entry of the graphical item, see {@link useGraphicalItemIdentity}.
* @returns the resolver, or undefined without a theme
*/
export function useSeriesLabelThemeResolver(graphicalItemThemeSelector) {
var _graphicalItemThemeSe;
var theme = useRechartsTheme();
var labelTheme = theme == null ? undefined : (_graphicalItemThemeSe = graphicalItemThemeSelector(theme)) === null || _graphicalItemThemeSe === void 0 ? void 0 : _graphicalItemThemeSe.label;
return useMemo(() => labelTheme == null ? undefined : () => labelTheme, [labelTheme]);
}
/**
* Returns a stable resolver for graphical items that take one theme entry per index, such as Pie and Funnel.
* The data label at index `i` takes the label theme of `graphicalItems[i % graphicalItems.length]`.
*
* The labels keep their theme even if the graphical item sets its own `fill` or `stroke`.
*
* @param graphicalItems the graphical item styles from the theme, or undefined without a theme
* @returns the resolver, or undefined if the labels take no theme
*/
export function useIndexedLabelThemeResolver(graphicalItems) {
return useMemo(() => {
if (graphicalItems == null || graphicalItems.length === 0) {
return undefined;
}
return index => {
var _graphicalItems;
return (_graphicalItems = graphicalItems[index % graphicalItems.length]) === null || _graphicalItems === void 0 ? void 0 : _graphicalItems.label;
};
}, [graphicalItems]);
}