echartLine3.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div ref="work" class="work-box"></div>
  3. </template>
  4. <script lang="ts" setup>
  5. import * as echarts from 'echarts';
  6. import { ref, nextTick, watch, defineProps } from 'vue';
  7. // well i know this is trash but the time is reaching
  8. let props = defineProps<{
  9. xData: string[];
  10. y1Data: number[];
  11. y2Data: number[];
  12. y3Data: number[];
  13. y4Data: number[];
  14. yUnit?: string;
  15. legendName?: string;
  16. }>();
  17. //获取dom元素节点
  18. let work = ref<any>();
  19. watch(
  20. () => props.xData,
  21. () => {
  22. getOption();
  23. },
  24. { immediate: true, deep: true }
  25. );
  26. function getOption() {
  27. nextTick(() => {
  28. const myChart = echarts.init(work.value);
  29. let option = {
  30. tooltip: {
  31. trigger: 'axis',
  32. backgroundColor: 'rgba(0, 0, 0, .6)',
  33. textStyle: {
  34. color: '#fff',
  35. fontSize: 12,
  36. },
  37. },
  38. grid: {
  39. top: '15%',
  40. left: '11%',
  41. bottom: '10%',
  42. right: '4%',
  43. },
  44. legend: {
  45. align: 'left',
  46. top: '0%',
  47. type: 'plain',
  48. textStyle: {
  49. color: '#7ec7ff',
  50. fontSize: 14,
  51. },
  52. itemGap: 25,
  53. itemWidth: 18,
  54. icon: 'path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z',
  55. },
  56. xAxis: [
  57. {
  58. type: 'category',
  59. axisLabel: {
  60. textStyle: {
  61. color: '#b3b8cc',
  62. },
  63. },
  64. // axisLine: {
  65. // lineStyle: {
  66. // color: '#244a94',
  67. // },
  68. // },
  69. splitLine: {
  70. show: false,
  71. },
  72. axisTick: {
  73. show: false,
  74. },
  75. data: props.xData,
  76. },
  77. ],
  78. yAxis: [
  79. {
  80. // boundaryGap: false,
  81. axisLabel: {
  82. textStyle: {
  83. color: '#b3b8cc',
  84. },
  85. },
  86. name: props.yUnit,
  87. // nameTextStyle: {
  88. // color: '#fff',
  89. // fontSize: 12,
  90. // lineHeight: 10,
  91. // },
  92. splitLine: {
  93. lineStyle: {
  94. color: '#0d2973',
  95. },
  96. },
  97. axisTick: {
  98. show: false,
  99. },
  100. },
  101. ],
  102. series: [
  103. {
  104. name: props.legendName + '实时值',
  105. type: 'line',
  106. smooth: true,
  107. yAxisIndex: 0,
  108. symbol: 'none',
  109. lineStyle: {
  110. normal: {
  111. width: 2,
  112. },
  113. },
  114. data: props.y4Data,
  115. },
  116. {
  117. name: props.legendName + '预测值',
  118. type: 'line',
  119. smooth: true,
  120. yAxisIndex: 0,
  121. symbol: 'none',
  122. lineStyle: {
  123. normal: {
  124. width: 2,
  125. },
  126. },
  127. data: props.y1Data,
  128. },
  129. {
  130. name: props.legendName + '最大值',
  131. type: 'line',
  132. smooth: true,
  133. symbol: 'none',
  134. yAxisIndex: 0,
  135. lineStyle: {
  136. normal: {
  137. width: 2,
  138. },
  139. },
  140. data: props.y2Data,
  141. },
  142. {
  143. name: props.legendName + '最小值',
  144. type: 'line',
  145. smooth: true,
  146. yAxisIndex: 0,
  147. symbol: 'none',
  148. lineStyle: {
  149. normal: {
  150. width: 2,
  151. },
  152. },
  153. data: props.y3Data,
  154. },
  155. ],
  156. };
  157. myChart.setOption(option);
  158. window.onresize = function () {
  159. myChart.resize();
  160. };
  161. });
  162. }
  163. </script>
  164. <style scoped lang="less">
  165. .work-box {
  166. width: 100%;
  167. height: 100%;
  168. }
  169. </style>