2022-10-31 10:23:26 +08:00
|
|
|
import { presetPrimaryColors } from '@ant-design/colors';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
import type { CircleProps } from './Circle';
|
|
|
|
import type { ProgressProps } from './progress';
|
2020-08-20 15:27:16 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
export function validProgress(progress?: number) {
|
2019-01-18 15:19:36 +08:00
|
|
|
if (!progress || progress < 0) {
|
|
|
|
return 0;
|
2019-08-14 18:21:24 +08:00
|
|
|
}
|
|
|
|
if (progress > 100) {
|
2019-01-18 15:19:36 +08:00
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return progress;
|
2019-03-06 13:45:40 +08:00
|
|
|
}
|
2020-08-20 15:27:16 +08:00
|
|
|
|
2022-10-31 10:23:26 +08:00
|
|
|
export function getSuccessPercent({ success, successPercent }: ProgressProps) {
|
2020-08-20 15:27:16 +08:00
|
|
|
let percent = successPercent;
|
|
|
|
/** @deprecated Use `percent` instead */
|
|
|
|
if (success && 'progress' in success) {
|
|
|
|
percent = success.progress;
|
|
|
|
}
|
|
|
|
if (success && 'percent' in success) {
|
|
|
|
percent = success.percent;
|
|
|
|
}
|
|
|
|
return percent;
|
|
|
|
}
|
2022-10-31 10:23:26 +08:00
|
|
|
|
|
|
|
export const getPercentage = ({ percent, success, successPercent }: ProgressProps) => {
|
|
|
|
const realSuccessPercent = validProgress(getSuccessPercent({ success, successPercent }));
|
|
|
|
return [realSuccessPercent, validProgress(validProgress(percent) - realSuccessPercent)];
|
|
|
|
};
|
|
|
|
|
2023-06-20 22:08:25 +08:00
|
|
|
export const getStrokeColor = ({
|
|
|
|
success = {},
|
|
|
|
strokeColor,
|
|
|
|
}: Partial<CircleProps>): (string | Record<PropertyKey, string>)[] => {
|
2022-10-31 10:23:26 +08:00
|
|
|
const { strokeColor: successColor } = success;
|
|
|
|
return [successColor || presetPrimaryColors.green, strokeColor || null!];
|
|
|
|
};
|
2023-03-01 11:49:42 +08:00
|
|
|
|
|
|
|
export const getSize = (
|
|
|
|
size: ProgressProps['size'],
|
|
|
|
type: ProgressProps['type'] | 'step',
|
|
|
|
extra?: {
|
|
|
|
steps?: number;
|
|
|
|
strokeWidth?: number;
|
|
|
|
},
|
|
|
|
): [number, number] => {
|
|
|
|
let width: number = -1;
|
|
|
|
let height: number = -1;
|
|
|
|
if (type === 'step') {
|
|
|
|
const steps = extra!.steps!;
|
|
|
|
const strokeWidth = extra!.strokeWidth!;
|
|
|
|
if (typeof size === 'string' || typeof size === 'undefined') {
|
|
|
|
width = size === 'small' ? 2 : 14;
|
|
|
|
height = strokeWidth ?? 8;
|
|
|
|
} else if (typeof size === 'number') {
|
|
|
|
[width, height] = [size, size];
|
|
|
|
} else {
|
2023-06-20 22:08:25 +08:00
|
|
|
[width = 14, height = 8] = size as [number, number];
|
2023-03-01 11:49:42 +08:00
|
|
|
}
|
|
|
|
width *= steps;
|
|
|
|
} else if (type === 'line') {
|
|
|
|
const strokeWidth = extra?.strokeWidth;
|
|
|
|
if (typeof size === 'string' || typeof size === 'undefined') {
|
|
|
|
height = strokeWidth || (size === 'small' ? 6 : 8);
|
|
|
|
} else if (typeof size === 'number') {
|
|
|
|
[width, height] = [size, size];
|
|
|
|
} else {
|
2023-06-20 22:08:25 +08:00
|
|
|
[width = -1, height = 8] = size as [number, number];
|
2023-03-01 11:49:42 +08:00
|
|
|
}
|
|
|
|
} else if (type === 'circle' || type === 'dashboard') {
|
|
|
|
if (typeof size === 'string' || typeof size === 'undefined') {
|
|
|
|
[width, height] = size === 'small' ? [60, 60] : [120, 120];
|
|
|
|
} else if (typeof size === 'number') {
|
|
|
|
[width, height] = [size, size];
|
|
|
|
} else {
|
2023-06-20 22:08:25 +08:00
|
|
|
width = (size[0] ?? size[1] ?? 120) as number;
|
|
|
|
height = (size[0] ?? size[1] ?? 120) as number;
|
2023-03-01 11:49:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [width, height];
|
|
|
|
};
|