source: node_modules/recharts/es6/cartesian/CartesianGrid.js@ ba17441

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

Added visualizations

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