| 1 | var _excluded = ["direction", "width", "dataKey", "isAnimationActive", "animationBegin", "animationDuration", "animationEasing"];
|
|---|
| 2 | 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); }
|
|---|
| 3 | 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; }
|
|---|
| 4 | 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; }
|
|---|
| 5 | 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; }
|
|---|
| 6 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 7 | 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); }
|
|---|
| 8 | 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; }
|
|---|
| 9 | 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; }
|
|---|
| 10 | /**
|
|---|
| 11 | * @fileOverview Render a group of error bar
|
|---|
| 12 | */
|
|---|
| 13 | import * as React from 'react';
|
|---|
| 14 | import { Layer } from '../container/Layer';
|
|---|
| 15 | import { ReportErrorBarSettings, useErrorBarContext } from '../context/ErrorBarContext';
|
|---|
| 16 | import { useXAxis, useYAxis } from '../hooks';
|
|---|
| 17 | import { resolveDefaultProps } from '../util/resolveDefaultProps';
|
|---|
| 18 | import { svgPropertiesNoEvents } from '../util/svgPropertiesNoEvents';
|
|---|
| 19 | import { useChartLayout } from '../context/chartLayoutContext';
|
|---|
| 20 | import { CSSTransitionAnimate } from '../animation/CSSTransitionAnimate';
|
|---|
| 21 | import { ZIndexLayer } from '../zIndex/ZIndexLayer';
|
|---|
| 22 | import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * So usually the direction is decided by the chart layout.
|
|---|
| 26 | * Horizontal layout means error bars are vertical means direction=y
|
|---|
| 27 | * Vertical layout means error bars are horizontal means direction=x
|
|---|
| 28 | *
|
|---|
| 29 | * Except! In Scatter chart, error bars can go both ways.
|
|---|
| 30 | *
|
|---|
| 31 | * So this property is only ever used in Scatter chart, and ignored elsewhere.
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * External ErrorBar props, visible for users of the library
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Props after defaults, and required props have been applied.
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | function ErrorBarImpl(props) {
|
|---|
| 43 | var {
|
|---|
| 44 | direction,
|
|---|
| 45 | width,
|
|---|
| 46 | dataKey,
|
|---|
| 47 | isAnimationActive,
|
|---|
| 48 | animationBegin,
|
|---|
| 49 | animationDuration,
|
|---|
| 50 | animationEasing
|
|---|
| 51 | } = props,
|
|---|
| 52 | others = _objectWithoutProperties(props, _excluded);
|
|---|
| 53 | var svgProps = svgPropertiesNoEvents(others);
|
|---|
| 54 | var {
|
|---|
| 55 | data,
|
|---|
| 56 | dataPointFormatter,
|
|---|
| 57 | xAxisId,
|
|---|
| 58 | yAxisId,
|
|---|
| 59 | errorBarOffset: offset
|
|---|
| 60 | } = useErrorBarContext();
|
|---|
| 61 | var xAxis = useXAxis(xAxisId);
|
|---|
| 62 | var yAxis = useYAxis(yAxisId);
|
|---|
| 63 | if ((xAxis === null || xAxis === void 0 ? void 0 : xAxis.scale) == null || (yAxis === null || yAxis === void 0 ? void 0 : yAxis.scale) == null || data == null) {
|
|---|
| 64 | return null;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // ErrorBar requires type number XAxis, why?
|
|---|
| 68 | if (direction === 'x' && xAxis.type !== 'number') {
|
|---|
| 69 | return null;
|
|---|
| 70 | }
|
|---|
| 71 | var errorBars = data.map((entry, dataIndex) => {
|
|---|
| 72 | var {
|
|---|
| 73 | x,
|
|---|
| 74 | y,
|
|---|
| 75 | value,
|
|---|
| 76 | errorVal
|
|---|
| 77 | } = dataPointFormatter(entry, dataKey, direction);
|
|---|
| 78 | if (!errorVal || x == null || y == null) {
|
|---|
| 79 | return null;
|
|---|
| 80 | }
|
|---|
| 81 | var lineCoordinates = [];
|
|---|
| 82 | var lowBound, highBound;
|
|---|
| 83 | if (Array.isArray(errorVal)) {
|
|---|
| 84 | var [low, high] = errorVal;
|
|---|
| 85 | if (low == null || high == null) {
|
|---|
| 86 | return null;
|
|---|
| 87 | }
|
|---|
| 88 | lowBound = low;
|
|---|
| 89 | highBound = high;
|
|---|
| 90 | } else {
|
|---|
| 91 | lowBound = highBound = errorVal;
|
|---|
| 92 | }
|
|---|
| 93 | if (direction === 'x') {
|
|---|
| 94 | // error bar for horizontal charts, the y is fixed, x is a range value
|
|---|
| 95 | var {
|
|---|
| 96 | scale
|
|---|
| 97 | } = xAxis;
|
|---|
| 98 | var yMid = y + offset;
|
|---|
| 99 | var yMin = yMid + width;
|
|---|
| 100 | var yMax = yMid - width;
|
|---|
| 101 | var xMin = scale.map(value - lowBound);
|
|---|
| 102 | var xMax = scale.map(value + highBound);
|
|---|
| 103 | if (xMin != null && xMax != null) {
|
|---|
| 104 | // the right line of |--|
|
|---|
| 105 | lineCoordinates.push({
|
|---|
| 106 | x1: xMax,
|
|---|
| 107 | y1: yMin,
|
|---|
| 108 | x2: xMax,
|
|---|
| 109 | y2: yMax
|
|---|
| 110 | });
|
|---|
| 111 | // the middle line of |--|
|
|---|
| 112 | lineCoordinates.push({
|
|---|
| 113 | x1: xMin,
|
|---|
| 114 | y1: yMid,
|
|---|
| 115 | x2: xMax,
|
|---|
| 116 | y2: yMid
|
|---|
| 117 | });
|
|---|
| 118 | // the left line of |--|
|
|---|
| 119 | lineCoordinates.push({
|
|---|
| 120 | x1: xMin,
|
|---|
| 121 | y1: yMin,
|
|---|
| 122 | x2: xMin,
|
|---|
| 123 | y2: yMax
|
|---|
| 124 | });
|
|---|
| 125 | }
|
|---|
| 126 | } else if (direction === 'y') {
|
|---|
| 127 | // error bar for horizontal charts, the x is fixed, y is a range value
|
|---|
| 128 | var {
|
|---|
| 129 | scale: _scale
|
|---|
| 130 | } = yAxis;
|
|---|
| 131 | var xMid = x + offset;
|
|---|
| 132 | var _xMin = xMid - width;
|
|---|
| 133 | var _xMax = xMid + width;
|
|---|
| 134 | var _yMin = _scale.map(value - lowBound);
|
|---|
| 135 | var _yMax = _scale.map(value + highBound);
|
|---|
| 136 | if (_yMin != null && _yMax != null) {
|
|---|
| 137 | // the top line
|
|---|
| 138 | lineCoordinates.push({
|
|---|
| 139 | x1: _xMin,
|
|---|
| 140 | y1: _yMax,
|
|---|
| 141 | x2: _xMax,
|
|---|
| 142 | y2: _yMax
|
|---|
| 143 | });
|
|---|
| 144 | // the middle line
|
|---|
| 145 | lineCoordinates.push({
|
|---|
| 146 | x1: xMid,
|
|---|
| 147 | y1: _yMin,
|
|---|
| 148 | x2: xMid,
|
|---|
| 149 | y2: _yMax
|
|---|
| 150 | });
|
|---|
| 151 | // the bottom line
|
|---|
| 152 | lineCoordinates.push({
|
|---|
| 153 | x1: _xMin,
|
|---|
| 154 | y1: _yMin,
|
|---|
| 155 | x2: _xMax,
|
|---|
| 156 | y2: _yMin
|
|---|
| 157 | });
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | var scaleDirection = direction === 'x' ? 'scaleX' : 'scaleY';
|
|---|
| 161 | var transformOrigin = "".concat(x + offset, "px ").concat(y + offset, "px");
|
|---|
| 162 | return /*#__PURE__*/React.createElement(Layer, _extends({
|
|---|
| 163 | className: "recharts-errorBar",
|
|---|
| 164 | key: "bar-".concat(x, "-").concat(y, "-").concat(value, "-").concat(dataIndex)
|
|---|
| 165 | }, svgProps), lineCoordinates.map((c, lineIndex) => {
|
|---|
| 166 | var lineStyle = isAnimationActive ? {
|
|---|
| 167 | transformOrigin
|
|---|
| 168 | } : undefined;
|
|---|
| 169 | return /*#__PURE__*/React.createElement(CSSTransitionAnimate, {
|
|---|
| 170 | animationId: "error-bar-".concat(direction, "_").concat(c.x1, "-").concat(c.x2, "-").concat(c.y1, "-").concat(c.y2),
|
|---|
| 171 | from: "".concat(scaleDirection, "(0)"),
|
|---|
| 172 | to: "".concat(scaleDirection, "(1)"),
|
|---|
| 173 | attributeName: "transform",
|
|---|
| 174 | begin: animationBegin,
|
|---|
| 175 | easing: animationEasing,
|
|---|
| 176 | isActive: isAnimationActive,
|
|---|
| 177 | duration: animationDuration,
|
|---|
| 178 | key: "errorbar-".concat(dataIndex, "-").concat(c.x1, "-").concat(c.y1, "-").concat(c.x2, "-").concat(c.y2, "-").concat(lineIndex)
|
|---|
| 179 | }, style => /*#__PURE__*/React.createElement("line", _extends({}, c, {
|
|---|
| 180 | style: _objectSpread(_objectSpread({}, lineStyle), style)
|
|---|
| 181 | })));
|
|---|
| 182 | }));
|
|---|
| 183 | });
|
|---|
| 184 | return /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 185 | className: "recharts-errorBars"
|
|---|
| 186 | }, errorBars);
|
|---|
| 187 | }
|
|---|
| 188 | function useErrorBarDirection(directionFromProps) {
|
|---|
| 189 | var layout = useChartLayout();
|
|---|
| 190 | if (directionFromProps != null) {
|
|---|
| 191 | return directionFromProps;
|
|---|
| 192 | }
|
|---|
| 193 | if (layout != null) {
|
|---|
| 194 | return layout === 'horizontal' ? 'y' : 'x';
|
|---|
| 195 | }
|
|---|
| 196 | return 'x';
|
|---|
| 197 | }
|
|---|
| 198 | export var errorBarDefaultProps = {
|
|---|
| 199 | stroke: 'black',
|
|---|
| 200 | strokeWidth: 1.5,
|
|---|
| 201 | width: 5,
|
|---|
| 202 | offset: 0,
|
|---|
| 203 | isAnimationActive: true,
|
|---|
| 204 | animationBegin: 0,
|
|---|
| 205 | animationDuration: 400,
|
|---|
| 206 | animationEasing: 'ease-in-out',
|
|---|
| 207 | zIndex: DefaultZIndexes.line
|
|---|
| 208 | };
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * ErrorBar renders whiskers to represent error margins on a chart.
|
|---|
| 212 | *
|
|---|
| 213 | * It must be a child of a graphical element.
|
|---|
| 214 | *
|
|---|
| 215 | * ErrorBar expects data in one of the following forms:
|
|---|
| 216 | * - Symmetric error bars: a single error value representing both lower and upper bounds.
|
|---|
| 217 | * - Asymmetric error bars: an array of two values representing lower and upper bounds separately. First value is the lower bound, second value is the upper bound.
|
|---|
| 218 | *
|
|---|
| 219 | * The values provided are relative to the main data value.
|
|---|
| 220 | * For example, if the main data value is 10 and the error value is 2,
|
|---|
| 221 | * the error bar will extend from 8 to 12 for symmetric error bars.
|
|---|
| 222 | *
|
|---|
| 223 | * In other words, what ErrorBar will render is:
|
|---|
| 224 | * - For symmetric error bars: [value - errorVal, value + errorVal]
|
|---|
| 225 | * - For asymmetric error bars: [value - errorVal[0], value + errorVal[1]]
|
|---|
| 226 | *
|
|---|
| 227 | * In stacked or ranged Bar charts, ErrorBar will use the higher data value
|
|---|
| 228 | * as the reference point for calculating the error bar positions.
|
|---|
| 229 | *
|
|---|
| 230 | * @consumes ErrorBarContext
|
|---|
| 231 | */
|
|---|
| 232 | export function ErrorBar(outsideProps) {
|
|---|
| 233 | var realDirection = useErrorBarDirection(outsideProps.direction);
|
|---|
| 234 | var props = resolveDefaultProps(outsideProps, errorBarDefaultProps);
|
|---|
| 235 | var {
|
|---|
| 236 | width,
|
|---|
| 237 | isAnimationActive,
|
|---|
| 238 | animationBegin,
|
|---|
| 239 | animationDuration,
|
|---|
| 240 | animationEasing,
|
|---|
| 241 | zIndex
|
|---|
| 242 | } = props;
|
|---|
| 243 | return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ReportErrorBarSettings, {
|
|---|
| 244 | dataKey: props.dataKey,
|
|---|
| 245 | direction: realDirection
|
|---|
| 246 | }), /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 247 | zIndex: zIndex
|
|---|
| 248 | }, /*#__PURE__*/React.createElement(ErrorBarImpl, _extends({}, props, {
|
|---|
| 249 | direction: realDirection,
|
|---|
| 250 | width: width,
|
|---|
| 251 | isAnimationActive: isAnimationActive,
|
|---|
| 252 | animationBegin: animationBegin,
|
|---|
| 253 | animationDuration: animationDuration,
|
|---|
| 254 | animationEasing: animationEasing
|
|---|
| 255 | }))));
|
|---|
| 256 | }
|
|---|
| 257 | ErrorBar.displayName = 'ErrorBar'; |
|---|