You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import themes from '@/styles/theme.less';
  2. import * as echarts from 'echarts';
  3. import React, { useEffect, useRef } from 'react';
  4. import styles from './index.less';
  5. const color1 = new echarts.graphic.LinearGradient(
  6. 0,
  7. 0,
  8. 0,
  9. 1,
  10. [
  11. { offset: 0, color: '#c73131' },
  12. { offset: 1, color: '#ff7e96' },
  13. ],
  14. false,
  15. );
  16. const color2 = new echarts.graphic.LinearGradient(
  17. 0,
  18. 0,
  19. 0,
  20. 1,
  21. [
  22. { offset: 0, color: '#6ac21d' },
  23. { offset: 1, color: '#96e850' },
  24. ],
  25. false,
  26. );
  27. const color3 = new echarts.graphic.LinearGradient(
  28. 0,
  29. 0,
  30. 0,
  31. 1,
  32. [
  33. { offset: 0, color: '#8c8c8c' },
  34. { offset: 1, color: '#c8c6c6' },
  35. ],
  36. false,
  37. );
  38. const color4 = new echarts.graphic.LinearGradient(
  39. 0,
  40. 0,
  41. 0,
  42. 1,
  43. [
  44. { offset: 0, color: '#ecb934' },
  45. { offset: 1, color: '#f0864d' },
  46. ],
  47. false,
  48. );
  49. const color5 = new echarts.graphic.LinearGradient(
  50. 0,
  51. 0,
  52. 0,
  53. 1,
  54. [
  55. { offset: 0, color: '#7ea9fe' },
  56. { offset: 1, color: '#1664ff' },
  57. ],
  58. false,
  59. );
  60. const color6 = new echarts.graphic.LinearGradient(
  61. 0,
  62. 0,
  63. 0,
  64. 1,
  65. [
  66. { offset: 0, color: 'rgba(255, 255, 255, 0.62)' },
  67. { offset: 1, color: '#ebf2ff ' },
  68. ],
  69. false,
  70. );
  71. export type ExperimentStatistics = {
  72. Failed: number;
  73. Pending: number;
  74. Running: number;
  75. Succeeded: number;
  76. Terminated: number;
  77. };
  78. type ExperimentChartProps = {
  79. style?: React.CSSProperties;
  80. chartData: ExperimentStatistics;
  81. };
  82. function ExperimentChart({ chartData, style }: ExperimentChartProps) {
  83. const chartRef = useRef<HTMLDivElement>(null);
  84. const total =
  85. chartData.Failed +
  86. chartData.Pending +
  87. chartData.Running +
  88. chartData.Succeeded +
  89. chartData.Terminated;
  90. const options: echarts.EChartsOption = {
  91. title: {
  92. show: true,
  93. left: '29%',
  94. top: 'center',
  95. textAlign: 'center',
  96. text: [`{a|${total}}`, '{b|实验状态}'].join('\n'),
  97. textStyle: {
  98. rich: {
  99. a: {
  100. color: themes['textColor'],
  101. fontSize: 20,
  102. fontWeight: 700,
  103. lineHeight: 28,
  104. },
  105. b: {
  106. color: themes['textColorSecondary'],
  107. fontSize: 10,
  108. fontWeight: 'normal',
  109. },
  110. },
  111. },
  112. },
  113. tooltip: {
  114. trigger: 'item',
  115. },
  116. legend: {
  117. top: 'center',
  118. right: '5%',
  119. orient: 'vertical',
  120. icon: 'circle',
  121. itemWidth: 6,
  122. itemGap: 20,
  123. height: 100,
  124. },
  125. color: [color1, color2, color3, color4, color5],
  126. series: [
  127. {
  128. type: 'pie',
  129. radius: ['70%', '80%'],
  130. center: ['30%', '50%'],
  131. avoidLabelOverlap: false,
  132. padAngle: 3,
  133. itemStyle: {
  134. borderRadius: 3,
  135. },
  136. label: {
  137. show: false,
  138. },
  139. emphasis: {
  140. label: {
  141. show: false,
  142. },
  143. },
  144. labelLine: {
  145. show: false,
  146. },
  147. data: [
  148. { value: chartData.Failed, name: '失败' },
  149. { value: chartData.Succeeded, name: '成功' },
  150. { value: chartData.Terminated, name: '中止' },
  151. { value: chartData.Pending, name: '等待' },
  152. { value: chartData.Running, name: '运行中' },
  153. ],
  154. },
  155. {
  156. type: 'pie',
  157. radius: '60%',
  158. center: ['30%', '50%'],
  159. avoidLabelOverlap: false,
  160. label: {
  161. show: false,
  162. },
  163. tooltip: {
  164. show: false,
  165. },
  166. emphasis: {
  167. label: {
  168. show: false,
  169. },
  170. disabled: true,
  171. },
  172. animation: false,
  173. labelLine: {
  174. show: false,
  175. },
  176. data: [
  177. {
  178. value: 100,
  179. itemStyle: { color: color6, borderColor: 'rgba(22, 100, 255, 0.08)', borderWidth: 1 },
  180. },
  181. ],
  182. },
  183. ],
  184. };
  185. useEffect(() => {
  186. // 创建一个echarts实例,返回echarts实例
  187. const chart = echarts.init(chartRef.current);
  188. // 设置图表实例的配置项和数据
  189. chart.setOption(options);
  190. // 组件卸载
  191. return () => {
  192. // myChart.dispose() 销毁实例
  193. chart.dispose();
  194. };
  195. }, []);
  196. return (
  197. <div className={styles['experiment-chart']} style={style}>
  198. <div style={{ width: '100%', height: '100%' }} ref={chartRef}></div>
  199. </div>
  200. );
  201. }
  202. export default ExperimentChart;