@mui/x-charts
Version:
The community edition of MUI X Charts components.
63 lines (62 loc) • 2.18 kB
JavaScript
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = ChartsContinuousGradient;
var React = _interopRequireWildcard(require("react"));
var _d3Interpolate = require("@mui/x-charts-vendor/d3-interpolate");
var _jsxRuntime = require("react/jsx-runtime");
const PX_PRECISION = 10;
function ChartsContinuousGradient(props) {
const {
gradientUnits,
isReversed,
gradientId,
size,
direction,
scale,
colorScale,
colorMap
} = props;
const extremumValues = [colorMap.min ?? 0, colorMap.max ?? 100];
const extremumPositions = extremumValues.map(scale).filter(p => p !== undefined);
if (extremumPositions.length !== 2) {
return null;
}
const interpolator = typeof extremumValues[0] === 'number' ? (0, _d3Interpolate.interpolateNumber)(extremumValues[0], extremumValues[1]) : (0, _d3Interpolate.interpolateDate)(extremumValues[0], extremumValues[1]);
const numberOfPoints = Math.round((Math.max(...extremumPositions) - Math.min(...extremumPositions)) / PX_PRECISION);
const keyPrefix = `${extremumValues[0]}-${extremumValues[1]}-`;
return /*#__PURE__*/(0, _jsxRuntime.jsx)("linearGradient", {
id: gradientId,
x1: "0",
x2: "0",
y1: "0",
y2: "0",
[`${direction}${isReversed ? 1 : 2}`]: gradientUnits === 'objectBoundingBox' ? 1 : `${size}px`,
gradientUnits: gradientUnits ?? 'userSpaceOnUse' // Use the SVG coordinate instead of the component ones.
,
children: Array.from({
length: numberOfPoints + 1
}, (_, index) => {
const value = interpolator(index / numberOfPoints);
if (value === undefined) {
return null;
}
const x = scale(value);
if (x === undefined) {
return null;
}
const offset = isReversed ? 1 - x / size : x / size;
const color = colorScale(value);
if (color === null) {
return null;
}
return /*#__PURE__*/(0, _jsxRuntime.jsx)("stop", {
offset: offset,
stopColor: color,
stopOpacity: 1
}, keyPrefix + index);
})
});
}
;