带区域的双折线

效果

区域双折线.gif

代码

  1. const xData = ['2020-7-01', '2020-7-02', '2020-7-03', '2020-7-04', '2020-7-05', '2020-7-06', '2020-7-07', '2020-7-08', '2020-7-09', '2020-7-10'];
  2. const startValue = '2020-7-03';
  3. const endValue = '2020-7-07';
  4. const yTwoData = ['7', '7', '7', '27', '27', '7', '7', '27', '27', '27'];
  5. const yOneData = ['13', '13', '13', '33', '33', '13', '13', '13', '13', '13'];
  6. option = {
  7. textStyle: {
  8. fontSize: 12,
  9. },
  10. grid: {
  11. top: 150,
  12. right: 0,
  13. bottom: 150,
  14. left: 30,
  15. containLabel: true,
  16. },
  17. dataZoom: [{
  18. type: 'slider',
  19. bottom: 40,
  20. left: 40,
  21. startValue,
  22. endValue,
  23. }, {
  24. type: 'inside',
  25. }],
  26. xAxis: {
  27. data: xData,
  28. axisLine: {
  29. show: false,
  30. },
  31. axisTick: {
  32. show: false,
  33. },
  34. axisLabel: {
  35. color: '#333333',
  36. },
  37. axisPointer: {
  38. type: 'line',
  39. lineStyle: {
  40. color: '#333333',
  41. type: 'dashed',
  42. }
  43. }
  44. },
  45. yAxis: {
  46. min: 0,
  47. max: 40,
  48. offset: 0,
  49. splitLine: {
  50. show: false,
  51. },
  52. axisLine: {
  53. show: false,
  54. },
  55. axisTick: {
  56. show: false,
  57. },
  58. axisLabel: {
  59. show: true,
  60. color: '#333333',
  61. fontSize: 20,
  62. lineHeight: 18,
  63. formatter: (value, index) => {
  64. switch (index) {
  65. case 1:
  66. return '异\n\n常';
  67. case 3:
  68. return '正\n\n常';
  69. default:
  70. return '';
  71. }
  72. },
  73. },
  74. },
  75. tooltip: {
  76. show: true,
  77. trigger: 'axis',
  78. backgroundColor: '#ffffff',
  79. borderWidth: 1,
  80. borderColor: '#333333',
  81. textStyle: {
  82. fontSize: 12,
  83. color: '#333333',
  84. },
  85. },
  86. series: [{
  87. name: '正常背景',
  88. type: 'line',
  89. z: 1,
  90. markArea: {
  91. silent: true,
  92. itemStyle: {
  93. color: '#DDFAE4',
  94. },
  95. data: [
  96. [{
  97. x: 40,
  98. yAxis: 21
  99. }, {
  100. x: '100%',
  101. yAxis: 39
  102. }]
  103. ],
  104. }
  105. },
  106. {
  107. name: '异常背景',
  108. type: 'line',
  109. z: 1,
  110. markArea: {
  111. silent: true,
  112. itemStyle: {
  113. color: '#FFE9E9',
  114. },
  115. data: [
  116. [{
  117. x: 40,
  118. yAxis: 1,
  119. }, {
  120. x: '100%',
  121. yAxis: 19
  122. }]
  123. ],
  124. }
  125. },
  126. {
  127. name: '双折线-上',
  128. type: 'line',
  129. z: 2,
  130. data: yOneData,
  131. symbol: 'circle',
  132. symbolSize: 4,
  133. itemStyle: {
  134. color: '#FF4D4F',
  135. },
  136. lineStyle: {
  137. type: 'dashed',
  138. width: 2,
  139. color: '#FF4D4F'
  140. },
  141. },
  142. {
  143. name: '双折线-下',
  144. type: 'line',
  145. z: 2,
  146. data: yTwoData,
  147. symbol: 'circle',
  148. symbolSize: 4,
  149. itemStyle: {
  150. color: '#40A9FF',
  151. },
  152. lineStyle: {
  153. type: 'dashed',
  154. width: 2,
  155. color: '#40A9FF',
  156. },
  157. }
  158. ]
  159. };

中间截断式折线图

效果

中间截断折线图.gif

代码

  1. import React, { useEffect, useRef } from 'react';
  2. import * as echarts from 'echarts';
  3. const xData = Array.from({ length: 31 }, () => 0).map((_, index) => `08-${index < 9 ? `0${index + 1}` : index + 1}`);
  4. const yData = Array.from({ length: 31 }, () => Math.ceil(Math.random() * 100 + 10));
  5. const option = {
  6. // grid: {
  7. // top: 30,
  8. // right: 60,
  9. // bottom: 30,
  10. // left: 60,
  11. // },
  12. xAxis: {
  13. type: 'category',
  14. data: xData,
  15. },
  16. yAxis: {
  17. type: 'value',
  18. },
  19. legend: {
  20. show: true,
  21. right: 0,
  22. },
  23. tooltip: {
  24. trigger: 'axis',
  25. axisPointer: {
  26. type: 'shadow',
  27. },
  28. },
  29. series: [
  30. {
  31. name: '晨检人次',
  32. // 设置 undefined 则不显示
  33. data: yData.map((item, index) => (index < 14 ? item : undefined)),
  34. type: 'line',
  35. },
  36. ],
  37. };
  38. const Charts = () => {
  39. const chartRef = useRef();
  40. const myChart = useRef();
  41. const initChart = () => {
  42. if (!myChart.current && chartRef?.current) {
  43. myChart.current = echarts.init(chartRef.current);
  44. myChart.current.setOption(option);
  45. }
  46. };
  47. useEffect(() => {
  48. initChart();
  49. }, []);
  50. return <div style={{ width: 800, height: 300 }} ref={chartRef} />;
  51. };
  52. export default Charts;