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