recharts
Version:
React charts
97 lines (92 loc) • 4.8 kB
JavaScript
/**
* @fileOverview
* Every graphical item has one effective dataKey.
*
* The dataKey defined directly on the graphical item always wins.
* When the item does not define its own, it inherits the dataKey of its value axis.
* The value axis is the axis where the item shows its value.
* Which one that is depends on the chart layout alone, and not on the axis `type`.
*
* This file is the single place where that rule is defined.
*/
import { createSelector } from 'reselect';
import { isNotNil } from '../../util/DataUtils';
import { selectChartLayout } from '../../context/chartLayoutContext';
import { selectAllUnfilteredGraphicalItems } from './graphicalItemSelectors';
/**
* The parts of a graphical item that decide its effective dataKey.
* Cartesian items have xAxisId and yAxisId, polar items have angleAxisId and radiusAxisId.
*/
/**
* Value axis is the axis where graphical items display their value,
* as opposed to the categorical axis where they display the category (or name, or index).
*
* The chart layout alone decides which axis that is.
* The axis `type` prop has no say in this: an axis in the value position is the value axis
* even if it has `type="category"`, and the other axis is not, even if it has `type="number"`.
*
* @param layout chart layout
* @param axisType kind of the axis
* @returns true if graphical items in this layout show their value on this axis
*/
export function isValueAxis(layout, axisType) {
return layout === 'horizontal' && axisType === 'yAxis' || layout === 'vertical' && axisType === 'xAxis' || layout === 'centric' && axisType === 'radiusAxis' || layout === 'radial' && axisType === 'angleAxis';
}
/**
* Decides which dataKey reads the value of a graphical item on the given axis.
*
* On the value axis the graphical item wins, and the axis is the fallback.
* On all other axes the axis wins, and the graphical item is the fallback.
*
* @param itemDataKey dataKey of the graphical item
* @param axisDataKey dataKey of the axis
* @param layout chart layout
* @param axisType kind of the axis
* @returns dataKey to use when reading the item value on this axis
*/
export function getDataKeyOnAxis(itemDataKey, axisDataKey, layout, axisType) {
if (isValueAxis(layout, axisType)) {
return itemDataKey !== null && itemDataKey !== void 0 ? itemDataKey : axisDataKey;
}
return axisDataKey !== null && axisDataKey !== void 0 ? axisDataKey : itemDataKey;
}
function getValueAxisDataKey(item, layout, _ref) {
var _cartesianAxis$yAxis$, _cartesianAxis$xAxis$, _polarAxis$radiusAxis, _polarAxis$angleAxis$;
var cartesianAxis = _ref.cartesianAxis,
polarAxis = _ref.polarAxis;
switch (layout) {
case 'horizontal':
return item.yAxisId == null ? undefined : (_cartesianAxis$yAxis$ = cartesianAxis.yAxis[item.yAxisId]) === null || _cartesianAxis$yAxis$ === void 0 ? void 0 : _cartesianAxis$yAxis$.dataKey;
case 'vertical':
return item.xAxisId == null ? undefined : (_cartesianAxis$xAxis$ = cartesianAxis.xAxis[item.xAxisId]) === null || _cartesianAxis$xAxis$ === void 0 ? void 0 : _cartesianAxis$xAxis$.dataKey;
case 'centric':
return item.radiusAxisId == null ? undefined : (_polarAxis$radiusAxis = polarAxis.radiusAxis[item.radiusAxisId]) === null || _polarAxis$radiusAxis === void 0 ? void 0 : _polarAxis$radiusAxis.dataKey;
case 'radial':
return item.angleAxisId == null ? undefined : (_polarAxis$angleAxis$ = polarAxis.angleAxis[item.angleAxisId]) === null || _polarAxis$angleAxis$ === void 0 ? void 0 : _polarAxis$angleAxis$.dataKey;
default:
return undefined;
}
}
/**
* Returns the effective dataKey of a graphical item:
* its own dataKey if it has one, otherwise the dataKey of its value axis.
*
* @param state Recharts root state
* @param item graphical item props or settings
* @returns effective dataKey, or undefined if neither the item nor its value axis define one
*/
export function selectEffectiveDataKey(state, item) {
var _item$dataKey;
return (_item$dataKey = item.dataKey) !== null && _item$dataKey !== void 0 ? _item$dataKey : getValueAxisDataKey(item, selectChartLayout(state), state);
}
/**
* Effective dataKeys of all graphical items in the chart, including the hidden ones.
* Items that do not have any effective dataKey are skipped.
*/
export var selectAllEffectiveGraphicalItemDataKeys = createSelector([selectAllUnfilteredGraphicalItems, selectChartLayout, state => state.cartesianAxis, state => state.polarAxis], (graphicalItems, layout, cartesianAxis, polarAxis) => graphicalItems.map(item => {
var _item$dataKey2;
return (_item$dataKey2 = item.dataKey) !== null && _item$dataKey2 !== void 0 ? _item$dataKey2 : getValueAxisDataKey(item, layout, {
cartesianAxis,
polarAxis
});
}).filter(isNotNil));