chart.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div ref="chart" class="chart" :style="{height: '400px', width: '100%'}" />
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. require('echarts/theme/macarons') // echarts theme
  7. import resize from '@/utils/resize'
  8. export default {
  9. mixins: [resize],
  10. props: {
  11. plain: Object,
  12. },
  13. mounted() {
  14. this.$nextTick(() => {
  15. this.chart = echarts.init(this.$refs.chart, 'macarons');
  16. })
  17. },
  18. beforeDestroy() {
  19. if (!this.chart) {
  20. return
  21. }
  22. this.chart.dispose()
  23. this.chart = null
  24. },
  25. watch: {
  26. plain: {
  27. handler(value) {
  28. this.setOptions(value)
  29. },
  30. deep: true,
  31. immediate: true,
  32. }
  33. },
  34. methods: {
  35. setOptions(plain) {
  36. if (!this.chart) {
  37. return;
  38. }
  39. const { sections, wlevelmax, wlevelmin, wlevel } = plain;
  40. const xAxisData = sections.map(({ x }) => x);
  41. const seriesData = sections.map(({ y }) => y);
  42. const options = {
  43. xAxis: {
  44. name: '起点距',
  45. boundaryGap: false,
  46. data: xAxisData,
  47. type: 'category',
  48. axisLine: {
  49. lineStyle: {
  50. color: '#FF8500'
  51. }
  52. },
  53. axisTick: {
  54. lineStyle: {
  55. color: '#8D99A4'
  56. }
  57. },
  58. axisLabel: {
  59. color: '#54606C'
  60. },
  61. nameTextStyle: {
  62. color: '#54606C'
  63. }
  64. },
  65. yAxis: [
  66. {
  67. name: '高程(m)',
  68. triggerEvent: true,
  69. axisLine: {
  70. show: true,
  71. lineStyle: {
  72. color: '#FF8500'
  73. }
  74. },
  75. axisTick: {
  76. show: true,
  77. lineStyle: {
  78. color: '#8D99A4'
  79. }
  80. },
  81. axisLabel: {
  82. color: '#54606C'
  83. },
  84. nameTextStyle: {
  85. color: '#54606C'
  86. },
  87. min: function (value) {
  88. return Math.round(value.min - (value.max - value.min) * 0.1);
  89. },
  90. max: function (value) {
  91. return Math.round(value.max + (value.max - value.min) * 0.1);
  92. },
  93. },
  94. {
  95. axisLine: {
  96. show: true,
  97. lineStyle: {
  98. color: '#FF8500'
  99. }
  100. },
  101. axisTick: {
  102. show: false,
  103. },
  104. axisLabel: {
  105. show: false,
  106. },
  107. },
  108. ],
  109. grid: {
  110. left: 50,
  111. right: 50,
  112. bottom: 40,
  113. top: 40,
  114. containLabel: true
  115. },
  116. tooltip: {
  117. trigger: 'axis',
  118. axisPointer: {
  119. type: 'cross'
  120. },
  121. padding: [5, 10]
  122. },
  123. series: [{
  124. name: '高程',
  125. itemStyle: {
  126. normal: {
  127. color: '#FF8500',
  128. lineStyle: {
  129. color: '#FF8500',
  130. width: 2
  131. }
  132. }
  133. },
  134. areaStyle: {
  135. color: '#FF8500',
  136. opacity: 0.5
  137. },
  138. smooth: false,
  139. symbol: 'none',
  140. type: 'line',
  141. data: seriesData,
  142. animations: false,
  143. markLine: {
  144. symbol: 'none',
  145. silent: true,
  146. lineStyle: { normal: { type: 'dashed' } },
  147. label: { position: 'start' },
  148. data: [
  149. {
  150. yAxis: wlevelmax,
  151. lineStyle: { width: 1, color: 'red' },
  152. name: '最高水位',
  153. label: {
  154. formatter: '{b}'
  155. }
  156. },
  157. {
  158. yAxis: wlevelmin,
  159. lineStyle: { width: 1, color: 'red' },
  160. name: '最低水位',
  161. label: {
  162. formatter: '{b}'
  163. }
  164. },
  165. ]
  166. },
  167. }]
  168. };
  169. if (wlevel > wlevelmin && wlevel < wlevelmax) {
  170. options.series.push({
  171. name: '起测水位',
  172. symbol: 'none',
  173. silent: true,
  174. lineStyle: {
  175. width: 0,
  176. },
  177. data: new Array(seriesData.length).fill(wlevel),
  178. type: 'line',
  179. markLine: {
  180. symbol: 'none',
  181. silent: true,
  182. lineStyle: { normal: { type: 'dashed' } },
  183. label: { position: 'start' },
  184. data: [{
  185. name: '起测水位',
  186. yAxis: wlevel,
  187. label: {
  188. formatter: '{b}'
  189. }
  190. }],
  191. },
  192. });
  193. }
  194. this.chart.setOption(options);
  195. }
  196. }
  197. }
  198. </script>
  199. <style scoped>
  200. </style>