source: node_modules/recharts/lib/cartesian/CartesianGrid.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: 16.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.CartesianGrid = CartesianGrid;
7exports.defaultCartesianGridProps = void 0;
8var React = _interopRequireWildcard(require("react"));
9var _LogUtils = require("../util/LogUtils");
10var _DataUtils = require("../util/DataUtils");
11var _ChartUtils = require("../util/ChartUtils");
12var _getTicks = require("./getTicks");
13var _CartesianAxis = require("./CartesianAxis");
14var _chartLayoutContext = require("../context/chartLayoutContext");
15var _axisSelectors = require("../state/selectors/axisSelectors");
16var _hooks = require("../state/hooks");
17var _PanoramaContext = require("../context/PanoramaContext");
18var _resolveDefaultProps = require("../util/resolveDefaultProps");
19var _svgPropertiesNoEvents = require("../util/svgPropertiesNoEvents");
20var _isWellBehavedNumber = require("../util/isWellBehavedNumber");
21var _ZIndexLayer = require("../zIndex/ZIndexLayer");
22var _DefaultZIndexes = require("../zIndex/DefaultZIndexes");
23var _excluded = ["x1", "y1", "x2", "y2", "key"],
24 _excluded2 = ["offset"],
25 _excluded3 = ["xAxisId", "yAxisId"],
26 _excluded4 = ["xAxisId", "yAxisId"];
27function _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); }
28function 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; }
29function _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; }
30function _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; }
31function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
32function _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); }
33function _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); }
34function _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; }
35function _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; }
36/**
37 * The <CartesianGrid horizontal
38 */
39
40var Background = props => {
41 var {
42 fill
43 } = props;
44 if (!fill || fill === 'none') {
45 return null;
46 }
47 var {
48 fillOpacity,
49 x,
50 y,
51 width,
52 height,
53 ry
54 } = props;
55 return /*#__PURE__*/React.createElement("rect", {
56 x: x,
57 y: y,
58 ry: ry,
59 width: width,
60 height: height,
61 stroke: "none",
62 fill: fill,
63 fillOpacity: fillOpacity,
64 className: "recharts-cartesian-grid-bg"
65 });
66};
67function LineItem(_ref) {
68 var {
69 option,
70 lineItemProps
71 } = _ref;
72 var lineItem;
73 if (/*#__PURE__*/React.isValidElement(option)) {
74 // @ts-expect-error typescript does not see the props type when cloning an element
75 lineItem = /*#__PURE__*/React.cloneElement(option, lineItemProps);
76 } else if (typeof option === 'function') {
77 lineItem = option(lineItemProps);
78 } else {
79 var _svgPropertiesNoEvent;
80 var {
81 x1,
82 y1,
83 x2,
84 y2,
85 key
86 } = lineItemProps,
87 others = _objectWithoutProperties(lineItemProps, _excluded);
88 var _ref2 = (_svgPropertiesNoEvent = (0, _svgPropertiesNoEvents.svgPropertiesNoEvents)(others)) !== null && _svgPropertiesNoEvent !== void 0 ? _svgPropertiesNoEvent : {},
89 {
90 offset: __
91 } = _ref2,
92 restOfFilteredProps = _objectWithoutProperties(_ref2, _excluded2);
93 lineItem = /*#__PURE__*/React.createElement("line", _extends({}, restOfFilteredProps, {
94 x1: x1,
95 y1: y1,
96 x2: x2,
97 y2: y2,
98 fill: "none",
99 key: key
100 }));
101 }
102 return lineItem;
103}
104function HorizontalGridLines(props) {
105 var {
106 x,
107 width,
108 horizontal = true,
109 horizontalPoints
110 } = props;
111 if (!horizontal || !horizontalPoints || !horizontalPoints.length) {
112 return null;
113 }
114 var {
115 xAxisId,
116 yAxisId
117 } = props,
118 otherLineItemProps = _objectWithoutProperties(props, _excluded3);
119 var items = horizontalPoints.map((entry, i) => {
120 var lineItemProps = _objectSpread(_objectSpread({}, otherLineItemProps), {}, {
121 x1: x,
122 y1: entry,
123 x2: x + width,
124 y2: entry,
125 key: "line-".concat(i),
126 index: i
127 });
128 return /*#__PURE__*/React.createElement(LineItem, {
129 key: "line-".concat(i),
130 option: horizontal,
131 lineItemProps: lineItemProps
132 });
133 });
134 return /*#__PURE__*/React.createElement("g", {
135 className: "recharts-cartesian-grid-horizontal"
136 }, items);
137}
138function VerticalGridLines(props) {
139 var {
140 y,
141 height,
142 vertical = true,
143 verticalPoints
144 } = props;
145 if (!vertical || !verticalPoints || !verticalPoints.length) {
146 return null;
147 }
148 var {
149 xAxisId,
150 yAxisId
151 } = props,
152 otherLineItemProps = _objectWithoutProperties(props, _excluded4);
153 var items = verticalPoints.map((entry, i) => {
154 var lineItemProps = _objectSpread(_objectSpread({}, otherLineItemProps), {}, {
155 x1: entry,
156 y1: y,
157 x2: entry,
158 y2: y + height,
159 key: "line-".concat(i),
160 index: i
161 });
162 return /*#__PURE__*/React.createElement(LineItem, {
163 option: vertical,
164 lineItemProps: lineItemProps,
165 key: "line-".concat(i)
166 });
167 });
168 return /*#__PURE__*/React.createElement("g", {
169 className: "recharts-cartesian-grid-vertical"
170 }, items);
171}
172function HorizontalStripes(props) {
173 var {
174 horizontalFill,
175 fillOpacity,
176 x,
177 y,
178 width,
179 height,
180 horizontalPoints,
181 horizontal = true
182 } = props;
183 if (!horizontal || !horizontalFill || !horizontalFill.length || horizontalPoints == null) {
184 return null;
185 }
186 var roundedSortedHorizontalPoints = horizontalPoints.map(e => Math.round(e + y - y)).sort((a, b) => a - b);
187 // Why is this condition `!==` instead of `<=` ?
188 if (y !== roundedSortedHorizontalPoints[0]) {
189 roundedSortedHorizontalPoints.unshift(0);
190 }
191 var items = roundedSortedHorizontalPoints.map((entry, i) => {
192 // Why do we strip only the last stripe if it is invisible, and not all invisible stripes?
193 var nextPoint = roundedSortedHorizontalPoints[i + 1];
194 var lastStripe = nextPoint == null;
195 var lineHeight = lastStripe ? y + height - entry : nextPoint - entry;
196 if (lineHeight <= 0) {
197 return null;
198 }
199 var colorIndex = i % horizontalFill.length;
200 return /*#__PURE__*/React.createElement("rect", {
201 key: "react-".concat(i),
202 y: entry,
203 x: x,
204 height: lineHeight,
205 width: width,
206 stroke: "none",
207 fill: horizontalFill[colorIndex],
208 fillOpacity: fillOpacity,
209 className: "recharts-cartesian-grid-bg"
210 });
211 });
212 return /*#__PURE__*/React.createElement("g", {
213 className: "recharts-cartesian-gridstripes-horizontal"
214 }, items);
215}
216function VerticalStripes(props) {
217 var {
218 vertical = true,
219 verticalFill,
220 fillOpacity,
221 x,
222 y,
223 width,
224 height,
225 verticalPoints
226 } = props;
227 if (!vertical || !verticalFill || !verticalFill.length) {
228 return null;
229 }
230 var roundedSortedVerticalPoints = verticalPoints.map(e => Math.round(e + x - x)).sort((a, b) => a - b);
231 if (x !== roundedSortedVerticalPoints[0]) {
232 roundedSortedVerticalPoints.unshift(0);
233 }
234 var items = roundedSortedVerticalPoints.map((entry, i) => {
235 var nextPoint = roundedSortedVerticalPoints[i + 1];
236 var lastStripe = nextPoint == null;
237 var lineWidth = lastStripe ? x + width - entry : nextPoint - entry;
238 if (lineWidth <= 0) {
239 return null;
240 }
241 var colorIndex = i % verticalFill.length;
242 return /*#__PURE__*/React.createElement("rect", {
243 key: "react-".concat(i),
244 x: entry,
245 y: y,
246 width: lineWidth,
247 height: height,
248 stroke: "none",
249 fill: verticalFill[colorIndex],
250 fillOpacity: fillOpacity,
251 className: "recharts-cartesian-grid-bg"
252 });
253 });
254 return /*#__PURE__*/React.createElement("g", {
255 className: "recharts-cartesian-gridstripes-vertical"
256 }, items);
257}
258var defaultVerticalCoordinatesGenerator = (_ref3, syncWithTicks) => {
259 var {
260 xAxis,
261 width,
262 height,
263 offset
264 } = _ref3;
265 return (0, _ChartUtils.getCoordinatesOfGrid)((0, _getTicks.getTicks)(_objectSpread(_objectSpread(_objectSpread({}, _CartesianAxis.defaultCartesianAxisProps), xAxis), {}, {
266 ticks: (0, _ChartUtils.getTicksOfAxis)(xAxis, true),
267 viewBox: {
268 x: 0,
269 y: 0,
270 width,
271 height
272 }
273 })), offset.left, offset.left + offset.width, syncWithTicks);
274};
275var defaultHorizontalCoordinatesGenerator = (_ref4, syncWithTicks) => {
276 var {
277 yAxis,
278 width,
279 height,
280 offset
281 } = _ref4;
282 return (0, _ChartUtils.getCoordinatesOfGrid)((0, _getTicks.getTicks)(_objectSpread(_objectSpread(_objectSpread({}, _CartesianAxis.defaultCartesianAxisProps), yAxis), {}, {
283 ticks: (0, _ChartUtils.getTicksOfAxis)(yAxis, true),
284 viewBox: {
285 x: 0,
286 y: 0,
287 width,
288 height
289 }
290 })), offset.top, offset.top + offset.height, syncWithTicks);
291};
292var defaultCartesianGridProps = exports.defaultCartesianGridProps = {
293 horizontal: true,
294 vertical: true,
295 // The ordinates of horizontal grid lines
296 horizontalPoints: [],
297 // The abscissas of vertical grid lines
298 verticalPoints: [],
299 stroke: '#ccc',
300 fill: 'none',
301 // The fill of colors of grid lines
302 verticalFill: [],
303 horizontalFill: [],
304 xAxisId: 0,
305 yAxisId: 0,
306 syncWithTicks: false,
307 zIndex: _DefaultZIndexes.DefaultZIndexes.grid
308};
309
310/**
311 * Renders background grid with lines and fill colors in a Cartesian chart.
312 *
313 * @consumes CartesianChartContext
314 */
315function CartesianGrid(props) {
316 var chartWidth = (0, _chartLayoutContext.useChartWidth)();
317 var chartHeight = (0, _chartLayoutContext.useChartHeight)();
318 var offset = (0, _chartLayoutContext.useOffsetInternal)();
319 var propsIncludingDefaults = _objectSpread(_objectSpread({}, (0, _resolveDefaultProps.resolveDefaultProps)(props, defaultCartesianGridProps)), {}, {
320 x: (0, _DataUtils.isNumber)(props.x) ? props.x : offset.left,
321 y: (0, _DataUtils.isNumber)(props.y) ? props.y : offset.top,
322 width: (0, _DataUtils.isNumber)(props.width) ? props.width : offset.width,
323 height: (0, _DataUtils.isNumber)(props.height) ? props.height : offset.height
324 });
325 var {
326 xAxisId,
327 yAxisId,
328 x,
329 y,
330 width,
331 height,
332 syncWithTicks,
333 horizontalValues,
334 verticalValues
335 } = propsIncludingDefaults;
336 var isPanorama = (0, _PanoramaContext.useIsPanorama)();
337 var xAxis = (0, _hooks.useAppSelector)(state => (0, _axisSelectors.selectAxisPropsNeededForCartesianGridTicksGenerator)(state, 'xAxis', xAxisId, isPanorama));
338 var yAxis = (0, _hooks.useAppSelector)(state => (0, _axisSelectors.selectAxisPropsNeededForCartesianGridTicksGenerator)(state, 'yAxis', yAxisId, isPanorama));
339 if (!(0, _isWellBehavedNumber.isPositiveNumber)(width) || !(0, _isWellBehavedNumber.isPositiveNumber)(height) || !(0, _DataUtils.isNumber)(x) || !(0, _DataUtils.isNumber)(y)) {
340 return null;
341 }
342
343 /*
344 * verticalCoordinatesGenerator and horizontalCoordinatesGenerator are defined
345 * outside the propsIncludingDefaults because they were never part of the original props
346 * and they were never passed as a prop down to horizontal/vertical custom elements.
347 * If we add these two to propsIncludingDefaults then we are changing public API.
348 * Not a bad thing per se but also not necessary.
349 */
350 var verticalCoordinatesGenerator = propsIncludingDefaults.verticalCoordinatesGenerator || defaultVerticalCoordinatesGenerator;
351 var horizontalCoordinatesGenerator = propsIncludingDefaults.horizontalCoordinatesGenerator || defaultHorizontalCoordinatesGenerator;
352 var {
353 horizontalPoints,
354 verticalPoints
355 } = propsIncludingDefaults;
356
357 // No horizontal points are specified
358 if ((!horizontalPoints || !horizontalPoints.length) && typeof horizontalCoordinatesGenerator === 'function') {
359 var isHorizontalValues = horizontalValues && horizontalValues.length;
360 var generatorResult = horizontalCoordinatesGenerator({
361 yAxis: yAxis ? _objectSpread(_objectSpread({}, yAxis), {}, {
362 ticks: isHorizontalValues ? horizontalValues : yAxis.ticks
363 }) : undefined,
364 width: chartWidth !== null && chartWidth !== void 0 ? chartWidth : width,
365 height: chartHeight !== null && chartHeight !== void 0 ? chartHeight : height,
366 offset
367 }, isHorizontalValues ? true : syncWithTicks);
368 (0, _LogUtils.warn)(Array.isArray(generatorResult), "horizontalCoordinatesGenerator should return Array but instead it returned [".concat(typeof generatorResult, "]"));
369 if (Array.isArray(generatorResult)) {
370 horizontalPoints = generatorResult;
371 }
372 }
373
374 // No vertical points are specified
375 if ((!verticalPoints || !verticalPoints.length) && typeof verticalCoordinatesGenerator === 'function') {
376 var isVerticalValues = verticalValues && verticalValues.length;
377 var _generatorResult = verticalCoordinatesGenerator({
378 xAxis: xAxis ? _objectSpread(_objectSpread({}, xAxis), {}, {
379 ticks: isVerticalValues ? verticalValues : xAxis.ticks
380 }) : undefined,
381 width: chartWidth !== null && chartWidth !== void 0 ? chartWidth : width,
382 height: chartHeight !== null && chartHeight !== void 0 ? chartHeight : height,
383 offset
384 }, isVerticalValues ? true : syncWithTicks);
385 (0, _LogUtils.warn)(Array.isArray(_generatorResult), "verticalCoordinatesGenerator should return Array but instead it returned [".concat(typeof _generatorResult, "]"));
386 if (Array.isArray(_generatorResult)) {
387 verticalPoints = _generatorResult;
388 }
389 }
390 return /*#__PURE__*/React.createElement(_ZIndexLayer.ZIndexLayer, {
391 zIndex: propsIncludingDefaults.zIndex
392 }, /*#__PURE__*/React.createElement("g", {
393 className: "recharts-cartesian-grid"
394 }, /*#__PURE__*/React.createElement(Background, {
395 fill: propsIncludingDefaults.fill,
396 fillOpacity: propsIncludingDefaults.fillOpacity,
397 x: propsIncludingDefaults.x,
398 y: propsIncludingDefaults.y,
399 width: propsIncludingDefaults.width,
400 height: propsIncludingDefaults.height,
401 ry: propsIncludingDefaults.ry
402 }), /*#__PURE__*/React.createElement(HorizontalStripes, _extends({}, propsIncludingDefaults, {
403 horizontalPoints: horizontalPoints
404 })), /*#__PURE__*/React.createElement(VerticalStripes, _extends({}, propsIncludingDefaults, {
405 verticalPoints: verticalPoints
406 })), /*#__PURE__*/React.createElement(HorizontalGridLines, _extends({}, propsIncludingDefaults, {
407 offset: offset,
408 horizontalPoints: horizontalPoints,
409 xAxis: xAxis,
410 yAxis: yAxis
411 })), /*#__PURE__*/React.createElement(VerticalGridLines, _extends({}, propsIncludingDefaults, {
412 offset: offset,
413 verticalPoints: verticalPoints,
414 xAxis: xAxis,
415 yAxis: yAxis
416 }))));
417}
418CartesianGrid.displayName = 'CartesianGrid';
Note: See TracBrowser for help on using the repository browser.