2019-01-18 15:19:36 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { Circle as RCCircle } from 'rc-progress';
|
2020-08-20 17:15:40 +08:00
|
|
|
import { presetPrimaryColors } from '@ant-design/colors';
|
2019-06-26 17:31:54 +08:00
|
|
|
import classNames from 'classnames';
|
2020-08-20 15:27:16 +08:00
|
|
|
import { validProgress, getSuccessPercent } from './utils';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ProgressProps } from './progress';
|
2019-01-18 15:19:36 +08:00
|
|
|
|
|
|
|
interface CircleProps extends ProgressProps {
|
|
|
|
prefixCls: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
progressStatus: string;
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
function getPercentage({ percent, success, successPercent }: CircleProps) {
|
2021-06-05 13:25:19 +08:00
|
|
|
const realSuccessPercent = validProgress(getSuccessPercent({ success, successPercent }));
|
|
|
|
return [realSuccessPercent, validProgress(validProgress(percent) - realSuccessPercent)];
|
2019-01-18 15:19:36 +08:00
|
|
|
}
|
|
|
|
|
2021-12-23 11:51:10 +08:00
|
|
|
function getStrokeColor({
|
|
|
|
success = {},
|
|
|
|
strokeColor,
|
|
|
|
}: Partial<CircleProps>): (string | Record<string, string>)[] {
|
2021-07-30 13:17:57 +08:00
|
|
|
const { strokeColor: successColor } = success;
|
2021-12-23 11:51:10 +08:00
|
|
|
return [successColor || presetPrimaryColors.green, strokeColor || null!];
|
2021-07-30 13:17:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 11:56:57 +08:00
|
|
|
const Circle: React.FC<CircleProps> = props => {
|
2019-01-18 15:19:36 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
width,
|
|
|
|
strokeWidth,
|
2022-05-06 18:34:45 +08:00
|
|
|
trailColor = null as any,
|
|
|
|
strokeLinecap = 'round',
|
2019-01-18 15:19:36 +08:00
|
|
|
gapPosition,
|
|
|
|
gapDegree,
|
|
|
|
type,
|
|
|
|
children,
|
2021-07-30 13:17:57 +08:00
|
|
|
success,
|
2019-01-18 15:19:36 +08:00
|
|
|
} = props;
|
|
|
|
const circleSize = width || 120;
|
|
|
|
const circleStyle = {
|
|
|
|
width: circleSize,
|
|
|
|
height: circleSize,
|
|
|
|
fontSize: circleSize * 0.15 + 6,
|
2020-08-20 17:15:40 +08:00
|
|
|
} as React.CSSProperties;
|
2019-01-18 15:19:36 +08:00
|
|
|
const circleWidth = strokeWidth || 6;
|
|
|
|
const gapPos = gapPosition || (type === 'dashboard' && 'bottom') || 'top';
|
2020-08-20 17:15:40 +08:00
|
|
|
|
|
|
|
const getGapDegree = () => {
|
|
|
|
// Support gapDeg = 0 when type = 'dashboard'
|
|
|
|
if (gapDegree || gapDegree === 0) {
|
|
|
|
return gapDegree;
|
|
|
|
}
|
|
|
|
if (type === 'dashboard') {
|
|
|
|
return 75;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
2020-03-22 17:59:43 +08:00
|
|
|
|
2019-12-04 21:01:55 +08:00
|
|
|
// using className to style stroke color
|
2021-06-05 13:25:19 +08:00
|
|
|
const isGradient = Object.prototype.toString.call(props.strokeColor) === '[object Object]';
|
2021-07-30 13:17:57 +08:00
|
|
|
const strokeColor = getStrokeColor({ success, strokeColor: props.strokeColor });
|
2019-06-26 17:31:54 +08:00
|
|
|
|
|
|
|
const wrapperClassName = classNames(`${prefixCls}-inner`, {
|
|
|
|
[`${prefixCls}-circle-gradient`]: isGradient,
|
|
|
|
});
|
2019-01-18 15:19:36 +08:00
|
|
|
|
|
|
|
return (
|
2019-06-26 17:31:54 +08:00
|
|
|
<div className={wrapperClassName} style={circleStyle}>
|
2019-01-18 15:19:36 +08:00
|
|
|
<RCCircle
|
|
|
|
percent={getPercentage(props)}
|
|
|
|
strokeWidth={circleWidth}
|
|
|
|
trailWidth={circleWidth}
|
2019-06-26 17:31:54 +08:00
|
|
|
strokeColor={strokeColor}
|
2019-01-18 15:19:36 +08:00
|
|
|
strokeLinecap={strokeLinecap}
|
|
|
|
trailColor={trailColor}
|
|
|
|
prefixCls={prefixCls}
|
2020-08-20 17:15:40 +08:00
|
|
|
gapDegree={getGapDegree()}
|
2019-01-18 15:19:36 +08:00
|
|
|
gapPosition={gapPos}
|
|
|
|
/>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Circle;
|