| 1 | function 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; }
|
|---|
| 2 | function _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; }
|
|---|
| 3 | function _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; }
|
|---|
| 4 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 5 | function _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); }
|
|---|
| 6 | import { findEntryInArray } from '../../../util/DataUtils';
|
|---|
| 7 | import { getTooltipEntry, getValueByDataKey } from '../../../util/ChartUtils';
|
|---|
| 8 | import { getSliced } from '../../../util/getSliced';
|
|---|
| 9 | function selectFinalData(dataDefinedOnItem, dataDefinedOnChart) {
|
|---|
| 10 | /*
|
|---|
| 11 | * If a payload has data specified directly from the graphical item, prefer that.
|
|---|
| 12 | * Otherwise, fill in data from the chart level, using the same index.
|
|---|
| 13 | */
|
|---|
| 14 | if (dataDefinedOnItem != null) {
|
|---|
| 15 | return dataDefinedOnItem;
|
|---|
| 16 | }
|
|---|
| 17 | return dataDefinedOnChart;
|
|---|
| 18 | }
|
|---|
| 19 | export var combineTooltipPayload = (tooltipPayloadConfigurations, activeIndex, chartDataState, tooltipAxisDataKey, activeLabel, tooltipPayloadSearcher, tooltipEventType) => {
|
|---|
| 20 | if (activeIndex == null || tooltipPayloadSearcher == null) {
|
|---|
| 21 | return undefined;
|
|---|
| 22 | }
|
|---|
| 23 | var {
|
|---|
| 24 | chartData,
|
|---|
| 25 | computedData,
|
|---|
| 26 | dataStartIndex,
|
|---|
| 27 | dataEndIndex
|
|---|
| 28 | } = chartDataState;
|
|---|
| 29 | var init = [];
|
|---|
| 30 | return tooltipPayloadConfigurations.reduce((agg, _ref) => {
|
|---|
| 31 | var _settings$dataKey;
|
|---|
| 32 | var {
|
|---|
| 33 | dataDefinedOnItem,
|
|---|
| 34 | settings
|
|---|
| 35 | } = _ref;
|
|---|
| 36 | var finalData = selectFinalData(dataDefinedOnItem, chartData);
|
|---|
| 37 | var sliced = Array.isArray(finalData) ? getSliced(finalData, dataStartIndex, dataEndIndex) : finalData;
|
|---|
| 38 | var finalDataKey = (_settings$dataKey = settings === null || settings === void 0 ? void 0 : settings.dataKey) !== null && _settings$dataKey !== void 0 ? _settings$dataKey : tooltipAxisDataKey;
|
|---|
| 39 | // BaseAxisProps does not support nameKey but it could!
|
|---|
| 40 | var finalNameKey = settings === null || settings === void 0 ? void 0 : settings.nameKey; // ?? tooltipAxis?.nameKey;
|
|---|
| 41 | var tooltipPayload;
|
|---|
| 42 | if (tooltipAxisDataKey && Array.isArray(sliced) &&
|
|---|
| 43 | /*
|
|---|
| 44 | * findEntryInArray won't work for Scatter because Scatter provides an array of arrays
|
|---|
| 45 | * as tooltip payloads and findEntryInArray is not prepared to handle that.
|
|---|
| 46 | * Sad but also ScatterChart only allows 'item' tooltipEventType
|
|---|
| 47 | * and also this is only a problem if there are multiple Scatters and each has its own data array
|
|---|
| 48 | * so let's fix that some other time.
|
|---|
| 49 | */
|
|---|
| 50 | !Array.isArray(sliced[0]) &&
|
|---|
| 51 | /*
|
|---|
| 52 | * If the tooltipEventType is 'axis', we should search for the dataKey in the sliced data
|
|---|
| 53 | * because thanks to allowDuplicatedCategory=false, the order of elements in the array
|
|---|
| 54 | * no longer matches the order of elements in the original data
|
|---|
| 55 | * and so we need to search by the active dataKey + label rather than by index.
|
|---|
| 56 | *
|
|---|
| 57 | * The same happens if multiple graphical items are present in the chart
|
|---|
| 58 | * and each of them has its own data array. Those arrays get concatenated
|
|---|
| 59 | * and again the tooltip index no longer matches the original data.
|
|---|
| 60 | *
|
|---|
| 61 | * On the other hand the tooltipEventType 'item' should always search by index
|
|---|
| 62 | * because we get the index from interacting over the individual elements
|
|---|
| 63 | * which is always accurate, irrespective of the allowDuplicatedCategory setting.
|
|---|
| 64 | */
|
|---|
| 65 | tooltipEventType === 'axis') {
|
|---|
| 66 | tooltipPayload = findEntryInArray(sliced, tooltipAxisDataKey, activeLabel);
|
|---|
| 67 | } else {
|
|---|
| 68 | /*
|
|---|
| 69 | * This is a problem because it assumes that the index is pointing to the displayed data
|
|---|
| 70 | * which it isn't because the index is pointing to the tooltip ticks array.
|
|---|
| 71 | * The above approach (with findEntryInArray) is the correct one, but it only works
|
|---|
| 72 | * if the axis dataKey is defined explicitly, and if the data is an array of objects.
|
|---|
| 73 | */
|
|---|
| 74 | tooltipPayload = tooltipPayloadSearcher(sliced, activeIndex, computedData, finalNameKey);
|
|---|
| 75 | }
|
|---|
| 76 | if (Array.isArray(tooltipPayload)) {
|
|---|
| 77 | tooltipPayload.forEach(item => {
|
|---|
| 78 | var newSettings = _objectSpread(_objectSpread({}, settings), {}, {
|
|---|
| 79 | // @ts-expect-error we're assuming that item has name and unit properties
|
|---|
| 80 | name: item.name,
|
|---|
| 81 | // @ts-expect-error we're assuming that item has name and unit properties
|
|---|
| 82 | unit: item.unit,
|
|---|
| 83 | // color and fill are erased to keep 100% the identical behaviour to recharts 2.x - but there's nothing stopping us from returning them here. It's technically a breaking change.
|
|---|
| 84 | color: undefined,
|
|---|
| 85 | // color and fill are erased to keep 100% the identical behaviour to recharts 2.x - but there's nothing stopping us from returning them here. It's technically a breaking change.
|
|---|
| 86 | fill: undefined
|
|---|
| 87 | });
|
|---|
| 88 | agg.push(getTooltipEntry({
|
|---|
| 89 | tooltipEntrySettings: newSettings,
|
|---|
| 90 | // @ts-expect-error we're assuming that item has name and unit properties
|
|---|
| 91 | dataKey: item.dataKey,
|
|---|
| 92 | // @ts-expect-error we're assuming that item has name and unit properties
|
|---|
| 93 | payload: item.payload,
|
|---|
| 94 | // @ts-expect-error getValueByDataKey does not validate the output type
|
|---|
| 95 | value: getValueByDataKey(item.payload, item.dataKey),
|
|---|
| 96 | // @ts-expect-error we're assuming that item has name and unit properties
|
|---|
| 97 | name: item.name
|
|---|
| 98 | }));
|
|---|
| 99 | });
|
|---|
| 100 | } else {
|
|---|
| 101 | var _getValueByDataKey;
|
|---|
| 102 | // I am not quite sure why these two branches (Array vs Array of Arrays) have to behave differently - I imagine we should unify these. 3.x breaking change?
|
|---|
| 103 | agg.push(getTooltipEntry({
|
|---|
| 104 | tooltipEntrySettings: settings,
|
|---|
| 105 | dataKey: finalDataKey,
|
|---|
| 106 | payload: tooltipPayload,
|
|---|
| 107 | // @ts-expect-error getValueByDataKey does not validate the output type
|
|---|
| 108 | value: getValueByDataKey(tooltipPayload, finalDataKey),
|
|---|
| 109 | // @ts-expect-error getValueByDataKey does not validate the output type
|
|---|
| 110 | name: (_getValueByDataKey = getValueByDataKey(tooltipPayload, finalNameKey)) !== null && _getValueByDataKey !== void 0 ? _getValueByDataKey : settings === null || settings === void 0 ? void 0 : settings.name
|
|---|
| 111 | }));
|
|---|
| 112 | }
|
|---|
| 113 | return agg;
|
|---|
| 114 | }, init);
|
|---|
| 115 | }; |
|---|