|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import themes from '@/styles/theme.less';
- import * as echarts from 'echarts';
- import React, { useEffect, useRef } from 'react';
- import styles from './index.less';
-
- const color1 = new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- { offset: 0, color: '#c73131' },
- { offset: 1, color: '#ff7e96' },
- ],
- false,
- );
-
- const color2 = new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- { offset: 0, color: '#6ac21d' },
- { offset: 1, color: '#96e850' },
- ],
- false,
- );
- const color3 = new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- { offset: 0, color: '#8c8c8c' },
- { offset: 1, color: '#c8c6c6' },
- ],
- false,
- );
- const color4 = new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- { offset: 0, color: '#ecb934' },
- { offset: 1, color: '#f0864d' },
- ],
- false,
- );
-
- const color5 = new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- { offset: 0, color: '#7ea9fe' },
- { offset: 1, color: '#1664ff' },
- ],
- false,
- );
-
- const color6 = new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- { offset: 0, color: 'rgba(255, 255, 255, 0.62)' },
- { offset: 1, color: '#ebf2ff ' },
- ],
- false,
- );
-
- export type ExperimentStatistics = {
- Failed: number;
- Pending: number;
- Running: number;
- Succeeded: number;
- Terminated: number;
- };
-
- type ExperimentChartProps = {
- style?: React.CSSProperties;
- chartData: ExperimentStatistics;
- };
-
- function ExperimentChart({ chartData, style }: ExperimentChartProps) {
- const chartRef = useRef<HTMLDivElement>(null);
- const total =
- chartData.Failed +
- chartData.Pending +
- chartData.Running +
- chartData.Succeeded +
- chartData.Terminated;
- const options: echarts.EChartsOption = {
- title: {
- show: true,
- left: '29%',
- top: 'center',
- textAlign: 'center',
- text: [`{a|${total}}`, '{b|实验状态}'].join('\n'),
- textStyle: {
- rich: {
- a: {
- color: themes['textColor'],
- fontSize: 20,
- fontWeight: 700,
- lineHeight: 28,
- },
- b: {
- color: themes['textColorSecondary'],
- fontSize: 10,
- fontWeight: 'normal',
- },
- },
- },
- },
- tooltip: {
- trigger: 'item',
- },
- legend: {
- top: 'center',
- right: '5%',
- orient: 'vertical',
- icon: 'circle',
- itemWidth: 6,
- itemGap: 20,
- height: 100,
- },
- color: [color1, color2, color3, color4, color5],
- series: [
- {
- type: 'pie',
- radius: ['70%', '80%'],
- center: ['30%', '50%'],
- avoidLabelOverlap: false,
- padAngle: 3,
- itemStyle: {
- borderRadius: 3,
- },
- label: {
- show: false,
- },
- emphasis: {
- label: {
- show: false,
- },
- },
- labelLine: {
- show: false,
- },
- data: [
- { value: chartData.Failed, name: '失败' },
- { value: chartData.Succeeded, name: '成功' },
- { value: chartData.Terminated, name: '中止' },
- { value: chartData.Pending, name: '等待' },
- { value: chartData.Running, name: '运行中' },
- ],
- },
- {
- type: 'pie',
- radius: '60%',
- center: ['30%', '50%'],
- avoidLabelOverlap: false,
- label: {
- show: false,
- },
- tooltip: {
- show: false,
- },
- emphasis: {
- label: {
- show: false,
- },
- disabled: true,
- },
- animation: false,
- labelLine: {
- show: false,
- },
- data: [
- {
- value: 100,
- itemStyle: { color: color6, borderColor: 'rgba(22, 100, 255, 0.08)', borderWidth: 1 },
- },
- ],
- },
- ],
- };
-
- useEffect(() => {
- // 创建一个echarts实例,返回echarts实例
- const chart = echarts.init(chartRef.current);
-
- // 设置图表实例的配置项和数据
- chart.setOption(options);
-
- // 组件卸载
- return () => {
- // myChart.dispose() 销毁实例
- chart.dispose();
- };
- }, []);
-
- return (
- <div className={styles['experiment-chart']} style={style}>
- <div style={{ width: '100%', height: '100%' }} ref={chartRef}></div>
- </div>
- );
- }
-
- export default ExperimentChart;
|