index.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import type { CSSProperties } from 'react'
  2. import { memo, useMemo } from 'react'
  3. import ReactECharts from 'echarts-for-react'
  4. import type { EChartsOption } from 'echarts'
  5. import style from './index.module.css'
  6. import classNames from '@/utils/classnames'
  7. export type SimplePieChartProps = {
  8. percentage?: number
  9. fill?: string
  10. stroke?: string
  11. size?: number
  12. animationDuration?: number
  13. className?: string
  14. }
  15. const SimplePieChart = ({ percentage = 80, fill = '#fdb022', stroke = '#f79009', size = 12, animationDuration, className }: SimplePieChartProps) => {
  16. const option: EChartsOption = useMemo(() => ({
  17. series: [
  18. {
  19. type: 'pie',
  20. radius: ['83%', '100%'],
  21. animation: false,
  22. data: [
  23. { value: 100, itemStyle: { color: stroke } },
  24. ],
  25. emphasis: {
  26. disabled: true,
  27. },
  28. labelLine: {
  29. show: false,
  30. },
  31. cursor: 'default',
  32. },
  33. {
  34. type: 'pie',
  35. radius: '83%',
  36. animationDuration: animationDuration ?? 600,
  37. data: [
  38. { value: percentage, itemStyle: { color: fill } },
  39. { value: 100 - percentage, itemStyle: { color: '#fff' } },
  40. ],
  41. emphasis: {
  42. disabled: true,
  43. },
  44. labelLine: {
  45. show: false,
  46. },
  47. cursor: 'default',
  48. },
  49. ],
  50. }), [stroke, fill, percentage, animationDuration])
  51. return (
  52. <ReactECharts
  53. option={option}
  54. className={classNames(style.simplePieChart, className)}
  55. style={{
  56. '--simple-pie-chart-color': fill,
  57. 'width': size,
  58. 'height': size,
  59. } as CSSProperties}
  60. />
  61. )
  62. }
  63. export default memo(SimplePieChart)