sjydlEcharts.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. message: {
  12. type: Array,
  13. default: true
  14. }
  15. })
  16. let xAxisData: string[] = [];
  17. let data1: number[] = [];
  18. let data2: number[] = [];
  19. let data3: number[] = [];
  20. let data4: number[] = [];
  21. for (let i = 0; i < 10; i++) {
  22. xAxisData.push('201' + i);
  23. data1.push(+(Math.random() * 2).toFixed(2));
  24. data2.push(+(Math.random() * 5).toFixed(2));
  25. data3.push(+(Math.random() + 0.3).toFixed(2));
  26. data4.push(+Math.random().toFixed(2));
  27. }
  28. var emphasisStyle = {
  29. itemStyle: {
  30. shadowBlur: 10,
  31. shadowColor: 'rgba(0,0,0,0.3)'
  32. }
  33. };
  34. let myEchart: EChartsType
  35. const timeId = ref(Math.floor(new Date().getTime() * Math.random())); // 使该图表保持唯id
  36. const myEchartData = () => {
  37. const option = {
  38. tooltip: {
  39. trigger: 'axis',
  40. axisPointer: {
  41. type: 'cross',
  42. label: {
  43. backgroundColor: '#6a7985'
  44. }
  45. }
  46. },
  47. legend: {
  48. data: ['省总用电量'],
  49. textStyle: {
  50. color: 'white' // 设置字体颜色为红色
  51. }
  52. },
  53. toolbox: {
  54. feature: {
  55. saveAsImage: {}
  56. }
  57. },
  58. grid: {
  59. left: '3%',
  60. right: '4%',
  61. bottom: '3%',
  62. containLabel: true
  63. },
  64. xAxis: [
  65. {
  66. type: 'category',
  67. boundaryGap: false,
  68. data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  69. }
  70. ],
  71. yAxis: [
  72. {
  73. type: 'value'
  74. }
  75. ],
  76. series: [
  77. {
  78. name: '省总用电量',
  79. type: 'bar',
  80. stack: 'Total',
  81. areaStyle: {},
  82. emphasis: {
  83. focus: 'series'
  84. },
  85. tooltip: {
  86. valueFormatter: function (value) {
  87. return value + ' Kwh';
  88. }
  89. },
  90. data: [20, 10, 40, 20, 10, 50, 20, 30, 40, 10, 20, 40]
  91. }
  92. ]
  93. };
  94. // { notMerge: true } 解决删除数据时,数据不刷新的问题
  95. myEchart.setOption(option, { notMerge: true })
  96. }
  97. watch(
  98. //监控数据变化
  99. () => props.message,
  100. () => {
  101. setTimeout(() => {
  102. myEchartData()
  103. }, 500)
  104. },
  105. { deep: true }
  106. )
  107. onMounted(() => {
  108. setTimeout(() => {
  109. const dom = document.getElementById(`echart${timeId.value}`) as any
  110. myEchart = echarts.init(dom)
  111. myEchartData()
  112. }, 500)
  113. // 当窗口发生变化时
  114. window.addEventListener('resize', () => {
  115. myEchart.resize()
  116. })
  117. })
  118. onBeforeUnmount(() => {
  119. window.removeEventListener('resize', () => {
  120. myEchart.resize()
  121. })
  122. })
  123. </script>
  124. <style scoped lang="scss">
  125. .container-echart {
  126. width: 100%;
  127. height: 100%;
  128. display: flex;
  129. justify-content: center;
  130. align-items: center;
  131. overflow: hidden;
  132. }
  133. .interface-echart {
  134. width: 100%;
  135. height: 100%;
  136. }
  137. </style>