| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.combineActiveTooltipIndex = void 0;
|
|---|
| 7 | var _isWellBehavedNumber = require("../../../util/isWellBehavedNumber");
|
|---|
| 8 | var _ChartUtils = require("../../../util/ChartUtils");
|
|---|
| 9 | var _isDomainSpecifiedByUser = require("../../../util/isDomainSpecifiedByUser");
|
|---|
| 10 | function toFiniteNumber(value) {
|
|---|
| 11 | if (typeof value === 'number') {
|
|---|
| 12 | return Number.isFinite(value) ? value : undefined;
|
|---|
| 13 | }
|
|---|
| 14 | if (value instanceof Date) {
|
|---|
| 15 | var numericValue = value.valueOf();
|
|---|
| 16 | return Number.isFinite(numericValue) ? numericValue : undefined;
|
|---|
| 17 | }
|
|---|
| 18 | var parsed = Number(value);
|
|---|
| 19 | return Number.isFinite(parsed) ? parsed : undefined;
|
|---|
| 20 | }
|
|---|
| 21 | function isValueWithinNumberDomain(value, domain) {
|
|---|
| 22 | var numericValue = toFiniteNumber(value);
|
|---|
| 23 | var lowerBound = domain[0];
|
|---|
| 24 | var upperBound = domain[1];
|
|---|
| 25 | if (numericValue === undefined) {
|
|---|
| 26 | return false;
|
|---|
| 27 | }
|
|---|
| 28 | var min = Math.min(lowerBound, upperBound);
|
|---|
| 29 | var max = Math.max(lowerBound, upperBound);
|
|---|
| 30 | return numericValue >= min && numericValue <= max;
|
|---|
| 31 | }
|
|---|
| 32 | function isValueWithinDomain(entry, axisDataKey, domain) {
|
|---|
| 33 | if (domain == null || axisDataKey == null) {
|
|---|
| 34 | return true;
|
|---|
| 35 | }
|
|---|
| 36 | var value = (0, _ChartUtils.getValueByDataKey)(entry, axisDataKey);
|
|---|
| 37 | if (value == null) {
|
|---|
| 38 | return true;
|
|---|
| 39 | }
|
|---|
| 40 | if (!(0, _isDomainSpecifiedByUser.isWellFormedNumberDomain)(domain)) {
|
|---|
| 41 | return true;
|
|---|
| 42 | }
|
|---|
| 43 | return isValueWithinNumberDomain(value, domain);
|
|---|
| 44 | }
|
|---|
| 45 | var combineActiveTooltipIndex = (tooltipInteraction, chartData, axisDataKey, domain) => {
|
|---|
| 46 | var desiredIndex = tooltipInteraction === null || tooltipInteraction === void 0 ? void 0 : tooltipInteraction.index;
|
|---|
| 47 | if (desiredIndex == null) {
|
|---|
| 48 | return null;
|
|---|
| 49 | }
|
|---|
| 50 | var indexAsNumber = Number(desiredIndex);
|
|---|
| 51 | if (!(0, _isWellBehavedNumber.isWellBehavedNumber)(indexAsNumber)) {
|
|---|
| 52 | // 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.
|
|---|
| 53 | return desiredIndex;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Zero is a trivial limit for single-dimensional charts like Line and Area,
|
|---|
| 58 | * but this also needs a support for multidimensional charts like Sankey and Treemap! TODO
|
|---|
| 59 | */
|
|---|
| 60 | var lowerLimit = 0;
|
|---|
| 61 | var upperLimit = +Infinity;
|
|---|
| 62 | if (chartData.length > 0) {
|
|---|
| 63 | upperLimit = chartData.length - 1;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // now let's clamp the desiredIndex between the limits
|
|---|
| 67 | var clampedIndex = Math.max(lowerLimit, Math.min(indexAsNumber, upperLimit));
|
|---|
| 68 | var entry = chartData[clampedIndex];
|
|---|
| 69 | if (entry == null) {
|
|---|
| 70 | return String(clampedIndex);
|
|---|
| 71 | }
|
|---|
| 72 | if (!isValueWithinDomain(entry, axisDataKey, domain)) {
|
|---|
| 73 | return null;
|
|---|
| 74 | }
|
|---|
| 75 | return String(clampedIndex);
|
|---|
| 76 | };
|
|---|
| 77 | exports.combineActiveTooltipIndex = combineActiveTooltipIndex; |
|---|