@mui/x-charts
Version:
The community edition of MUI X Charts components.
122 lines • 4.49 kB
JavaScript
import { getPreviousNonEmptySeries } from "./plugins/featurePlugins/useChartKeyboardNavigation/utils/getPreviousNonEmptySeries.js";
import { getMaxSeriesLength } from "./plugins/featurePlugins/useChartKeyboardNavigation/utils/getMaxSeriesLength.js";
import { selectorChartSeriesProcessed } from "./plugins/corePlugins/useChartSeries/index.js";
import { getNextNonEmptySeries } from "./plugins/featurePlugins/useChartKeyboardNavigation/utils/getNextNonEmptySeries.js";
import { seriesHasData } from "./seriesHasData.js";
export function createGetNextIndexFocusedItem(
/**
* The set of series types compatible with this navigation action.
*/
compatibleSeriesTypes,
/**
* If true, allows cycling from the last item to the first one.
*/
allowCycles = false) {
return function getNextIndexFocusedItem(currentItem, state) {
const processedSeries = selectorChartSeriesProcessed(state);
let seriesId = currentItem?.seriesId;
let type = currentItem?.type;
if (!type || seriesId == null || !seriesHasData(processedSeries, type, seriesId)) {
const nextSeries = getNextNonEmptySeries(processedSeries, compatibleSeriesTypes, type, seriesId);
if (nextSeries === null) {
return null;
}
type = nextSeries.type;
seriesId = nextSeries.seriesId;
}
const maxLength = getMaxSeriesLength(processedSeries, compatibleSeriesTypes);
let dataIndex = currentItem?.dataIndex == null ? 0 : currentItem.dataIndex + 1;
if (allowCycles) {
dataIndex = dataIndex % maxLength;
} else {
dataIndex = Math.min(maxLength - 1, dataIndex);
}
return {
type,
seriesId,
dataIndex
};
};
}
export function createGetPreviousIndexFocusedItem(
/**
* The set of series types compatible with this navigation action.
*/
compatibleSeriesTypes,
/**
* If true, allows cycling from the last item to the first one.
*/
allowCycles = false) {
return function getPreviousIndexFocusedItem(currentItem, state) {
const processedSeries = selectorChartSeriesProcessed(state);
let seriesId = currentItem?.seriesId;
let type = currentItem?.type;
if (!type || seriesId == null || !seriesHasData(processedSeries, type, seriesId)) {
const previousSeries = getPreviousNonEmptySeries(processedSeries, compatibleSeriesTypes, type, seriesId);
if (previousSeries === null) {
return null;
}
type = previousSeries.type;
seriesId = previousSeries.seriesId;
}
const maxLength = getMaxSeriesLength(processedSeries, compatibleSeriesTypes);
let dataIndex = currentItem?.dataIndex == null ? maxLength - 1 : currentItem.dataIndex - 1;
if (allowCycles) {
dataIndex = (maxLength + dataIndex) % maxLength;
} else {
dataIndex = Math.max(0, dataIndex);
}
return {
type,
seriesId,
dataIndex
};
};
}
export function createGetNextSeriesFocusedItem(
/**
* The set of series types compatible with this navigation action.
*/
compatibleSeriesTypes) {
return function getNextSeriesFocusedItem(currentItem, state) {
const processedSeries = selectorChartSeriesProcessed(state);
let seriesId = currentItem?.seriesId;
let type = currentItem?.type;
const nextSeries = getNextNonEmptySeries(processedSeries, compatibleSeriesTypes, type, seriesId);
if (nextSeries === null) {
return null; // No series to move the focus to.
}
type = nextSeries.type;
seriesId = nextSeries.seriesId;
const dataIndex = currentItem?.dataIndex == null ? 0 : currentItem.dataIndex;
return {
type,
seriesId,
dataIndex
};
};
}
export function createGetPreviousSeriesFocusedItem(
/**
* The set of series types compatible with this navigation action.
*/
compatibleSeriesTypes) {
return function getPreviousSeriesFocusedItem(currentItem, state) {
const processedSeries = selectorChartSeriesProcessed(state);
let seriesId = currentItem?.seriesId;
let type = currentItem?.type;
const previousSeries = getPreviousNonEmptySeries(processedSeries, compatibleSeriesTypes, type, seriesId);
if (previousSeries === null) {
return null; // No series to move the focus to.
}
type = previousSeries.type;
seriesId = previousSeries.seriesId;
const data = processedSeries[type].series[seriesId].data;
const dataIndex = currentItem?.dataIndex == null ? data.length - 1 : currentItem.dataIndex;
return {
type,
seriesId,
dataIndex
};
};
}