UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

113 lines (110 loc) 4.13 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _d3Shape = require("@mui/x-charts-vendor/d3-shape"); var _warning = require("@mui/x-internals/warning"); var _stacking = require("../../internals/stacking"); const lineValueFormatter = v => v == null ? '' : v.toLocaleString(); const seriesProcessor = (params, dataset, isItemVisible) => { const { seriesOrder, series } = params; const stackingGroups = (0, _stacking.getStackingGroups)((0, _extends2.default)({}, params, { defaultStrategy: { stackOffset: 'none' } })); // Create a data set with format adapted to d3 const d3Dataset = dataset ?? []; seriesOrder.forEach(id => { const data = series[id].data; if (data !== undefined) { data.forEach((value, index) => { if (d3Dataset.length <= index) { d3Dataset.push({ [id]: value }); } else { d3Dataset[index][id] = value; } }); } else if (dataset === undefined && process.env.NODE_ENV !== 'production') { throw new Error([`MUI X Charts: line series with id='${id}' has no data.`, 'Either provide a data property to the series or use the dataset prop.'].join('\n')); } if (process.env.NODE_ENV !== 'production') { if (!data && dataset) { const dataKey = series[id].dataKey; if (!dataKey) { throw new Error([`MUI X Charts: line series with id='${id}' has no data and no dataKey.`, 'You must provide a dataKey when using the dataset prop.'].join('\n')); } dataset.forEach((entry, index) => { const value = entry[dataKey]; if (value != null && typeof value !== 'number') { (0, _warning.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.'].join('\n')); } }); } } }); 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 = (0, _d3Shape.stack)().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 = (0, _d3Shape.stack)().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 = series[id].dataKey; const data = dataKey ? dataset.map(d => { const value = d[dataKey]; return typeof value === 'number' ? value : null; }) : series[id].data; const hidden = !isItemVisible?.({ type: 'line', seriesId: id }); completedSeries[id] = (0, _extends2.default)({ labelMarkType: 'line' }, series[id], { data, valueFormatter: series[id].valueFormatter ?? lineValueFormatter, hidden, stackedData: stackedData[index], visibleStackedData: visibleStackedData[index] }); }); }); return { seriesOrder, stackingGroups, series: completedSeries }; }; var _default = exports.default = seriesProcessor;