UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

135 lines (134 loc) 5.11 kB
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage"; import _extends from "@babel/runtime/helpers/esm/extends"; import { stack as d3Stack } from '@mui/x-charts-vendor/d3-shape'; import { warnOnce } from '@mui/x-internals/warning'; import { getStackingGroups } from "../../internals/stacking/index.mjs"; const defaultShapes = ['circle', 'square', 'diamond', 'cross', 'star', 'triangle', 'wye']; const lineValueFormatter = v => v == null ? '' : v.toLocaleString(); const seriesProcessor = (params, dataset, isItemVisible) => { const { seriesOrder, series } = params; const stackingGroups = getStackingGroups(_extends({}, params, { defaultStrategy: { stackOffset: 'none' } })); const idToIndex = new Map(); // Create a data set with format adapted to d3 const d3Dataset = dataset ?? []; seriesOrder.forEach((id, seriesIndex) => { idToIndex.set(id, seriesIndex); const data = series[id].data; if (data !== undefined) { data.forEach((value, dataIndex) => { if (d3Dataset.length <= dataIndex) { d3Dataset.push({ [id]: value }); } else { d3Dataset[dataIndex][id] = value; } }); } else if (series[id].valueGetter && dataset) { // When valueGetter is used without dataKey, populate d3Dataset with the series id as key dataset.forEach((entry, dataIndex) => { const value = series[id].valueGetter(entry); if (d3Dataset.length <= dataIndex) { d3Dataset.push({ [id]: value }); } else { d3Dataset[dataIndex][id] = value; } }); } else if (dataset === undefined && process.env.NODE_ENV !== 'production') { throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Charts: Line series with id="${id}" has no data. ` + 'The chart cannot render this series without data. ' + 'Provide a data property to the series or use the dataset prop.' : _formatErrorMessage(27, id)); } if (process.env.NODE_ENV !== 'production') { if (!data && dataset) { const dataKey = series[id].dataKey; if (!dataKey && !series[id].valueGetter) { throw new Error(`MUI X Charts: Line series with id="${id}" has no data, no dataKey, and no valueGetter. ` + 'When using the dataset prop, each series must have a dataKey or valueGetter to identify which dataset values to use. ' + 'Add a dataKey or valueGetter property to the series configuration.'); } if (dataKey) { dataset.forEach((entry, index) => { const value = entry[dataKey]; if (value != null && typeof value !== 'number') { warnOnce(`MUI X Charts: your dataset key "${dataKey}" is used for plotting lines, but the dataset contains the non-null non-numerical element "${value}" at index ${index}. Line plots only support numeric and null values.`); } }); } } } }); const completedSeries = {}; stackingGroups.forEach(stackingGroup => { const { ids, stackingOffset, stackingOrder } = stackingGroup; const keys = ids.map(id => { // Use dataKey if needed and available const dataKey = series[id].dataKey; return series[id].data === undefined && dataKey !== undefined ? dataKey : id; }); const stackedData = d3Stack().keys(keys).value((d, key) => d[key] ?? 0) // defaultize null value to 0 .order(stackingOrder).offset(stackingOffset)(d3Dataset); const idOrder = stackedData.map(s => s.index); const fixedOrder = () => idOrder; // Compute visible stacked data const visibleStackedData = d3Stack().keys(keys).value((d, key) => { const keyIndex = keys.indexOf(key); const seriesId = ids[keyIndex]; if (!isItemVisible?.({ type: 'line', seriesId })) { // For hidden series, return 0 so they don't contribute to the stack return 0; } return d[key] ?? 0; }).order(fixedOrder).offset(stackingOffset)(d3Dataset); ids.forEach((id, index) => { const { dataKey, valueGetter } = series[id]; let data; if (valueGetter) { data = dataset.map(d => valueGetter(d)); } else if (dataKey) { data = dataset.map(d => { const value = d[dataKey]; return typeof value === 'number' ? value : null; }); } else { data = series[id].data; } const hidden = !isItemVisible?.({ type: 'line', seriesId: id }); completedSeries[id] = _extends({ labelMarkType: 'line+mark' }, series[id], { shape: series[id].shape ?? defaultShapes[(idToIndex.get(id) ?? 0) % defaultShapes.length], data, valueFormatter: series[id].valueFormatter ?? lineValueFormatter, hidden, stackedData: stackedData[index], visibleStackedData: visibleStackedData[index] }); }); }); return { seriesOrder, stackingGroups, series: completedSeries }; }; export default seriesProcessor;