recharts
Version:
React charts
118 lines • 6.91 kB
JavaScript
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; }
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; }
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; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
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); }
import { findEntryInArray } from '../../../util/DataUtils';
import { getTooltipEntry, getValueByDataKey } from '../../../util/ChartUtils';
function getSliced(arr, startIndex, endIndex) {
if (!Array.isArray(arr)) {
return arr;
}
if (arr && startIndex + endIndex !== 0) {
return arr.slice(startIndex, endIndex + 1);
}
return arr;
}
function selectFinalData(dataDefinedOnItem, dataDefinedOnChart) {
/*
* If a payload has data specified directly from the graphical item, prefer that.
* Otherwise, fill in data from the chart level, using the same index.
*/
if (dataDefinedOnItem != null) {
return dataDefinedOnItem;
}
return dataDefinedOnChart;
}
export var combineTooltipPayload = (tooltipPayloadConfigurations, activeIndex, chartDataState, tooltipAxis, activeLabel, tooltipPayloadSearcher, tooltipEventType) => {
if (activeIndex == null || tooltipPayloadSearcher == null) {
return undefined;
}
var {
chartData,
computedData,
dataStartIndex,
dataEndIndex
} = chartDataState;
var init = [];
return tooltipPayloadConfigurations.reduce((agg, _ref) => {
var _settings$dataKey;
var {
dataDefinedOnItem,
settings
} = _ref;
var finalData = selectFinalData(dataDefinedOnItem, chartData);
var sliced = getSliced(finalData, dataStartIndex, dataEndIndex);
var finalDataKey = (_settings$dataKey = settings === null || settings === void 0 ? void 0 : settings.dataKey) !== null && _settings$dataKey !== void 0 ? _settings$dataKey : tooltipAxis === null || tooltipAxis === void 0 ? void 0 : tooltipAxis.dataKey;
// BaseAxisProps does not support nameKey but it could!
var finalNameKey = settings === null || settings === void 0 ? void 0 : settings.nameKey; // ?? tooltipAxis?.nameKey;
var tooltipPayload;
if (tooltipAxis !== null && tooltipAxis !== void 0 && tooltipAxis.dataKey && Array.isArray(sliced) &&
/*
* findEntryInArray won't work for Scatter because Scatter provides an array of arrays
* as tooltip payloads and findEntryInArray is not prepared to handle that.
* Sad but also ScatterChart only allows 'item' tooltipEventType
* and also this is only a problem if there are multiple Scatters and each has its own data array
* so let's fix that some other time.
*/
!Array.isArray(sliced[0]) &&
/*
* If the tooltipEventType is 'axis', we should search for the dataKey in the sliced data
* because thanks to allowDuplicatedCategory=false, the order of elements in the array
* no longer matches the order of elements in the original data
* and so we need to search by the active dataKey + label rather than by index.
*
* The same happens if multiple graphical items are present in the chart
* and each of them has its own data array. Those arrays get concatenated
* and again the tooltip index no longer matches the original data.
*
* On the other hand the tooltipEventType 'item' should always search by index
* because we get the index from interacting over the individual elements
* which is always accurate, irrespective of the allowDuplicatedCategory setting.
*/
tooltipEventType === 'axis') {
tooltipPayload = findEntryInArray(sliced, tooltipAxis.dataKey, activeLabel);
} else {
/*
* This is a problem because it assumes that the index is pointing to the displayed data
* which it isn't because the index is pointing to the tooltip ticks array.
* The above approach (with findEntryInArray) is the correct one, but it only works
* if the axis dataKey is defined explicitly, and if the data is an array of objects.
*/
tooltipPayload = tooltipPayloadSearcher(sliced, activeIndex, computedData, finalNameKey);
}
if (Array.isArray(tooltipPayload)) {
tooltipPayload.forEach(item => {
var newSettings = _objectSpread(_objectSpread({}, settings), {}, {
name: item.name,
unit: item.unit,
// 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.
color: undefined,
// 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.
fill: undefined
});
agg.push(getTooltipEntry({
tooltipEntrySettings: newSettings,
dataKey: item.dataKey,
payload: item.payload,
// @ts-expect-error getValueByDataKey does not validate the output type
value: getValueByDataKey(item.payload, item.dataKey),
name: item.name
}));
});
} else {
var _getValueByDataKey;
// 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?
agg.push(getTooltipEntry({
tooltipEntrySettings: settings,
dataKey: finalDataKey,
payload: tooltipPayload,
// @ts-expect-error getValueByDataKey does not validate the output type
value: getValueByDataKey(tooltipPayload, finalDataKey),
// @ts-expect-error getValueByDataKey does not validate the output type
name: (_getValueByDataKey = getValueByDataKey(tooltipPayload, finalNameKey)) !== null && _getValueByDataKey !== void 0 ? _getValueByDataKey : settings === null || settings === void 0 ? void 0 : settings.name
}));
}
return agg;
}, init);
};