source: node_modules/recharts/es6/util/isDomainSpecifiedByUser.js@ a762898

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

Added visualizations

  • Property mode set to 100644
File size: 7.0 KB
Line 
1import { MAX_VALUE_REG, MIN_VALUE_REG } from './ChartUtils';
2import { isNumber } from './DataUtils';
3import { isWellBehavedNumber } from './isWellBehavedNumber';
4export function isWellFormedNumberDomain(v) {
5 if (Array.isArray(v) && v.length === 2) {
6 var [min, max] = v;
7 if (isWellBehavedNumber(min) && isWellBehavedNumber(max)) {
8 return true;
9 }
10 }
11 return false;
12}
13export function extendDomain(providedDomain, boundaryDomain, allowDataOverflow) {
14 if (allowDataOverflow) {
15 // If the data are allowed to overflow - we're fine with whatever user provided
16 return providedDomain;
17 }
18 /*
19 * If the data are not allowed to overflow - we need to extend the domain.
20 * Means that effectively the user is allowed to make the domain larger
21 * but not smaller.
22 */
23 return [Math.min(providedDomain[0], boundaryDomain[0]), Math.max(providedDomain[1], boundaryDomain[1])];
24}
25
26/**
27 * So Recharts allows users to provide their own domains,
28 * but it also places some expectations on what the domain is.
29 * We can improve on the typescript typing, but we also need a runtime test
30 to observe that the user-provided domain is well-formed,
31 * that is: an array with exactly two numbers.
32 *
33 * This function does not accept data as an argument.
34 * This is to enable a performance optimization - if the domain is there,
35 * and we know what it is without traversing all the data,
36 * then we don't have to traverse all the data!
37 *
38 * If the user-provided domain is not well-formed,
39 * this function will return undefined - in which case we should traverse the data to calculate the real domain.
40 *
41 * This function is for parsing the numerical domain only.
42 *
43 * @param userDomain external prop, user provided, before validation. Can have various shapes: array, function, special magical strings inside too.
44 * @param allowDataOverflow boolean, provided by users. If true then the data domain wins
45 *
46 * @return [min, max] domain if it's well-formed; undefined if the domain is invalid
47 */
48export function numericalDomainSpecifiedWithoutRequiringData(userDomain, allowDataOverflow) {
49 if (!allowDataOverflow) {
50 // Cannot compute data overflow if the data is not provided
51 return undefined;
52 }
53 if (typeof userDomain === 'function') {
54 // The user function expects the data to be provided as an argument
55 return undefined;
56 }
57 if (Array.isArray(userDomain) && userDomain.length === 2) {
58 var [providedMin, providedMax] = userDomain;
59 var finalMin, finalMax;
60 if (isWellBehavedNumber(providedMin)) {
61 finalMin = providedMin;
62 } else if (typeof providedMin === 'function') {
63 // The user function expects the data to be provided as an argument
64 return undefined;
65 }
66 if (isWellBehavedNumber(providedMax)) {
67 finalMax = providedMax;
68 } else if (typeof providedMax === 'function') {
69 // The user function expects the data to be provided as an argument
70 return undefined;
71 }
72 var candidate = [finalMin, finalMax];
73 if (isWellFormedNumberDomain(candidate)) {
74 return candidate;
75 }
76 }
77 return undefined;
78}
79
80/**
81 * So Recharts allows users to provide their own domains,
82 * but it also places some expectations on what the domain is.
83 * We can improve on the typescript typing, but we also need a runtime test
84 * to observe that the user-provided domain is well-formed,
85 * that is: an array with exactly two numbers.
86 * If the user-provided domain is not well-formed,
87 * this function will return undefined - in which case we should traverse the data to calculate the real domain.
88 *
89 * This function is for parsing the numerical domain only.
90 *
91 * You are probably thinking, why does domain need tick count?
92 * Well it adjusts the domain based on where the "nice ticks" land, and nice ticks depend on the tick count.
93 *
94 * @param userDomain external prop, user provided, before validation. Can have various shapes: array, function, special magical strings inside too.
95 * @param dataDomain calculated from data. Can be undefined, as an option for performance optimization
96 * @param allowDataOverflow provided by users. If true then the data domain wins
97 *
98 * @return [min, max] domain if it's well-formed; undefined if the domain is invalid
99 */
100export function parseNumericalUserDomain(userDomain, dataDomain, allowDataOverflow) {
101 if (!allowDataOverflow && dataDomain == null) {
102 // Cannot compute data overflow if the data is not provided
103 return undefined;
104 }
105 if (typeof userDomain === 'function' && dataDomain != null) {
106 try {
107 var result = userDomain(dataDomain, allowDataOverflow);
108 if (isWellFormedNumberDomain(result)) {
109 return extendDomain(result, dataDomain, allowDataOverflow);
110 }
111 } catch (_unused) {
112 /* ignore the exception and compute domain from data later */
113 }
114 }
115 if (Array.isArray(userDomain) && userDomain.length === 2) {
116 var [providedMin, providedMax] = userDomain;
117 var finalMin, finalMax;
118 if (providedMin === 'auto') {
119 if (dataDomain != null) {
120 finalMin = Math.min(...dataDomain);
121 }
122 } else if (isNumber(providedMin)) {
123 finalMin = providedMin;
124 } else if (typeof providedMin === 'function') {
125 try {
126 if (dataDomain != null) {
127 finalMin = providedMin(dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[0]);
128 }
129 } catch (_unused2) {
130 /* ignore the exception and compute domain from data later */
131 }
132 } else if (typeof providedMin === 'string' && MIN_VALUE_REG.test(providedMin)) {
133 var match = MIN_VALUE_REG.exec(providedMin);
134 if (match == null || match[1] == null || dataDomain == null) {
135 finalMin = undefined;
136 } else {
137 var value = +match[1];
138 finalMin = dataDomain[0] - value;
139 }
140 } else {
141 finalMin = dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[0];
142 }
143 if (providedMax === 'auto') {
144 if (dataDomain != null) {
145 finalMax = Math.max(...dataDomain);
146 }
147 } else if (isNumber(providedMax)) {
148 finalMax = providedMax;
149 } else if (typeof providedMax === 'function') {
150 try {
151 if (dataDomain != null) {
152 finalMax = providedMax(dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[1]);
153 }
154 } catch (_unused3) {
155 /* ignore the exception and compute domain from data later */
156 }
157 } else if (typeof providedMax === 'string' && MAX_VALUE_REG.test(providedMax)) {
158 var _match = MAX_VALUE_REG.exec(providedMax);
159 if (_match == null || _match[1] == null || dataDomain == null) {
160 finalMax = undefined;
161 } else {
162 var _value = +_match[1];
163 finalMax = dataDomain[1] + _value;
164 }
165 } else {
166 finalMax = dataDomain === null || dataDomain === void 0 ? void 0 : dataDomain[1];
167 }
168 var candidate = [finalMin, finalMax];
169 if (isWellFormedNumberDomain(candidate)) {
170 if (dataDomain == null) {
171 return candidate;
172 }
173 return extendDomain(candidate, dataDomain, allowDataOverflow);
174 }
175 }
176 return undefined;
177}
Note: See TracBrowser for help on using the repository browser.