@mui/x-charts
Version:
The community edition of MUI X Charts components.
51 lines • 1.59 kB
JavaScript
/**
* Walk forward (or backward) from `startIndex` and return the first dataIndex
* whose data point is visible. Returns `null` if every traversed index is hidden.
*
* Series-level hidden flags are filtered upstream (see `getNonEmptySeriesArray`),
* so only per-data-point hidden flags are relevant here. Today only Pie sets a
* `hidden` flag on individual data items; for other series types the data
* elements are scalars or objects without that field, and this helper returns
* the proposed index unchanged.
*/
export function findVisibleDataIndex({
processedSeries,
type,
seriesId,
startIndex,
dataLength,
direction,
allowCycles
}) {
if (dataLength <= 0) {
return null;
}
const seriesItem = processedSeries[type]?.series[seriesId];
if (seriesItem && 'hidden' in seriesItem && seriesItem.hidden) {
return null;
}
const seriesData = seriesItem?.data;
const isIndexHidden = idx => {
if (!seriesData) {
return false;
}
const item = seriesData[idx];
return typeof item === 'object' && item !== null && 'hidden' in item && Boolean(item.hidden);
};
let dataIndex = startIndex;
for (let attempt = 0; attempt < dataLength; attempt += 1) {
if (dataIndex >= 0 && dataIndex < dataLength && !isIndexHidden(dataIndex)) {
return dataIndex;
}
if (allowCycles) {
dataIndex = (dataIndex + direction + dataLength) % dataLength;
} else {
const next = dataIndex + direction;
if (next < 0 || next >= dataLength) {
return null;
}
dataIndex = next;
}
}
return null;
}