lineChart.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import React, { useEffect, useRef } from 'react';
  2. import * as echarts from 'echarts';
  3. interface propsData {
  4. xData: string[];
  5. yData: string[];
  6. color: string;
  7. width: string;
  8. height: string;
  9. }
  10. const LineChart: React.FC<propsData> = (props) => {
  11. const chartRef: any = useRef();
  12. const { xData, yData, color, width, height } = props;
  13. // 初始化
  14. useEffect(() => {
  15. const chart = echarts.init(chartRef.current);
  16. const options = {
  17. legend: {
  18. show: false,
  19. },
  20. tooltip: {
  21. trigger: 'axis',
  22. },
  23. grid: {
  24. left: '3%',
  25. right: '3%',
  26. bottom: '3%',
  27. top: '5%',
  28. containLabel: true,
  29. },
  30. xAxis: [
  31. {
  32. type: 'category',
  33. data: xData,
  34. axisLine: {
  35. lineStyle: {
  36. color: '#ddd',
  37. },
  38. },
  39. axisTick: {
  40. show: false,
  41. },
  42. axisLabel: {
  43. rotate: 45,
  44. interval: 0,
  45. textStyle: {
  46. color: '#333',
  47. },
  48. margin: 15,
  49. },
  50. boundaryGap: false,
  51. },
  52. ],
  53. yAxis: [
  54. {
  55. type: 'value',
  56. axisTick: {
  57. show: false,
  58. },
  59. axisLine: {
  60. lineStyle: {
  61. color: '#ddd',
  62. },
  63. },
  64. axisLabel: {
  65. textStyle: {
  66. color: '#333',
  67. },
  68. },
  69. splitLine: {
  70. show: false,
  71. },
  72. },
  73. ],
  74. series: [
  75. {
  76. name: '',
  77. type: 'line',
  78. data: yData,
  79. symbolSize: 6,
  80. symbol: 'circle',
  81. smooth: true,
  82. lineStyle: {
  83. color: color,
  84. },
  85. itemStyle: {
  86. normal: {
  87. color: color,
  88. borderColor: color,
  89. },
  90. },
  91. areaStyle: {
  92. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  93. {
  94. offset: 0,
  95. color: color + 'b3',
  96. },
  97. {
  98. offset: 1,
  99. color: color + '03',
  100. },
  101. ]),
  102. },
  103. emphasis: {
  104. itemStyle: {
  105. color: {
  106. type: 'radial',
  107. x: 0.5,
  108. y: 0.5,
  109. r: 0.5,
  110. colorStops: [
  111. {
  112. offset: 0,
  113. color: color,
  114. },
  115. {
  116. offset: 1,
  117. color: '#fff',
  118. },
  119. ],
  120. },
  121. borderColor: color,
  122. borderWidth: 2,
  123. },
  124. },
  125. },
  126. ],
  127. };
  128. chart.setOption(options);
  129. }, []);
  130. return <div style={{ width: width, height: height }} ref={chartRef} />;
  131. };
  132. export default LineChart;