recharts
Version:
React charts
107 lines (103 loc) • 5.14 kB
JavaScript
var _excluded = ["type", "cx", "cy", "r", "className"];
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); }
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; }
import * as React from 'react';
import { isValidElement } from 'react';
import { clsx } from '../util/clsx';
import { Symbols } from './Symbols';
import { adaptEventHandlers } from '../util/types';
import { svgPropertiesNoEvents } from '../util/svgPropertiesNoEvents';
/**
* Returns the predefined symbol that a `dot` or `activeDot` option asks for:
* either the option itself (`dot="diamond"`), or its `symbol` field (`dot={{ symbol: 'diamond' }}`).
*
* @param option the `dot` or `activeDot` prop
* @returns the symbol name, or undefined if the option renders a circle or a custom dot
*/
export function getDotSymbol(option) {
if (typeof option === 'string') {
return option;
}
if (option != null && typeof option === 'object' && ! /*#__PURE__*/isValidElement(option) && typeof option.symbol === 'string') {
return option.symbol;
}
return undefined;
}
function getOwnColor(option, key) {
if (option == null || typeof option !== 'object' || /*#__PURE__*/isValidElement(option)) {
return undefined;
}
var value = option[key];
return typeof value === 'string' ? value : undefined;
}
/**
* Returns true if the `dot` option renders the default circle {@link Dot}:
* `true`, or an object without a symbol.
*/
function rendersDefaultCircle(dot) {
if (dot === true) {
return true;
}
return dot != null && typeof dot === 'object' && ! /*#__PURE__*/isValidElement(dot) && dot.symbol == null;
}
/**
* Describes the dots of a graphical item for its legend icon,
* so that the icon draws the same symbol with the same colors as the dots.
*
* Without a theme, the default circle dots keep the default `line` legend icon, so that existing legends stay
* unchanged. With a theme, they are described as a `circle` so that the legend icon follows their theme colors.
* A custom dot (a function or an element) is never described, because the legend cannot know what it draws.
*
* @param dot the `dot` prop, after defaults
* @param seriesDotColors the colors that the dots get from the graphical item, including the theme
* @param isThemed true if the chart renders with a Recharts theme
* @returns the legend dot, or undefined if the legend icon does not draw the dots
*/
export function getLegendPayloadDot(dot, seriesDotColors, isThemed) {
var _getDotSymbol, _getOwnColor, _getOwnColor2;
var symbol = (_getDotSymbol = getDotSymbol(dot)) !== null && _getDotSymbol !== void 0 ? _getDotSymbol : isThemed && rendersDefaultCircle(dot) ? 'circle' : undefined;
if (symbol == null || dot == null) {
return undefined;
}
return {
symbol,
// colors in the dot object win over the graphical item, the same as in the dots
fill: (_getOwnColor = getOwnColor(dot, 'fill')) !== null && _getOwnColor !== void 0 ? _getOwnColor : seriesDotColors.fill,
stroke: (_getOwnColor2 = getOwnColor(dot, 'stroke')) !== null && _getOwnColor2 !== void 0 ? _getOwnColor2 : seriesDotColors.stroke
};
}
/**
* The default legend icon of Line and Area with a theme when they draw no dots:
* a plain line, because there is no dot to show.
* Without a theme, the default `line` icon stays, so that existing legends stay unchanged.
*
* @param dot the `dot` prop, after defaults
* @param isThemed true if the chart renders with a Recharts theme
* @returns `plainline` if the legend should show a plain line by default, otherwise undefined
*/
export function getNoDotsLegendType(dot, isThemed) {
return isThemed && !dot ? 'plainline' : undefined;
}
/**
* Renders a predefined symbol in place of a {@link Dot}, used when `dot` or `activeDot` asks for a symbol.
*
* It accepts the same props as Dot and handles them the same way, including event handlers.
* The symbol spans the same diameter as the dot would, so `circle` looks the same as the default dot.
*/
export function DotSymbol(props) {
var type = props.type,
cx = props.cx,
cy = props.cy,
r = props.r,
className = props.className,
rest = _objectWithoutProperties(props, _excluded);
return /*#__PURE__*/React.createElement(Symbols, _extends({}, svgPropertiesNoEvents(rest), adaptEventHandlers(props), {
className: clsx('recharts-dot', className),
type: type,
cx: cx,
cy: cy,
size: 2 * Number(r),
sizeType: "diameter"
}));
}