UNPKG

recharts

Version:
187 lines (186 loc) • 12.5 kB
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); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } import { readEntryFill } from '../../../fill/fillDefinitions'; import { getBrushSliceIndexes } from '../../chartDataSlice'; import { findEntryInArray } from '../../../util/DataUtils'; import { getTooltipEntry, getValueByDataKey } from '../../../util/ChartUtils'; import { getSliced } from '../../../util/getSliced'; function parseName(value) { if (typeof value === 'string' || typeof value === 'number') { return value; } return undefined; } function parseUnit(value) { if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { return value; } return undefined; } function parseDataKey(value) { if (typeof value === 'string' || typeof value === 'number') { return value; } if (typeof value === 'function') { return obj => value(obj); } return undefined; } function parseColor(value) { if (typeof value === 'string') { return value; } return undefined; } function parseTooltipPayloadItem(item) { if (item == null || typeof item !== 'object') { return undefined; } var name = 'name' in item ? parseName(item.name) : undefined; var unit = 'unit' in item ? parseUnit(item.unit) : undefined; var dataKey = 'dataKey' in item ? parseDataKey(item.dataKey) : undefined; var payload = 'payload' in item ? item.payload : undefined; var color = 'color' in item ? parseColor(item.color) : undefined; // a fill object in the data counts by its color var fill = readEntryFill(item).color; var stroke = 'stroke' in item ? parseColor(item.stroke) : undefined; return { name, unit, dataKey, payload, color, fill, stroke }; } 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, tooltipAxisDataKey, activeLabel, tooltipPayloadSearcher, tooltipEventType) => { if (activeIndex == null || tooltipPayloadSearcher == null) { return undefined; } var chartData = chartDataState.chartData, computedData = chartDataState.computedData; var _getBrushSliceIndexes = getBrushSliceIndexes(chartDataState), _getBrushSliceIndexes2 = _slicedToArray(_getBrushSliceIndexes, 2), sliceStartIndex = _getBrushSliceIndexes2[0], sliceEndIndex = _getBrushSliceIndexes2[1]; // Brush indexes address the chart data. Data defined on an item is used as the item drew it. var slicedChartData = chartData == null ? undefined : getSliced(chartData, sliceStartIndex, sliceEndIndex); var init = []; return tooltipPayloadConfigurations.reduce((agg, configuration) => { var _settings$dataKey; var dataDefinedOnItem = configuration.dataDefinedOnItem, settings = configuration.settings, getEntryColor = configuration.getEntryColor; var sliced = selectFinalData(dataDefinedOnItem, slicedChartData); var finalDataKey = (_settings$dataKey = settings === null || settings === void 0 ? void 0 : settings.dataKey) !== null && _settings$dataKey !== void 0 ? _settings$dataKey : tooltipAxisDataKey; // BaseAxisProps does not support nameKey but it could! var finalNameKey = settings === null || settings === void 0 ? void 0 : settings.nameKey; // ?? tooltipAxis?.nameKey; var tooltipPayload; if (tooltipAxisDataKey && 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') { var _findEntryInArray; // A category can repeat, so the row at the active index wins when it has the active label. var entryAtIndex = tooltipPayloadSearcher(sliced, activeIndex, computedData, finalNameKey); tooltipPayload = (_findEntryInArray = findEntryInArray([entryAtIndex], tooltipAxisDataKey, activeLabel)) !== null && _findEntryInArray !== void 0 ? _findEntryInArray : findEntryInArray(sliced, tooltipAxisDataKey, 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 _parsedItem$color, _parsedItem$fill, _parsedItem$stroke; var parsedItem = parseTooltipPayloadItem(item); var itemName = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.name; var itemDataKey = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.dataKey; var itemPayload = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.payload; var newSettings = _objectSpread(_objectSpread({}, settings), {}, { name: itemName, unit: parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.unit, // Preserve item-level color/fill from graphical items. color: (_parsedItem$color = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.color) !== null && _parsedItem$color !== void 0 ? _parsedItem$color : settings.color, fill: (_parsedItem$fill = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.fill) !== null && _parsedItem$fill !== void 0 ? _parsedItem$fill : settings.fill, stroke: (_parsedItem$stroke = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.stroke) !== null && _parsedItem$stroke !== void 0 ? _parsedItem$stroke : settings.stroke }); agg.push(getTooltipEntry({ tooltipEntrySettings: newSettings, dataKey: itemDataKey, payload: itemPayload, value: getValueByDataKey(itemPayload, itemDataKey), name: itemName == null ? undefined : String(itemName) })); }); } else { var _ref, _parsedItem$color2, _parsedItem$fill2, _parsedItem$stroke2, _getValueByDataKey; /* * Graphical items that resolve the styles of each data entry on their own (Bar) report the color * they render the active entry with. The others take the colors of the active datum before their series colors. * Line, Area and Radar draw one shape per series, so they keep the series color. */ var parsedItem = settings.ignoreDataEntryColors ? undefined : parseTooltipPayloadItem(tooltipPayload); var entryColor = getEntryColor == null ? (_ref = (_parsedItem$color2 = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.color) !== null && _parsedItem$color2 !== void 0 ? _parsedItem$color2 : parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.fill) !== null && _ref !== void 0 ? _ref : parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.stroke : getEntryColor(tooltipPayload); var tooltipEntrySettings = _objectSpread(_objectSpread({}, settings), {}, { color: entryColor !== null && entryColor !== void 0 ? entryColor : settings.color, fill: (_parsedItem$fill2 = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.fill) !== null && _parsedItem$fill2 !== void 0 ? _parsedItem$fill2 : settings.fill, stroke: (_parsedItem$stroke2 = parsedItem === null || parsedItem === void 0 ? void 0 : parsedItem.stroke) !== null && _parsedItem$stroke2 !== void 0 ? _parsedItem$stroke2 : settings.stroke }); // I am not quite sure why these two branches (Array vs Array of Arrays) have to behave differently - I imagine we should unify these agg.push(getTooltipEntry({ tooltipEntrySettings, dataKey: finalDataKey, payload: tooltipPayload, // getValueByDataKey does not validate the output type value: getValueByDataKey(tooltipPayload, finalDataKey), // 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); };