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 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import * as echarts from 'echarts';
  2. import { useEffect, useRef } from 'react';
  3. import styles from './index.less';
  4. import './tooltip.css';
  5. const colors = [
  6. '#0D5EF8',
  7. '#6AC21D',
  8. '#F98E1B',
  9. '#ECB934',
  10. '#8A34EC',
  11. '#FF1493',
  12. '#FFFF00',
  13. '#DAA520',
  14. '#CD853F',
  15. '#FF6347',
  16. '#808080',
  17. '#00BFFF',
  18. '#008000',
  19. '#00FFFF',
  20. '#FFFACD',
  21. '#FFA500',
  22. '#FF4500',
  23. '#800080',
  24. '#FF1493',
  25. '#000080',
  26. ];
  27. const backgroundColor = new echarts.graphic.LinearGradient(
  28. 0,
  29. 0,
  30. 0,
  31. 1,
  32. [
  33. { offset: 0, color: '#ffffff' },
  34. { offset: 1, color: '#fdfeff' },
  35. ],
  36. false,
  37. );
  38. function getTooltip(xTitle: string, xValue: number, yTitle: string, yValue: number) {
  39. const str = `<div class="metrics-tooltip">
  40. <span class="y-text">Y:</span>
  41. <span class="x-text">X:</span>
  42. <div class="title">${yTitle}</div>
  43. <div class="value">${yValue}</div>
  44. <div class="title" style="margin-top: 10px">${xTitle}</div>
  45. <div class="value">${xValue}</div>
  46. <div>`;
  47. return str;
  48. }
  49. export type MetricsChatData = {
  50. name: string;
  51. values: number[];
  52. version: string;
  53. iters: number[];
  54. };
  55. export type MetricsChartProps = {
  56. name: string;
  57. chartData: MetricsChatData[];
  58. };
  59. function MetricsChart({ name, chartData }: MetricsChartProps) {
  60. const chartRef = useRef<HTMLDivElement>(null);
  61. const xAxisData = chartData[0]?.iters;
  62. const seriesData = chartData.map((item) => {
  63. return {
  64. name: item.version,
  65. type: 'line' as const,
  66. smooth: true,
  67. data: item.values,
  68. };
  69. });
  70. const options: echarts.EChartsOption = {
  71. backgroundColor: backgroundColor,
  72. title: {
  73. show: false,
  74. },
  75. tooltip: {
  76. trigger: 'item',
  77. padding: 10,
  78. formatter: (params: any) => {
  79. const { name: xTitle, data } = params;
  80. return getTooltip('step', xTitle, name, data);
  81. },
  82. },
  83. legend: {
  84. bottom: 10,
  85. icon: 'rect',
  86. itemWidth: 10,
  87. itemHeight: 10,
  88. itemGap: 20,
  89. textStyle: {
  90. color: 'rgba(29, 29, 32, 0.75)',
  91. fontSize: 12,
  92. },
  93. },
  94. color: colors,
  95. grid: {
  96. left: '15',
  97. right: '15',
  98. top: '20',
  99. bottom: '60',
  100. containLabel: true,
  101. },
  102. xAxis: {
  103. type: 'category',
  104. boundaryGap: true,
  105. offset: 10,
  106. data: xAxisData,
  107. axisLabel: {
  108. color: 'rgba(29, 29, 32, 0.75)',
  109. fontSize: 12,
  110. },
  111. axisTick: {
  112. show: false,
  113. },
  114. axisLine: {
  115. lineStyle: {
  116. color: '#eaeaea',
  117. width: 1,
  118. },
  119. },
  120. },
  121. yAxis: {
  122. type: 'value',
  123. axisLabel: {
  124. color: 'rgba(29, 29, 32, 0.75)',
  125. fontSize: 12,
  126. margin: 15,
  127. },
  128. axisLine: {
  129. show: false,
  130. },
  131. splitLine: {
  132. lineStyle: {
  133. color: '#e4e4e4',
  134. width: 1,
  135. type: 'dashed',
  136. },
  137. },
  138. },
  139. series: seriesData,
  140. };
  141. useEffect(() => {
  142. // 创建一个echarts实例,返回echarts实例
  143. const chart = echarts.init(chartRef.current);
  144. // 设置图表实例的配置项和数据
  145. chart.setOption(options);
  146. // 组件卸载
  147. return () => {
  148. // myChart.dispose() 销毁实例
  149. chart.dispose();
  150. };
  151. }, []);
  152. return (
  153. <div className={styles['metrics-chart']}>
  154. <div className={styles['metrics-chart__title']}>
  155. <img src={require('@/assets/img/metrics-title-icon.png')}></img>
  156. <span>{name}</span>
  157. </div>
  158. <div className={styles['metrics-chart__chart']} ref={chartRef}></div>
  159. </div>
  160. );
  161. }
  162. export default MetricsChart;