| 1 | 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); }
|
|---|
| 2 | 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; }
|
|---|
| 3 | 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; }
|
|---|
| 4 | 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; }
|
|---|
| 5 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 6 | 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); }
|
|---|
| 7 | /**
|
|---|
| 8 | * @fileOverview Curve
|
|---|
| 9 | */
|
|---|
| 10 | import * as React from 'react';
|
|---|
| 11 | import { line as shapeLine, area as shapeArea, curveBasisClosed, curveBasisOpen, curveBasis, curveBumpX, curveBumpY, curveLinearClosed, curveLinear, curveMonotoneX, curveMonotoneY, curveNatural, curveStep, curveStepAfter, curveStepBefore } from 'victory-vendor/d3-shape';
|
|---|
| 12 | import { clsx } from 'clsx';
|
|---|
| 13 | import { adaptEventHandlers } from '../util/types';
|
|---|
| 14 | import { isNumber, upperFirst } from '../util/DataUtils';
|
|---|
| 15 | import { isWellBehavedNumber } from '../util/isWellBehavedNumber';
|
|---|
| 16 | import { svgPropertiesNoEvents } from '../util/svgPropertiesNoEvents';
|
|---|
| 17 | import { useChartLayout } from '../context/chartLayoutContext';
|
|---|
| 18 | var CURVE_FACTORIES = {
|
|---|
| 19 | curveBasisClosed,
|
|---|
| 20 | curveBasisOpen,
|
|---|
| 21 | curveBasis,
|
|---|
| 22 | curveBumpX,
|
|---|
| 23 | curveBumpY,
|
|---|
| 24 | curveLinearClosed,
|
|---|
| 25 | curveLinear,
|
|---|
| 26 | curveMonotoneX,
|
|---|
| 27 | curveMonotoneY,
|
|---|
| 28 | curveNatural,
|
|---|
| 29 | curveStep,
|
|---|
| 30 | curveStepAfter,
|
|---|
| 31 | curveStepBefore
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * @inline
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | var defined = p => isWellBehavedNumber(p.x) && isWellBehavedNumber(p.y);
|
|---|
| 39 | var areaDefined = d => d.base != null && defined(d.base) && defined(d);
|
|---|
| 40 | var getX = p => p.x;
|
|---|
| 41 | var getY = p => p.y;
|
|---|
| 42 | var getCurveFactory = (type, layout) => {
|
|---|
| 43 | if (typeof type === 'function') {
|
|---|
| 44 | return type;
|
|---|
| 45 | }
|
|---|
| 46 | var name = "curve".concat(upperFirst(type));
|
|---|
| 47 | if ((name === 'curveMonotone' || name === 'curveBump') && layout) {
|
|---|
| 48 | var factory = CURVE_FACTORIES["".concat(name).concat(layout === 'vertical' ? 'Y' : 'X')];
|
|---|
| 49 | if (factory) {
|
|---|
| 50 | return factory;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | return CURVE_FACTORIES[name] || curveLinear;
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | // Mouse event handlers receive the full Props, including the event handlers themselves.
|
|---|
| 57 |
|
|---|
| 58 | export var defaultCurveProps = {
|
|---|
| 59 | connectNulls: false,
|
|---|
| 60 | type: 'linear'
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Calculate the path of curve. Returns null if points is an empty array.
|
|---|
| 65 | * @return path or null
|
|---|
| 66 | */
|
|---|
| 67 | export var getPath = _ref => {
|
|---|
| 68 | var {
|
|---|
| 69 | type = defaultCurveProps.type,
|
|---|
| 70 | points = [],
|
|---|
| 71 | baseLine,
|
|---|
| 72 | layout,
|
|---|
| 73 | connectNulls = defaultCurveProps.connectNulls
|
|---|
| 74 | } = _ref;
|
|---|
| 75 | var curveFactory = getCurveFactory(type, layout);
|
|---|
| 76 | var formatPoints = connectNulls ? points.filter(defined) : points;
|
|---|
| 77 |
|
|---|
| 78 | // When dealing with an area chart (where `baseLine` is an array),
|
|---|
| 79 | // we need to pair points with their corresponding `baseLine` points first.
|
|---|
| 80 | // This is to ensure that we filter points and their baseline counterparts together,
|
|---|
| 81 | // preventing errors from mismatched array lengths and ensuring `defined` checks both.
|
|---|
| 82 | if (Array.isArray(baseLine)) {
|
|---|
| 83 | var _lineFunction;
|
|---|
| 84 | var areaPoints = points.map((entry, index) => _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 85 | base: baseLine[index]
|
|---|
| 86 | }));
|
|---|
| 87 | if (layout === 'vertical') {
|
|---|
| 88 | _lineFunction = shapeArea().y(getY).x1(getX).x0(d => d.base.x);
|
|---|
| 89 | } else {
|
|---|
| 90 | _lineFunction = shapeArea().x(getX).y1(getY).y0(d => d.base.y);
|
|---|
| 91 | }
|
|---|
| 92 | /*
|
|---|
| 93 | * What happens here is that the `.defined()` call will make it so that this function can accept
|
|---|
| 94 | * nullable points, and internally it will filter them out and skip when generating the path.
|
|---|
| 95 | * So on the input it accepts NullableCoordinate, but it never calls getX/getY on null points because of the defined() filter.
|
|---|
| 96 | *
|
|---|
| 97 | * The d3 type definition has only one generic so it doesn't allow to describe this properly.
|
|---|
| 98 | * However. d3 types are mutable, but we can pretend that they are not, and we can pretend
|
|---|
| 99 | * that calling defined() returns a new function with a different generic type.
|
|---|
| 100 | */
|
|---|
| 101 | // @ts-expect-error the defined call changes the generic type internally but d3 types don't reflect that
|
|---|
| 102 | var _nullableLineFunction = _lineFunction.defined(areaDefined).curve(curveFactory);
|
|---|
| 103 | var finalPoints = connectNulls ? areaPoints.filter(areaDefined) : areaPoints;
|
|---|
| 104 | return _nullableLineFunction(finalPoints);
|
|---|
| 105 | }
|
|---|
| 106 | var lineFunction;
|
|---|
| 107 | if (layout === 'vertical' && isNumber(baseLine)) {
|
|---|
| 108 | lineFunction = shapeArea().y(getY).x1(getX).x0(baseLine);
|
|---|
| 109 | } else if (isNumber(baseLine)) {
|
|---|
| 110 | lineFunction = shapeArea().x(getX).y1(getY).y0(baseLine);
|
|---|
| 111 | } else {
|
|---|
| 112 | lineFunction = shapeLine().x(getX).y(getY);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | // @ts-expect-error the defined call changes the generic type internally but d3 types don't reflect that
|
|---|
| 116 | var nullableLineFunction = lineFunction.defined(defined).curve(curveFactory);
|
|---|
| 117 | return nullableLineFunction(formatPoints);
|
|---|
| 118 | };
|
|---|
| 119 | export var Curve = props => {
|
|---|
| 120 | var {
|
|---|
| 121 | className,
|
|---|
| 122 | points,
|
|---|
| 123 | path,
|
|---|
| 124 | pathRef
|
|---|
| 125 | } = props;
|
|---|
| 126 | var layout = useChartLayout();
|
|---|
| 127 | if ((!points || !points.length) && !path) {
|
|---|
| 128 | return null;
|
|---|
| 129 | }
|
|---|
| 130 | var getPathInput = {
|
|---|
| 131 | type: props.type,
|
|---|
| 132 | points: props.points,
|
|---|
| 133 | baseLine: props.baseLine,
|
|---|
| 134 | layout: props.layout || layout,
|
|---|
| 135 | connectNulls: props.connectNulls
|
|---|
| 136 | };
|
|---|
| 137 | var realPath = points && points.length ? getPath(getPathInput) : path;
|
|---|
| 138 | return /*#__PURE__*/React.createElement("path", _extends({}, svgPropertiesNoEvents(props), adaptEventHandlers(props), {
|
|---|
| 139 | className: clsx('recharts-curve', className),
|
|---|
| 140 | d: realPath === null ? undefined : realPath,
|
|---|
| 141 | ref: pathRef
|
|---|
| 142 | }));
|
|---|
| 143 | }; |
|---|