lineChartsCommon.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React, { useEffect, useRef, useState } from 'react';
  2. import styles from './lineChartsCommon.less';
  3. import * as echarts from 'echarts';
  4. interface editPros {
  5. width: number;
  6. height: number;
  7. data: any;
  8. }
  9. /**
  10. * 折线图组件
  11. * @constructor
  12. */
  13. const LineChartsCommon: React.FC<editPros> = (props) => {
  14. const chartRef: any = useRef();
  15. const { width, height, data } = props;
  16. const [chartsData, setChartsData] = useState(data);
  17. const options = {
  18. color: ['#1890FF'],
  19. tooltip: {
  20. trigger: 'axis',
  21. axisPointer: {
  22. // 坐标轴指示器,坐标轴触发有效
  23. type: 'shadow', // 默认为直线,可选为:'line' | 'shadow'
  24. },
  25. },
  26. grid: {
  27. top: '15%',
  28. left: '5%',
  29. right: '0',
  30. bottom: '8%',
  31. containLabel: true,
  32. },
  33. xAxis: [
  34. {
  35. type: 'category',
  36. data: [],
  37. boundaryGap: false,
  38. axisTick: {
  39. show: false, // 不显示坐标轴刻度线
  40. },
  41. splitLine: {
  42. show: false,
  43. lineStyle: {
  44. type: 'solid',
  45. color: 'rgba(230, 247, 255, 0.20)',
  46. },
  47. },
  48. axisLine: {
  49. lineStyle: {
  50. color: '#27b4c2',
  51. },
  52. },
  53. //x底部文字
  54. axisLabel: {
  55. interval: 0,
  56. rotate: 30,
  57. },
  58. },
  59. ],
  60. yAxis: [
  61. {
  62. type: 'value',
  63. // y轴的分割线
  64. splitLine: {
  65. show: true,
  66. lineStyle: {
  67. type: 'solid',
  68. color: 'rgba(230, 247, 255, 0.20)',
  69. },
  70. },
  71. },
  72. ],
  73. series: [
  74. {
  75. type: 'line',
  76. smooth: false,
  77. symbol: 'circle', //拐点设置为实心
  78. symbolSize: 2, //拐点大小
  79. itemStyle: {
  80. borderColor: 'rgba(42,157,255,.2)', //拐点边框颜色
  81. borderWidth: 10, //拐点边框大小,
  82. color: '#1890FF',
  83. },
  84. tooltip: {
  85. trigger: 'axis',
  86. axisPointer: {
  87. // 坐标轴指示器,坐标轴触发有效
  88. type: 'line', // 默认为直线,可选为:'line' | 'shadow'
  89. },
  90. },
  91. lineStyle: {
  92. width: 3,
  93. shadowColor: '#1890FF',
  94. shadowBlur: 20,
  95. },
  96. areaStyle: {
  97. opacity: 1,
  98. //右下左上
  99. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  100. {
  101. offset: 0,
  102. color: 'rgba(24, 144, 255, .5)',
  103. },
  104. {
  105. offset: 0.3,
  106. color: 'rgba(24, 144, 255, 0.2)',
  107. },
  108. {
  109. offset: 1,
  110. color: 'rgba(24, 144, 255, 0)',
  111. },
  112. ]),
  113. },
  114. data: [],
  115. },
  116. {
  117. type: 'lines',
  118. coordinateSystem: 'cartesian2d',
  119. symbolSize: 8,
  120. polyline: true, // 多线段
  121. effect: {
  122. show: true,
  123. period: 5,
  124. trailLength: 0.3,
  125. symbolSize: 11,
  126. symbol: 'circle',
  127. color: '#0069d9',
  128. },
  129. lineStyle: {
  130. width: 1,
  131. opacity: 0,
  132. },
  133. // 光点
  134. data: [
  135. {
  136. coords: [],
  137. },
  138. ],
  139. },
  140. ],
  141. };
  142. const setCharts = () => {
  143. const myChart = echarts.init(chartRef.current);
  144. if (chartsData && chartsData.length > 0) {
  145. const list: any = [];
  146. const value: any = [];
  147. const total: any = [];
  148. chartsData.forEach((res: { time: string; value: number }) => {
  149. list.push(res.time);
  150. value.push(res.value);
  151. total.push([res.time, res.value]);
  152. });
  153. options.xAxis[0].data = list;
  154. options.series[0].data = value;
  155. options.series[1].data[0].coords = total;
  156. myChart.setOption(options, true);
  157. }
  158. };
  159. useEffect(() => {
  160. setChartsData(data);
  161. setCharts();
  162. }, [data]);
  163. return (
  164. <div className={styles.container}>
  165. <div ref={chartRef} style={{ width: `${width}px`, height: `${height}px` }} />
  166. </div>
  167. );
  168. };
  169. export default LineChartsCommon;