@mui/x-charts
Version:
The community edition of MUI X Charts components.
126 lines (124 loc) • 4.91 kB
JavaScript
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useAreaPlotData = useAreaPlotData;
var React = _interopRequireWildcard(require("react"));
var _d3Shape = require("@mui/x-charts-vendor/d3-shape");
var _useChartGradientId = require("../hooks/useChartGradientId");
var _isBandScale = require("../internals/isBandScale");
var _getCurve = require("../internals/getCurve");
var _hooks = require("../hooks");
var _constants = require("../constants");
function useAreaPlotData(xAxes, yAxes) {
const seriesData = (0, _hooks.useLineSeriesContext)();
const defaultXAxisId = (0, _hooks.useXAxes)().xAxisIds[0];
const defaultYAxisId = (0, _hooks.useYAxes)().yAxisIds[0];
const getGradientId = (0, _useChartGradientId.useChartGradientIdBuilder)();
// This memo prevents odd line chart behavior when hydrating.
const allData = React.useMemo(() => {
if (seriesData === undefined) {
return [];
}
const {
series,
stackingGroups
} = seriesData;
const areaPlotData = [];
for (const stackingGroup of stackingGroups) {
const groupIds = stackingGroup.ids;
for (let i = groupIds.length - 1; i >= 0; i -= 1) {
const seriesId = groupIds[i];
const {
xAxisId = defaultXAxisId,
yAxisId = defaultYAxisId,
stackedData,
data,
connectNulls,
baseline,
curve,
strictStepCurve,
area
} = series[seriesId];
if (!area || !(xAxisId in xAxes) || !(yAxisId in yAxes)) {
continue;
}
const xScale = xAxes[xAxisId].scale;
const xPosition = (0, _hooks.getValueToPositionMapper)(xScale);
const yScale = yAxes[yAxisId].scale;
const xData = xAxes[xAxisId].data;
const gradientId = yAxes[yAxisId].colorScale && getGradientId(yAxisId) || xAxes[xAxisId].colorScale && getGradientId(xAxisId) || undefined;
if (process.env.NODE_ENV !== 'production') {
if (xData === undefined) {
throw new Error(`MUI X Charts: ${xAxisId === _constants.DEFAULT_X_AXIS_KEY ? 'The first `xAxis`' : `The x-axis with id "${xAxisId}"`} should have data property to be able to display a line plot.`);
}
if (xData.length < stackedData.length) {
throw new Error(`MUI X Charts: The data length of the x axis (${xData.length} items) is lower than the length of series (${stackedData.length} items).`);
}
}
const shouldExpand = curve?.includes('step') && !strictStepCurve && (0, _isBandScale.isBandScale)(xScale);
const formattedData = xData?.flatMap((x, index) => {
const nullData = data[index] == null;
if (shouldExpand) {
const rep = [{
x,
y: stackedData[index],
nullData,
isExtension: false
}];
if (!nullData && (index === 0 || data[index - 1] == null)) {
rep.unshift({
x: (xScale(x) ?? 0) - (xScale.step() - xScale.bandwidth()) / 2,
y: stackedData[index],
nullData,
isExtension: true
});
}
if (!nullData && (index === data.length - 1 || data[index + 1] == null)) {
rep.push({
x: (xScale(x) ?? 0) + (xScale.step() + xScale.bandwidth()) / 2,
y: stackedData[index],
nullData,
isExtension: true
});
}
return rep;
}
return {
x,
y: stackedData[index],
nullData
};
}) ?? [];
const d3Data = connectNulls ? formattedData.filter(d => !d.nullData) : formattedData;
const areaPath = (0, _d3Shape.area)().x(d => d.isExtension ? d.x : xPosition(d.x)).defined(d => connectNulls || !d.nullData || !!d.isExtension).y0(d => {
if (typeof baseline === 'number') {
return yScale(baseline);
}
if (baseline === 'max') {
return yScale.range()[1];
}
if (baseline === 'min') {
return yScale.range()[0];
}
const value = d.y && yScale(d.y[0]);
if (Number.isNaN(value)) {
return yScale.range()[0];
}
return value;
}).y1(d => d.y && yScale(d.y[1]));
const d = areaPath.curve((0, _getCurve.getCurveFactory)(curve))(d3Data) || '';
areaPlotData.push({
area: series[seriesId].area,
color: series[seriesId].color,
gradientId,
d,
seriesId
});
}
}
return areaPlotData;
}, [seriesData, defaultXAxisId, defaultYAxisId, xAxes, yAxes, getGradientId]);
return allData;
}
;