UNPKG

recharts

Version:
488 lines (477 loc) • 23.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CartesianGrid = CartesianGrid; exports.defaultCartesianGridProps = void 0; var React = _interopRequireWildcard(require("react")); var _LogUtils = require("../util/LogUtils"); var _DataUtils = require("../util/DataUtils"); var _ChartUtils = require("../util/ChartUtils"); var _getTicks = require("./getTicks"); var _CartesianAxis = require("./CartesianAxis"); var _chartLayoutContext = require("../context/chartLayoutContext"); var _axisSelectors = require("../state/selectors/axisSelectors"); var _zoomSelectors = require("../state/selectors/zoomSelectors"); var _hooks = require("../state/hooks"); var _PanoramaContext = require("../context/PanoramaContext"); var _ClipPathProvider = require("../container/ClipPathProvider"); var _resolveDefaultProps = require("../util/resolveDefaultProps"); var _svgPropertiesNoEvents = require("../util/svgPropertiesNoEvents"); var _isWellBehavedNumber = require("../util/isWellBehavedNumber"); var _propsAreEqual = require("../util/propsAreEqual"); var _ZIndexLayer = require("../zIndex/ZIndexLayer"); var _DefaultZIndexes = require("../zIndex/DefaultZIndexes"); var _themeFill = require("../theme/themeFill"); var _useBackwardsCompatibleTheme = require("../theme/useBackwardsCompatibleTheme"); var _fillDefinitions = require("../fill/fillDefinitions"); var _excluded = ["x1", "y1", "x2", "y2", "key"], _excluded2 = ["offset"], _excluded3 = ["xAxisId", "yAxisId"], _excluded4 = ["xAxisId", "yAxisId"]; function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, 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); } 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; } /** * The <CartesianGrid horizontal * @public */ /** * @public */ // We exclude strokeDasharray here because Recharts allows array of numbers while DOM does not /** * @public */ /** * Grid props after the fills are resolved: `fill` is the base color, and the stripes are colors or `url(#…)`. */ var Background = props => { var fill = props.fill; if (!fill || fill === 'none') { return null; } var fillOpacity = props.fillOpacity, x = props.x, y = props.y, width = props.width, height = props.height, ry = props.ry; return /*#__PURE__*/React.createElement("rect", { x: x, y: y, ry: ry, width: width, height: height, stroke: "none", fill: fill, fillOpacity: fillOpacity, className: "recharts-cartesian-grid-bg" }); }; function LineItem(_ref) { var option = _ref.option, lineItemProps = _ref.lineItemProps; var lineItem; if (/*#__PURE__*/React.isValidElement(option)) { // @ts-expect-error TypeScript does not see the props type when cloning an element lineItem = /*#__PURE__*/React.cloneElement(option, lineItemProps); } else if (typeof option === 'function') { lineItem = option(lineItemProps); } else { var _svgPropertiesNoEvent; var x1 = lineItemProps.x1, y1 = lineItemProps.y1, x2 = lineItemProps.x2, y2 = lineItemProps.y2, key = lineItemProps.key, others = _objectWithoutProperties(lineItemProps, _excluded); var _ref2 = (_svgPropertiesNoEvent = (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(others)) !== null && _svgPropertiesNoEvent !== void 0 ? _svgPropertiesNoEvent : {}, __ = _ref2.offset, restOfFilteredProps = _objectWithoutProperties(_ref2, _excluded2); var strokeDasharray = Array.isArray(restOfFilteredProps.strokeDasharray) ? restOfFilteredProps.strokeDasharray.join(',') : restOfFilteredProps.strokeDasharray; lineItem = /*#__PURE__*/React.createElement("line", _extends({}, restOfFilteredProps, { strokeDasharray: strokeDasharray, x1: x1, y1: y1, x2: x2, y2: y2, fill: "none", key: key })); } return lineItem; } function HorizontalGridLines(props) { var x = props.x, width = props.width, _props$horizontal = props.horizontal, horizontal = _props$horizontal === void 0 ? true : _props$horizontal, horizontalPoints = props.horizontalPoints; if (!horizontal || !horizontalPoints || !horizontalPoints.length) { return null; } var xAxisId = props.xAxisId, yAxisId = props.yAxisId, otherLineItemProps = _objectWithoutProperties(props, _excluded3); var items = horizontalPoints.map((entry, i) => { var lineItemProps = _objectSpread(_objectSpread({}, otherLineItemProps), {}, { x1: x, y1: entry, x2: x + width, y2: entry, key: "line-".concat(i), index: i }); return /*#__PURE__*/React.createElement(LineItem, { key: "line-".concat(i), option: horizontal, lineItemProps: lineItemProps }); }); return /*#__PURE__*/React.createElement("g", { className: "recharts-cartesian-grid-horizontal" }, items); } function VerticalGridLines(props) { var y = props.y, height = props.height, _props$vertical = props.vertical, vertical = _props$vertical === void 0 ? true : _props$vertical, verticalPoints = props.verticalPoints; if (!vertical || !verticalPoints || !verticalPoints.length) { return null; } var xAxisId = props.xAxisId, yAxisId = props.yAxisId, otherLineItemProps = _objectWithoutProperties(props, _excluded4); var items = verticalPoints.map((entry, i) => { var lineItemProps = _objectSpread(_objectSpread({}, otherLineItemProps), {}, { x1: entry, y1: y, x2: entry, y2: y + height, key: "line-".concat(i), index: i }); return /*#__PURE__*/React.createElement(LineItem, { option: vertical, lineItemProps: lineItemProps, key: "line-".concat(i) }); }); return /*#__PURE__*/React.createElement("g", { className: "recharts-cartesian-grid-vertical" }, items); } function HorizontalStripes(props) { var horizontalFill = props.horizontalFill, fillOpacity = props.fillOpacity, x = props.x, y = props.y, width = props.width, height = props.height, horizontalPoints = props.horizontalPoints, _props$horizontal2 = props.horizontal, horizontal = _props$horizontal2 === void 0 ? true : _props$horizontal2; if (!horizontal || !horizontalFill || !horizontalFill.length || horizontalPoints == null) { return null; } var roundedSortedHorizontalPoints = horizontalPoints.map(e => Math.round(e + y - y)).sort((a, b) => a - b); // Why is this condition `!==` instead of `<=` ? if (y !== roundedSortedHorizontalPoints[0]) { roundedSortedHorizontalPoints.unshift(0); } var items = roundedSortedHorizontalPoints.map((entry, i) => { // Why do we strip only the last stripe if it is invisible, and not all invisible stripes? var nextPoint = roundedSortedHorizontalPoints[i + 1]; var lastStripe = nextPoint == null; var lineHeight = lastStripe ? y + height - entry : nextPoint - entry; if (lineHeight <= 0) { return null; } var colorIndex = i % horizontalFill.length; return /*#__PURE__*/React.createElement("rect", { key: "react-".concat(i), y: entry, x: x, height: lineHeight, width: width, stroke: "none", fill: horizontalFill[colorIndex], fillOpacity: fillOpacity, className: "recharts-cartesian-grid-bg" }); }); return /*#__PURE__*/React.createElement("g", { className: "recharts-cartesian-gridstripes-horizontal" }, items); } function VerticalStripes(props) { var _props$vertical2 = props.vertical, vertical = _props$vertical2 === void 0 ? true : _props$vertical2, verticalFill = props.verticalFill, fillOpacity = props.fillOpacity, x = props.x, y = props.y, width = props.width, height = props.height, verticalPoints = props.verticalPoints; if (!vertical || !verticalFill || !verticalFill.length) { return null; } var roundedSortedVerticalPoints = verticalPoints.map(e => Math.round(e + x - x)).sort((a, b) => a - b); if (x !== roundedSortedVerticalPoints[0]) { roundedSortedVerticalPoints.unshift(0); } var items = roundedSortedVerticalPoints.map((entry, i) => { var nextPoint = roundedSortedVerticalPoints[i + 1]; var lastStripe = nextPoint == null; var lineWidth = lastStripe ? x + width - entry : nextPoint - entry; if (lineWidth <= 0) { return null; } var colorIndex = i % verticalFill.length; return /*#__PURE__*/React.createElement("rect", { key: "react-".concat(i), x: entry, y: y, width: lineWidth, height: height, stroke: "none", fill: verticalFill[colorIndex], fillOpacity: fillOpacity, className: "recharts-cartesian-grid-bg" }); }); return /*#__PURE__*/React.createElement("g", { className: "recharts-cartesian-gridstripes-vertical" }, items); } /** * Reads fontSize/letterSpacing from an axis's `tick` prop when it is a plain * style object (e.g. `tick={{ fontSize: 30 }}`). Unlike CartesianAxis, the grid * has no DOM to measure, so function/element ticks are not covered. */ function getTickFontStyle(tick) { if (tick == null || typeof tick !== 'object' || /*#__PURE__*/React.isValidElement(tick)) { return {}; } var fontSize = 'fontSize' in tick && tick.fontSize != null ? "".concat(tick.fontSize).replace(/^(\d+(\.\d+)?)$/, '$1px') : undefined; var letterSpacing = 'letterSpacing' in tick && tick.letterSpacing != null ? String(tick.letterSpacing) : undefined; return { fontSize, letterSpacing }; } var defaultVerticalCoordinatesGenerator = (_ref3, syncWithTicks) => { var xAxis = _ref3.xAxis, width = _ref3.width, height = _ref3.height, offset = _ref3.offset; var _getTickFontStyle = getTickFontStyle(xAxis === null || xAxis === void 0 ? void 0 : xAxis.tick), fontSize = _getTickFontStyle.fontSize, letterSpacing = _getTickFontStyle.letterSpacing; return (0, _ChartUtils.getCoordinatesOfGrid)((0, _getTicks.getTicks)(_objectSpread(_objectSpread(_objectSpread({}, _CartesianAxis.defaultCartesianAxisProps), xAxis), {}, { ticks: (0, _ChartUtils.getTicksOfAxis)(xAxis, true), viewBox: { x: 0, y: 0, width, height } }), fontSize, letterSpacing), offset.left, offset.left + offset.width, syncWithTicks); }; var defaultHorizontalCoordinatesGenerator = (_ref4, syncWithTicks) => { var yAxis = _ref4.yAxis, width = _ref4.width, height = _ref4.height, offset = _ref4.offset; var _getTickFontStyle2 = getTickFontStyle(yAxis === null || yAxis === void 0 ? void 0 : yAxis.tick), fontSize = _getTickFontStyle2.fontSize, letterSpacing = _getTickFontStyle2.letterSpacing; return (0, _ChartUtils.getCoordinatesOfGrid)((0, _getTicks.getTicks)(_objectSpread(_objectSpread(_objectSpread({}, _CartesianAxis.defaultCartesianAxisProps), yAxis), {}, { ticks: (0, _ChartUtils.getTicksOfAxis)(yAxis, true), viewBox: { x: 0, y: 0, width, height } }), fontSize, letterSpacing), offset.top, offset.top + offset.height, syncWithTicks); }; function areGridAxisScalesEqual(a, b, realScaleType) { if (a === b) { return true; } if (realScaleType == null) { return false; } return (0, _propsAreEqual.areScalesApproximatelyEqual)(a, b); } function areGridAxisPropsEqual(a, b) { if (a === b) { return true; } if (a == null || b == null) { return false; } return a.angle === b.angle && a.axisType === b.axisType && a.interval === b.interval && a.isCategorical === b.isCategorical && a.minTickGap === b.minTickGap && a.orientation === b.orientation && a.realScaleType === b.realScaleType && a.tick === b.tick && a.tickCount === b.tickCount && a.tickFormatter === b.tickFormatter && a.type === b.type && a.unit === b.unit && (0, _propsAreEqual.areValueArraysApproximatelyEqual)(a.categoricalDomain, b.categoricalDomain) && (0, _propsAreEqual.areValueArraysApproximatelyEqual)(a.duplicateDomain, b.duplicateDomain) && (0, _propsAreEqual.areValueArraysApproximatelyEqual)(a.niceTicks, b.niceTicks) && (0, _propsAreEqual.areValueArraysApproximatelyEqual)(a.range, b.range) && (0, _propsAreEqual.areValueArraysApproximatelyEqual)(a.ticks, b.ticks) && areGridAxisScalesEqual(a.scale, b.scale, a.realScaleType); } var defaultCartesianGridProps = exports.defaultCartesianGridProps = { horizontal: true, vertical: true, // The ordinates of horizontal grid lines horizontalPoints: [], // The abscissas of vertical grid lines verticalPoints: [], // The fill of colors of grid lines verticalFill: [], horizontalFill: [], xAxisId: 0, yAxisId: 0, syncWithTicks: false, zIndex: _DefaultZIndexes.DefaultZIndexes.grid }; var defaultLegacyGridThemeProps = { fill: 'none', stroke: '#ccc' }; /** * Renders background grid with lines and fill colors in a Cartesian chart. * * @consumes CartesianChartContext * @public */ function CartesianGrid(propsWithFillObjects) { var _propsWithFillObjects; var chartWidth = (0, _chartLayoutContext.useChartWidth)(); var chartHeight = (0, _chartLayoutContext.useChartHeight)(); var offset = (0, _chartLayoutContext.useOffsetInternal)(); var horizontalStripes = (0, _fillDefinitions.useFillPaints)(propsWithFillObjects.horizontalFill, 'recharts-cartesian-grid-horizontal-fill'); var verticalStripes = (0, _fillDefinitions.useFillPaints)(propsWithFillObjects.verticalFill, 'recharts-cartesian-grid-vertical-fill'); // From here on, `fill` is the base color, and the stripes are colors or `url(#…)` var props = _objectSpread(_objectSpread({}, propsWithFillObjects), {}, { fill: (0, _fillDefinitions.splitFill)(propsWithFillObjects.fill).color, horizontalFill: propsWithFillObjects.horizontalFill == null ? undefined : horizontalStripes.paints, verticalFill: propsWithFillObjects.verticalFill == null ? undefined : verticalStripes.paints }); var propsIncludingDefaults = _objectSpread(_objectSpread({}, (0, _resolveDefaultProps.resolveDefaultProps)(props, defaultCartesianGridProps)), {}, { x: (0, _DataUtils.isNumber)(props.x) ? props.x : offset.left, y: (0, _DataUtils.isNumber)(props.y) ? props.y : offset.top, width: (0, _DataUtils.isNumber)(props.width) ? props.width : offset.width, height: (0, _DataUtils.isNumber)(props.height) ? props.height : offset.height }); var xAxisId = propsIncludingDefaults.xAxisId, yAxisId = propsIncludingDefaults.yAxisId, x = propsIncludingDefaults.x, y = propsIncludingDefaults.y, width = propsIncludingDefaults.width, height = propsIncludingDefaults.height, syncWithTicks = propsIncludingDefaults.syncWithTicks, horizontalValues = propsIncludingDefaults.horizontalValues, verticalValues = propsIncludingDefaults.verticalValues; var isPanorama = (0, _PanoramaContext.useIsPanorama)(); var isZoomed = (0, _hooks.useAppSelector)(_zoomSelectors.selectIsZoomed); var clipPathId = (0, _ClipPathProvider.useClipPathId)(); var selectXAxis = React.useCallback(state => (0, _axisSelectors.selectAxisPropsNeededForCartesianGridTicksGenerator)(state, 'xAxis', xAxisId, isPanorama), [isPanorama, xAxisId]); var selectYAxis = React.useCallback(state => (0, _axisSelectors.selectAxisPropsNeededForCartesianGridTicksGenerator)(state, 'yAxis', yAxisId, isPanorama), [isPanorama, yAxisId]); var xAxis = (0, _hooks.useAppSelector)(selectXAxis, areGridAxisPropsEqual); var yAxis = (0, _hooks.useAppSelector)(selectYAxis, areGridAxisPropsEqual); var themeProps = (0, _useBackwardsCompatibleTheme.useBackwardsCompatibleTheme)(theme => (0, _themeFill.withFillColor)(theme.grid), propsIncludingDefaults, defaultLegacyGridThemeProps); // Without a fill of its own, the grid paints with the fill object of the theme, if there is one var themeFillObject = (0, _themeFill.useThemeFillObject)(theme => theme.grid); var background = (0, _fillDefinitions.useFillPaint)((_propsWithFillObjects = propsWithFillObjects.fill) !== null && _propsWithFillObjects !== void 0 ? _propsWithFillObjects : themeFillObject, themeProps.fill, 'recharts-cartesian-grid-fill'); if (!(0, _isWellBehavedNumber.isPositiveNumber)(width) || !(0, _isWellBehavedNumber.isPositiveNumber)(height) || !(0, _DataUtils.isNumber)(x) || !(0, _DataUtils.isNumber)(y)) { return null; } /* * verticalCoordinatesGenerator and horizontalCoordinatesGenerator are defined * outside the propsIncludingDefaults because they were never part of the original props * and they were never passed as a prop down to horizontal/vertical custom elements. * If we add these two to propsIncludingDefaults then we are changing public API. * Not a bad thing per se but also not necessary. */ var verticalCoordinatesGenerator = propsIncludingDefaults.verticalCoordinatesGenerator || defaultVerticalCoordinatesGenerator; var horizontalCoordinatesGenerator = propsIncludingDefaults.horizontalCoordinatesGenerator || defaultHorizontalCoordinatesGenerator; var horizontalPoints = propsIncludingDefaults.horizontalPoints, verticalPoints = propsIncludingDefaults.verticalPoints; // No horizontal points are specified if ((!horizontalPoints || !horizontalPoints.length) && typeof horizontalCoordinatesGenerator === 'function') { var isHorizontalValues = horizontalValues && horizontalValues.length; var generatorResult = horizontalCoordinatesGenerator({ yAxis: yAxis ? _objectSpread(_objectSpread({}, yAxis), {}, { ticks: isHorizontalValues ? horizontalValues : yAxis.ticks }) : undefined, width: chartWidth !== null && chartWidth !== void 0 ? chartWidth : width, height: chartHeight !== null && chartHeight !== void 0 ? chartHeight : height, offset }, isHorizontalValues ? true : syncWithTicks); (0, _LogUtils.warn)(Array.isArray(generatorResult), "horizontalCoordinatesGenerator should return Array but instead it returned [".concat(typeof generatorResult, "]")); if (Array.isArray(generatorResult)) { horizontalPoints = generatorResult; } } // No vertical points are specified if ((!verticalPoints || !verticalPoints.length) && typeof verticalCoordinatesGenerator === 'function') { var isVerticalValues = verticalValues && verticalValues.length; var _generatorResult = verticalCoordinatesGenerator({ xAxis: xAxis ? _objectSpread(_objectSpread({}, xAxis), {}, { ticks: isVerticalValues ? verticalValues : xAxis.ticks }) : undefined, width: chartWidth !== null && chartWidth !== void 0 ? chartWidth : width, height: chartHeight !== null && chartHeight !== void 0 ? chartHeight : height, offset }, isVerticalValues ? true : syncWithTicks); (0, _LogUtils.warn)(Array.isArray(_generatorResult), "verticalCoordinatesGenerator should return Array but instead it returned [".concat(typeof _generatorResult, "]")); if (Array.isArray(_generatorResult)) { verticalPoints = _generatorResult; } } /* * When zoomed, the generators place grid lines across the stretched range, so most land outside * the plotting area. The filter is a coarse cull so a deep zoom doesn't render hundreds of * invisible lines; the clip path below cuts the survivors exactly at the plot edge, whatever * their stroke width. */ if (isZoomed) { var _horizontalPoints, _verticalPoints; horizontalPoints = (_horizontalPoints = horizontalPoints) === null || _horizontalPoints === void 0 ? void 0 : _horizontalPoints.filter(p => p >= y - 0.5 && p <= y + height + 0.5); verticalPoints = (_verticalPoints = verticalPoints) === null || _verticalPoints === void 0 ? void 0 : _verticalPoints.filter(p => p >= x - 0.5 && p <= x + width + 0.5); } return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, { zIndex: propsIncludingDefaults.zIndex }, /*#__PURE__*/React.createElement("g", { className: "recharts-cartesian-grid", clipPath: isZoomed && clipPathId != null ? "url(#".concat(clipPathId, ")") : undefined }, (background.definition != null || horizontalStripes.definitions != null || verticalStripes.definitions != null) && /*#__PURE__*/React.createElement("defs", null, background.definition, horizontalStripes.definitions, verticalStripes.definitions), /*#__PURE__*/React.createElement(Background, { fill: background.paint, fillOpacity: themeProps.fillOpacity, x: propsIncludingDefaults.x, y: propsIncludingDefaults.y, width: propsIncludingDefaults.width, height: propsIncludingDefaults.height, ry: propsIncludingDefaults.ry }), /*#__PURE__*/React.createElement(HorizontalStripes, _extends({}, propsIncludingDefaults, { horizontalPoints: horizontalPoints })), /*#__PURE__*/React.createElement(VerticalStripes, _extends({}, propsIncludingDefaults, { verticalPoints: verticalPoints })), /*#__PURE__*/React.createElement(HorizontalGridLines, _extends({}, propsIncludingDefaults, themeProps, { offset: offset, horizontalPoints: horizontalPoints, xAxis: xAxis, yAxis: yAxis })), /*#__PURE__*/React.createElement(VerticalGridLines, _extends({}, propsIncludingDefaults, themeProps, { offset: offset, verticalPoints: verticalPoints, xAxis: xAxis, yAxis: yAxis })))); } CartesianGrid.displayName = 'CartesianGrid';