source: node_modules/recharts/es6/state/selectors/combiners/combineActiveTooltipIndex.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: 2.4 KB
Line 
1import { isWellBehavedNumber } from '../../../util/isWellBehavedNumber';
2import { getValueByDataKey } from '../../../util/ChartUtils';
3import { isWellFormedNumberDomain } from '../../../util/isDomainSpecifiedByUser';
4function toFiniteNumber(value) {
5 if (typeof value === 'number') {
6 return Number.isFinite(value) ? value : undefined;
7 }
8 if (value instanceof Date) {
9 var numericValue = value.valueOf();
10 return Number.isFinite(numericValue) ? numericValue : undefined;
11 }
12 var parsed = Number(value);
13 return Number.isFinite(parsed) ? parsed : undefined;
14}
15function isValueWithinNumberDomain(value, domain) {
16 var numericValue = toFiniteNumber(value);
17 var lowerBound = domain[0];
18 var upperBound = domain[1];
19 if (numericValue === undefined) {
20 return false;
21 }
22 var min = Math.min(lowerBound, upperBound);
23 var max = Math.max(lowerBound, upperBound);
24 return numericValue >= min && numericValue <= max;
25}
26function isValueWithinDomain(entry, axisDataKey, domain) {
27 if (domain == null || axisDataKey == null) {
28 return true;
29 }
30 var value = getValueByDataKey(entry, axisDataKey);
31 if (value == null) {
32 return true;
33 }
34 if (!isWellFormedNumberDomain(domain)) {
35 return true;
36 }
37 return isValueWithinNumberDomain(value, domain);
38}
39export var combineActiveTooltipIndex = (tooltipInteraction, chartData, axisDataKey, domain) => {
40 var desiredIndex = tooltipInteraction === null || tooltipInteraction === void 0 ? void 0 : tooltipInteraction.index;
41 if (desiredIndex == null) {
42 return null;
43 }
44 var indexAsNumber = Number(desiredIndex);
45 if (!isWellBehavedNumber(indexAsNumber)) {
46 // this is for charts like Sankey and Treemap that do not support numerical indexes. We need a proper solution for this before we can start supporting keyboard events on these charts.
47 return desiredIndex;
48 }
49
50 /*
51 * Zero is a trivial limit for single-dimensional charts like Line and Area,
52 * but this also needs a support for multidimensional charts like Sankey and Treemap! TODO
53 */
54 var lowerLimit = 0;
55 var upperLimit = +Infinity;
56 if (chartData.length > 0) {
57 upperLimit = chartData.length - 1;
58 }
59
60 // now let's clamp the desiredIndex between the limits
61 var clampedIndex = Math.max(lowerLimit, Math.min(indexAsNumber, upperLimit));
62 var entry = chartData[clampedIndex];
63 if (entry == null) {
64 return String(clampedIndex);
65 }
66 if (!isValueWithinDomain(entry, axisDataKey, domain)) {
67 return null;
68 }
69 return String(clampedIndex);
70};
Note: See TracBrowser for help on using the repository browser.