source: node_modules/recharts/lib/cartesian/ErrorBar.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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