chart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div ref="chart" class="chart" :style="{height: '400px', width: '100%'}" />
  3. </template>
  4. <script>
  5. import { listWaterLevel } from '@/api/analysis/achievement'
  6. import * as echarts from "echarts";
  7. require('echarts/theme/macarons') // echarts theme
  8. import resize from '@/utils/resize'
  9. export default {
  10. mixins: [resize],
  11. props: {
  12. queryParams: Object,
  13. },
  14. data() {
  15. return {
  16. pagination: {
  17. page: 1,
  18. size: 10000,
  19. }
  20. }
  21. },
  22. mounted() {
  23. this.$nextTick(() => {
  24. this.chart = echarts.init(this.$refs.chart, 'macarons');
  25. this.setOptions();
  26. })
  27. },
  28. beforeDestroy() {
  29. if (!this.chart) {
  30. return
  31. }
  32. this.chart.dispose()
  33. this.chart = null
  34. },
  35. methods: {
  36. setOptions() {
  37. const { queryParams } = this;
  38. if (!queryParams.siteId) return;
  39. const params = {
  40. ...this.queryParams,
  41. ...this.pagination,
  42. };
  43. listWaterLevel(params).then((res) => {
  44. const chartData = res.data.records || [];
  45. chartData.reverse()
  46. const xAxisData = chartData.map(({ time }) => time);
  47. const seriesData = chartData.map(({ avgWaterlevel }) => avgWaterlevel);
  48. this.chart.setOption({
  49. xAxis: {
  50. name: '时间',
  51. boundaryGap: false,
  52. data: xAxisData,
  53. type: 'category',
  54. axisLine: {
  55. lineStyle: {
  56. color: '#54606C'
  57. }
  58. },
  59. },
  60. yAxis: [
  61. {
  62. name: '水位(m)',
  63. axisLine: {
  64. lineStyle: {
  65. color: '#54606C'
  66. }
  67. },
  68. min: 'dataMin',
  69. max: 'dataMax',
  70. },
  71. {
  72. axisLine: {
  73. show: true,
  74. lineStyle: {
  75. color: '#FF8500'
  76. }
  77. },
  78. axisTick: {
  79. show: false,
  80. },
  81. axisLabel: {
  82. show: false,
  83. },
  84. },
  85. ],
  86. grid: {
  87. left: 50,
  88. right: 50,
  89. bottom: 40,
  90. top: 40,
  91. containLabel: true
  92. },
  93. tooltip: {
  94. trigger: 'axis',
  95. axisPointer: {
  96. type: 'cross'
  97. },
  98. padding: [5, 10]
  99. },
  100. series: [{
  101. name: '水位',
  102. itemStyle: {
  103. normal: {
  104. color: '#FF8500',
  105. lineStyle: {
  106. color: '#FF8500',
  107. width: 2
  108. }
  109. }
  110. },
  111. smooth: false,
  112. type: 'line',
  113. data: seriesData,
  114. animations: false,
  115. }]
  116. })
  117. })
  118. }
  119. },
  120. watch: {
  121. queryParams: {
  122. handler() {
  123. this.setOptions();
  124. },
  125. deep: true,
  126. },
  127. }
  128. }
  129. </script>
  130. <style scoped>
  131. </style>