RadarChartModule.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. //y轴显示数据
  3. Data:{
  4. type:number, 0(实线) 1虚线(预测) 2(填充)
  5. name:string,
  6. yIndex:number, //yName的下标索引(第几个y坐标轴)
  7. data:string[],
  8. }
  9. props:{
  10. yName:string || string[], //y轴名称 一个或多个
  11. xData:string[], //x轴时间数据
  12. dataList:Data[], //折线的数据列表
  13. currentType:string, //当前选择那个tabs 需要外部更新当前选中type时传入
  14. typeList:string[], //图表下的选择类型列表
  15. onChange:(id:number)=>{}
  16. }
  17. */
  18. import { Empty } from 'antd';
  19. import * as echarts from 'echarts';
  20. import { useEffect, useRef } from 'react';
  21. import styles from './index.less';
  22. //图表模块
  23. const RadarChartModule = (props) => {
  24. const chartDomRef = useRef();
  25. const chartRef = useRef();
  26. const { indicator, data, name = '' } = props;
  27. useEffect(() => {
  28. chartRef.current = echarts.init(chartDomRef.current);
  29. window.addEventListener('resize', resetChart);
  30. return () => window.removeEventListener('resize', resetChart);
  31. }, []);
  32. useEffect(() => {
  33. if (!chartRef.current) return;
  34. const option = { ...defaultOption };
  35. option.radar.indicator = indicator;
  36. option.series = [{ ...option.series[0], data: data, name }];
  37. console.log(JSON.stringify(option));
  38. chartRef.current.clear();
  39. chartRef.current.setOption(option);
  40. chartRef.current.resize();
  41. }, [indicator, data]);
  42. const resetChart = () => {
  43. if (chartRef.current) chartRef.current.resize();
  44. };
  45. const getStyle = () => {
  46. if (data && data.length != 0) return { width: '100%', height: '100%' };
  47. else return { width: '100%', height: '100%', display: 'none' };
  48. };
  49. return (
  50. <div className={styles.content}>
  51. <div style={getStyle()} ref={chartDomRef} />
  52. {(!data || data.length == 0) && <Empty />}
  53. </div>
  54. );
  55. };
  56. export default RadarChartModule;
  57. const colors = [
  58. // '#666600',
  59. // '#91cc75',
  60. '#fac858',
  61. '#ee6666',
  62. '#73c0de',
  63. '#3ba272',
  64. '#fc8452',
  65. '#9a60b4',
  66. '#ea7ccc',
  67. ];
  68. const defaultOption = {
  69. color: colors,
  70. tooltip: {
  71. trigger: 'item',
  72. },
  73. radar: {
  74. name: {
  75. textStyle: {
  76. fontSize: 12,
  77. },
  78. },
  79. splitArea: {
  80. areaStyle: {
  81. color: [
  82. 'rgba(27, 133, 168, 0.8)',
  83. 'rgba(27, 133, 168, 0.7)',
  84. 'rgba(27, 133, 168, 0.6)',
  85. 'rgba(27, 133, 168, 0.4)',
  86. 'rgba(27, 133, 168, 0.3)',
  87. ],
  88. // shadowColor: 'rgba(0, 0, 0, 0.2)',
  89. // shadowBlur: 10
  90. },
  91. },
  92. axisLine: {
  93. lineStyle: {
  94. color: '#238BB6',
  95. },
  96. },
  97. splitLine: {
  98. show: false,
  99. },
  100. indicator: [
  101. { name: 'Sales' },
  102. { name: 'Administration' },
  103. { name: 'Information Technology' },
  104. { name: 'Customer Support' },
  105. { name: 'Development' },
  106. { name: 'Marketing' },
  107. ],
  108. },
  109. series: [
  110. {
  111. name: '',
  112. type: 'radar',
  113. tooltip: {
  114. trigger: 'item',
  115. },
  116. symbol: 'none',
  117. // areaStyle: {},
  118. data: [
  119. {
  120. value: [2, 3, 0, 100, 0, 1],
  121. },
  122. {
  123. value: [3, 5, 10, 90, 0, 1],
  124. },
  125. ],
  126. },
  127. ],
  128. };