@mui/x-charts
Version: 
The community edition of the charts components (MUI X).
110 lines • 4.25 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["id", "classes", "color", "startAngle", "endAngle", "paddingAngle", "innerRadius", "outerRadius", "cornerRadius", "formattedArcLabel", "isHighlighted", "isFaded", "style"];
import * as React from 'react';
import PropTypes from 'prop-types';
import { animated, to } from '@react-spring/web';
import { arc as d3Arc } from 'd3-shape';
import composeClasses from '@mui/utils/composeClasses';
import generateUtilityClass from '@mui/utils/generateUtilityClass';
import { styled } from '@mui/material/styles';
import generateUtilityClasses from '@mui/utils/generateUtilityClasses';
import { jsx as _jsx } from "react/jsx-runtime";
export function getPieArcLabelUtilityClass(slot) {
  return generateUtilityClass('MuiPieArcLabel', slot);
}
export var pieArcLabelClasses = generateUtilityClasses('MuiPieArcLabel', ['root', 'highlighted', 'faded']);
var useUtilityClasses = function useUtilityClasses(ownerState) {
  var classes = ownerState.classes,
    id = ownerState.id,
    isFaded = ownerState.isFaded,
    isHighlighted = ownerState.isHighlighted;
  var slots = {
    root: ['root', "series-".concat(id), isHighlighted && 'highlighted', isFaded && 'faded']
  };
  return composeClasses(slots, getPieArcLabelUtilityClass, classes);
};
var PieArcLabelRoot = styled(animated.text, {
  name: 'MuiPieArcLabel',
  slot: 'Root',
  overridesResolver: function overridesResolver(_, styles) {
    return styles.root;
  }
})(function (_ref) {
  var theme = _ref.theme;
  return {
    fill: (theme.vars || theme).palette.text.primary,
    textAnchor: 'middle',
    dominantBaseline: 'middle'
  };
});
/**
 * Helper to compute label position.
 * It's not an inline function because we need it in inerpolation.
 */
var getLabelPosition = function getLabelPosition(formattedArcLabel, variable) {
  return function (startAngle, endAngle, padAngle, innerRadius, outerRadius, cornerRadius) {
    if (!formattedArcLabel) {
      return 0;
    }
    var _ref2 = d3Arc().cornerRadius(cornerRadius).centroid({
        padAngle: padAngle,
        startAngle: startAngle,
        endAngle: endAngle,
        innerRadius: innerRadius,
        outerRadius: outerRadius
      }),
      _ref3 = _slicedToArray(_ref2, 2),
      x = _ref3[0],
      y = _ref3[1];
    if (variable === 'x') {
      return x;
    }
    return y;
  };
};
function PieArcLabel(props) {
  var id = props.id,
    innerClasses = props.classes,
    color = props.color,
    startAngle = props.startAngle,
    endAngle = props.endAngle,
    paddingAngle = props.paddingAngle,
    innerRadius = props.innerRadius,
    outerRadius = props.outerRadius,
    cornerRadius = props.cornerRadius,
    formattedArcLabel = props.formattedArcLabel,
    isHighlighted = props.isHighlighted,
    isFaded = props.isFaded,
    style = props.style,
    other = _objectWithoutProperties(props, _excluded);
  var ownerState = {
    id: id,
    classes: innerClasses,
    color: color,
    isFaded: isFaded,
    isHighlighted: isHighlighted
  };
  var classes = useUtilityClasses(ownerState);
  return /*#__PURE__*/_jsx(PieArcLabelRoot, _extends({
    className: classes.root
  }, other, {
    style: _extends({
      x: to([startAngle, endAngle, paddingAngle, innerRadius, outerRadius, cornerRadius], getLabelPosition(formattedArcLabel, 'x')),
      y: to([startAngle, endAngle, paddingAngle, innerRadius, outerRadius, cornerRadius], getLabelPosition(formattedArcLabel, 'y'))
    }, style),
    children: formattedArcLabel
  }));
}
process.env.NODE_ENV !== "production" ? PieArcLabel.propTypes = {
  // ----------------------------- Warning --------------------------------
  // | These PropTypes are generated from the TypeScript type definitions |
  // | To update them edit the TypeScript types and run "yarn proptypes"  |
  // ----------------------------------------------------------------------
  classes: PropTypes.object,
  formattedArcLabel: PropTypes.string,
  isFaded: PropTypes.bool.isRequired,
  isHighlighted: PropTypes.bool.isRequired
} : void 0;
export { PieArcLabel };