lineChartsCommon.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. normal: {
  81. borderColor: 'rgba(42,157,255,.2)', //拐点边框颜色
  82. borderWidth: 10, //拐点边框大小,
  83. color: '#1890FF',
  84. },
  85. },
  86. tooltip: {
  87. trigger: 'axis',
  88. axisPointer: {
  89. // 坐标轴指示器,坐标轴触发有效
  90. type: 'line', // 默认为直线,可选为:'line' | 'shadow'
  91. },
  92. },
  93. lineStyle: {
  94. normal: {
  95. width: 3,
  96. shadowColor: '#1890FF',
  97. shadowBlur: 20,
  98. },
  99. },
  100. areaStyle: {
  101. opacity: 1,
  102. //右下左上
  103. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  104. {
  105. offset: 0,
  106. color: 'rgba(24, 144, 255, .5)',
  107. },
  108. {
  109. offset: 0.3,
  110. color: 'rgba(24, 144, 255, 0.2)',
  111. },
  112. {
  113. offset: 1,
  114. color: 'rgba(24, 144, 255, 0)',
  115. },
  116. ]),
  117. },
  118. data: [],
  119. },
  120. {
  121. type: 'lines',
  122. coordinateSystem: 'cartesian2d',
  123. symbolSize: 8,
  124. polyline: true, // 多线段
  125. effect: {
  126. show: true,
  127. period: 5,
  128. trailLength: 0.3,
  129. symbolSize: 11,
  130. symbol: 'circle',
  131. color: '#0069d9',
  132. },
  133. lineStyle: {
  134. normal: {
  135. width: 1,
  136. opacity: 0,
  137. },
  138. },
  139. // 光点
  140. data: [
  141. {
  142. coords: [],
  143. },
  144. ],
  145. },
  146. ],
  147. };
  148. const setCharts = () => {
  149. const myChart = echarts.init(chartRef.current);
  150. if (chartsData && chartsData.length > 0) {
  151. const list: any = [];
  152. const value: any = [];
  153. const total: any = [];
  154. chartsData.forEach((res: { time: string; value: number }) => {
  155. list.push(res.time);
  156. value.push(res.value);
  157. total.push([res.time, res.value]);
  158. });
  159. options.xAxis[0].data = list;
  160. options.series[0].data = value;
  161. options.series[1].data[0].coords = total;
  162. myChart.setOption(options, true);
  163. }
  164. };
  165. useEffect(() => {
  166. setChartsData(data);
  167. setCharts();
  168. }, [data]);
  169. return (
  170. <div className={styles.container}>
  171. <div ref={chartRef} style={{ width: `${width}vw`, height: `${height}vh` }} />
  172. </div>
  173. );
  174. };
  175. export default LineChartsCommon;