waterChart.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts2"
  4. class="echarts"></view>
  5. </view>
  6. </template>
  7. <script>
  8. var dwtype = "2";
  9. var tes = '0';
  10. var tes2 = '';
  11. var tyss = "";
  12. if (dwtype == 2) {
  13. tes = '一周设备数值变化';
  14. tes2 = '水压值';
  15. tyss = "兆帕";
  16. }
  17. if (dwtype == 5) {
  18. tes = '一周水位值变化 (m)';
  19. tes2 = '水位值';
  20. tyss = "米";
  21. document.getElementById('dev_title_lab').innerHTML = '当前水位:';
  22. document.getElementById('dev_title_unit').innerHTML = 'm';
  23. }
  24. export default {
  25. props: {
  26. bindData: {
  27. type: Object,
  28. default: ''
  29. }
  30. },
  31. data() {
  32. return {
  33. option: {
  34. title: {
  35. x: 'center',
  36. text: tes
  37. },
  38. tooltip: {
  39. trigger: 'axis',
  40. axisPointer: {
  41. type: 'cross',
  42. animation: false,
  43. label: {
  44. backgroundColor: '#505765'
  45. }
  46. }
  47. },
  48. grid: {
  49. left: '7%',
  50. right: '4%',
  51. bottom: '3%',
  52. containLabel: true
  53. },
  54. toolbox: {
  55. // feature: {
  56. // saveAsImage: {}
  57. // }
  58. },
  59. legend: {
  60. data: ['水压/水位(兆帕/米)', '电量/信号(%)'],
  61. left: 10
  62. },
  63. xAxis: {
  64. type: 'category',
  65. axisTick: {
  66. alignWithLabel: true
  67. },
  68. data: this.bindData.stiem
  69. },
  70. yAxis: [{
  71. name: '水压/水位',
  72. type: 'value',
  73. max: 1
  74. }, {
  75. name: '电量/信号',
  76. nameLocation: 'start',
  77. max: 100,
  78. type: 'value',
  79. // inverse: true
  80. }],
  81. dataZoom: [{
  82. type: 'slider',
  83. xAxisIndex: 0,
  84. filterMode: 'empty',
  85. start: 0,
  86. end: 100
  87. }, {
  88. type: 'inside',
  89. xAxisIndex: 0,
  90. filterMode: 'empty',
  91. start: 0,
  92. end: 100
  93. }],
  94. series: [{
  95. name: tes2,
  96. type: 'line',
  97. stack: 'Pa',
  98. animation: false,
  99. data: this.bindData.water_data,
  100. color: ['#2699fb'],
  101. yAxisIndex: 0
  102. }, {
  103. name: "信号强度",
  104. type: 'line',
  105. stack: '%',
  106. animation: false,
  107. data: this.bindData.signal_data,
  108. color: ['#ffff00'],
  109. yAxisIndex: 1
  110. }, {
  111. name: "电池电量",
  112. type: 'line',
  113. stack: 'a',
  114. animation: false,
  115. data: this.bindData.battery_data,
  116. color: ['#ff0000'],
  117. yAxisIndex: 1
  118. }]
  119. }
  120. }
  121. },
  122. onLoad() {
  123. },
  124. methods: {
  125. changeOption() {
  126. const data = this.option.series[0].data
  127. // 随机更新示例数据
  128. data.forEach((item, index) => {
  129. data.splice(index, 1, Math.random() * 40)
  130. })
  131. },
  132. onViewClick(options) {
  133. console.log(options)
  134. }
  135. }
  136. }
  137. </script>
  138. <script module="echarts" lang="renderjs">
  139. let myChart
  140. export default {
  141. mounted() {
  142. if (typeof window.echarts === 'function') {
  143. this.initEcharts()
  144. } else {
  145. // 动态引入较大类库避免影响页面展示
  146. const script = document.createElement('script')
  147. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  148. script.src = 'static/echarts.min.js'
  149. script.onload = this.initEcharts.bind(this)
  150. document.head.appendChild(script)
  151. }
  152. },
  153. methods: {
  154. initEcharts() {
  155. myChart = echarts.init(document.getElementById('echarts2'))
  156. // 观测更新的数据在 view 层可以直接访问到
  157. myChart.setOption(this.option)
  158. },
  159. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  160. // 监听 service 层数据变更
  161. myChart.setOption(newValue)
  162. },
  163. onClick(event, ownerInstance) {
  164. // 调用 service 层的方法
  165. ownerInstance.callMethod('onViewClick', {
  166. test: 'test'
  167. })
  168. }
  169. }
  170. }
  171. </script>
  172. <style>
  173. .content {
  174. display: flex;
  175. flex-direction: column;
  176. align-items: center;
  177. justify-content: center;
  178. }
  179. .echarts {
  180. width: 100%;
  181. height: 500rpx;
  182. margin-bottom: 70rpx;
  183. }
  184. </style>