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