index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div class="container-echart">
  3. <div :id="'echart' + timeId" class="interface-echart"></div>
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import * as echarts from 'echarts'
  8. import { EChartsType } from 'echarts'
  9. import { onMounted, ref, watch, onBeforeUnmount, nextTick } from 'vue'
  10. const props = defineProps({
  11. NamechartData: {
  12. // 柱形图x轴数据
  13. type: Array,
  14. default: true
  15. },
  16. ArrOneData: {
  17. // 柱形图x轴数据
  18. type: Array,
  19. default: true
  20. },
  21. ArrTwoData: {
  22. // 柱形图x轴数据
  23. type: Array,
  24. default: true
  25. }
  26. })
  27. console.log(props);
  28. let myEchart: EChartsType
  29. const timeId = ref(Math.floor(new Date().getTime() * Math.random())); // 使该图表保持唯id
  30. const myEchartData = () => {
  31. const option = {
  32. legend: {
  33. //图标信息提示
  34. type: 'scroll',
  35. left: '40%',
  36. top: 0,
  37. align: 'auto',
  38. icon: 'circle',
  39. textStyle: {
  40. color: "white"
  41. }
  42. },
  43. grid: {
  44. //图表距离边框的偏离
  45. right: '8%', //图表距离容器右侧距离
  46. left: '12%',
  47. top: '20%',
  48. bottom: '20%'
  49. },
  50. tooltip: { trigger: 'axis' }, // 设置图案和容器的距离
  51. xAxis: {
  52. type: 'category',
  53. nameLocation: 'end',
  54. data: props.NamechartData
  55. },
  56. yAxis: {
  57. type: 'value',
  58. nameLocation: 'end',
  59. // 坐标轴轴线
  60. axisLine: {
  61. show: false
  62. },
  63. // 坐标轴刻度
  64. axisTick: {
  65. show: false
  66. },
  67. min: 0,
  68. splitLine: {
  69. show: false // 不显示网格线
  70. }
  71. // axisLabel: { formatter: '{value} ' },
  72. },
  73. series: [
  74. {
  75. name: (new Date().getFullYear()-1).toString(),
  76. type: 'bar',
  77. data: props.ArrOneData
  78. },
  79. {
  80. name: (new Date().getFullYear()).toString(),
  81. type: 'bar',
  82. data: props.ArrTwoData
  83. }
  84. ],
  85. dataZoom: [
  86. // 数据选择范围 最下面的范围拉条
  87. {
  88. type: 'slider', // 开启滑动条
  89. show: true, // 显示缩放条
  90. start: 0,
  91. end: 100,
  92. backgroundColor: '#26CAF026',
  93. selectedDataBackground: {
  94. lineStyle: {
  95. type: 'dotted'
  96. },
  97. areaStyle: {
  98. color: '#26CAF0'
  99. }
  100. },
  101. bottom: '3%',
  102. height: 14,
  103. brushSelect: false,
  104. }
  105. ]
  106. }
  107. // { notMerge: true } 解决删除数据时,数据不刷新的问题
  108. myEchart.setOption(option, { notMerge: true })
  109. }
  110. watch(
  111. //监控数据变化
  112. () => props.NamechartData,
  113. () => {
  114. setTimeout(() => {
  115. myEchartData()
  116. }, 500)
  117. },
  118. { deep: true }
  119. )
  120. watch(
  121. //监控数据变化
  122. () => props.ArrOneData,
  123. () => {
  124. setTimeout(() => {
  125. myEchartData()
  126. }, 500)
  127. },
  128. { deep: true }
  129. )
  130. watch(
  131. //监控数据变化
  132. () => props.ArrTwoData,
  133. () => {
  134. setTimeout(() => {
  135. myEchartData()
  136. }, 500)
  137. },
  138. { deep: true }
  139. )
  140. onMounted(() => {
  141. setTimeout(() => {
  142. const dom = document.getElementById(`echart${timeId.value}`) as any
  143. myEchart = echarts.init(dom)
  144. myEchartData()
  145. }, 500)
  146. // 当窗口发生变化时
  147. window.addEventListener('resize', () => {
  148. myEchart.resize()
  149. })
  150. })
  151. onBeforeUnmount(() => {
  152. window.removeEventListener('resize', () => {
  153. myEchart.resize()
  154. })
  155. })
  156. </script>
  157. <style scoped lang="scss">
  158. .container-echart {
  159. width: 100%;
  160. height: 100%;
  161. display: flex;
  162. justify-content: center;
  163. align-items: center;
  164. overflow: hidden;
  165. }
  166. .interface-echart {
  167. width: 100%;
  168. height: 100%;
  169. }
  170. </style>