echartLine3.vue 3.6 KB

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