BarChartModule.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. //y轴显示数据
  3. Data:{
  4. name:string,
  5. data:string[],
  6. }
  7. props:{
  8. xData:string[], //x轴时间数据
  9. dataList:Data[], //数据列表
  10. }
  11. */
  12. import { useEffect, useRef, useState } from 'react';
  13. import echarts from 'echarts';
  14. import styles from './index.less';
  15. import moment from 'moment';
  16. import { Empty } from 'antd';
  17. //图表模块
  18. const BarChartModule = props => {
  19. const chartDomRef = useRef();
  20. const chartRef = useRef();
  21. const { xData, dataList } = props;
  22. useEffect(() => {
  23. chartRef.current = echarts.init(chartDomRef.current);
  24. window.addEventListener('resize', resetChart);
  25. return () => window.removeEventListener('resize', resetChart);
  26. }, []);
  27. useEffect(() => {
  28. if (!chartRef.current || !dataList) return;
  29. const option = { ...defaultOption };
  30. option.xAxis.data = xData;
  31. option.series = dataList.map((item, idx) => {
  32. return {
  33. ...option.series[idx],
  34. ...item,
  35. };
  36. });
  37. chartRef.current.clear();
  38. chartRef.current.setOption(option);
  39. chartRef.current.resize();
  40. }, [xData, dataList]);
  41. const resetChart = () => {
  42. if (chartRef.current) chartRef.current.resize();
  43. };
  44. const getStyle = () => {
  45. if (dataList && dataList.length != 0) return { width: '100%', height: '100%' };
  46. else return { width: '100%', height: '100%', display: 'none' };
  47. };
  48. return (
  49. <div className={styles.content}>
  50. <div style={getStyle()} ref={chartDomRef} />
  51. {(!dataList || dataList.length == 0) && <Empty />}
  52. </div>
  53. );
  54. };
  55. export default BarChartModule;
  56. const colors = [
  57. '#5470c6',
  58. '#91cc75',
  59. '#fac858',
  60. '#ee6666',
  61. '#73c0de',
  62. '#3ba272',
  63. '#fc8452',
  64. '#9a60b4',
  65. '#ea7ccc',
  66. ];
  67. const defaultOption = {
  68. color: colors,
  69. grid: {
  70. bottom: 30,
  71. left: 60,
  72. right: 30,
  73. },
  74. xAxis: {
  75. type: 'category',
  76. axisTick: { show: false },
  77. axisLine: {
  78. lineStyle: {
  79. color: '#c9d2d2',
  80. },
  81. },
  82. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  83. },
  84. yAxis: {
  85. type: 'value',
  86. splitLine: {
  87. lineStyle: {
  88. type: 'dashed',
  89. },
  90. },
  91. axisTick: { show: false },
  92. axisLine: {
  93. show: false,
  94. },
  95. axisLabel: {
  96. color: '#000',
  97. },
  98. },
  99. legend: {
  100. // icon: 'circle',
  101. left: '20%',
  102. textStyle: {
  103. color: '#000',
  104. },
  105. },
  106. series: [
  107. {
  108. name: '2022',
  109. data: [120, 200, 150, 80, 70, 110, 130],
  110. type: 'bar',
  111. label: {
  112. show: true,
  113. position: 'top',
  114. color: '#000',
  115. },
  116. barGap: '0',
  117. barMaxWidth: '10%',
  118. },
  119. {
  120. name: '2023',
  121. data: [120, 200, 150, 80, 70, 110, 130],
  122. type: 'bar',
  123. label: {
  124. show: true,
  125. position: 'top',
  126. color: '#000',
  127. },
  128. barGap: '0',
  129. barMaxWidth: '10%',
  130. },
  131. ],
  132. };