utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { presetPrimaryColors } from '@ant-design/colors';
  2. export function validProgress(progress) {
  3. if (!progress || progress < 0) {
  4. return 0;
  5. }
  6. if (progress > 100) {
  7. return 100;
  8. }
  9. return progress;
  10. }
  11. export function getSuccessPercent({
  12. success,
  13. successPercent
  14. }) {
  15. let percent = successPercent;
  16. /** @deprecated Use `percent` instead */
  17. if (success && 'progress' in success) {
  18. percent = success.progress;
  19. }
  20. if (success && 'percent' in success) {
  21. percent = success.percent;
  22. }
  23. return percent;
  24. }
  25. export const getPercentage = ({
  26. percent,
  27. success,
  28. successPercent
  29. }) => {
  30. const realSuccessPercent = validProgress(getSuccessPercent({
  31. success,
  32. successPercent
  33. }));
  34. return [realSuccessPercent, validProgress(validProgress(percent) - realSuccessPercent)];
  35. };
  36. export const getStrokeColor = ({
  37. success = {},
  38. strokeColor
  39. }) => {
  40. const {
  41. strokeColor: successColor
  42. } = success;
  43. return [successColor || presetPrimaryColors.green, strokeColor || null];
  44. };
  45. export const getSize = (size, type, extra) => {
  46. var _a, _b, _c, _d;
  47. let width = -1;
  48. let height = -1;
  49. if (type === 'step') {
  50. const steps = extra.steps;
  51. const strokeWidth = extra.strokeWidth;
  52. if (typeof size === 'string' || typeof size === 'undefined') {
  53. width = size === 'small' ? 2 : 14;
  54. height = strokeWidth !== null && strokeWidth !== void 0 ? strokeWidth : 8;
  55. } else if (typeof size === 'number') {
  56. [width, height] = [size, size];
  57. } else {
  58. [width = 14, height = 8] = Array.isArray(size) ? size : [size.width, size.height];
  59. }
  60. width *= steps;
  61. } else if (type === 'line') {
  62. const strokeWidth = extra === null || extra === void 0 ? void 0 : extra.strokeWidth;
  63. if (typeof size === 'string' || typeof size === 'undefined') {
  64. height = strokeWidth || (size === 'small' ? 6 : 8);
  65. } else if (typeof size === 'number') {
  66. [width, height] = [size, size];
  67. } else {
  68. [width = -1, height = 8] = Array.isArray(size) ? size : [size.width, size.height];
  69. }
  70. } else if (type === 'circle' || type === 'dashboard') {
  71. if (typeof size === 'string' || typeof size === 'undefined') {
  72. [width, height] = size === 'small' ? [60, 60] : [120, 120];
  73. } else if (typeof size === 'number') {
  74. [width, height] = [size, size];
  75. } else if (Array.isArray(size)) {
  76. width = (_b = (_a = size[0]) !== null && _a !== void 0 ? _a : size[1]) !== null && _b !== void 0 ? _b : 120;
  77. height = (_d = (_c = size[0]) !== null && _c !== void 0 ? _c : size[1]) !== null && _d !== void 0 ? _d : 120;
  78. }
  79. }
  80. return [width, height];
  81. };