waterChart.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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: '15%',
  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.map( function(value,index){
  69. return `${value.substring(0,10)}\n${value.substring(11,20)}`
  70. })
  71. },
  72. yAxis: [{
  73. name: '水压/水位',
  74. type: 'value',
  75. max: 1
  76. }, {
  77. name: '电量/信号',
  78. // nameLocation: 'start',
  79. max: 100,
  80. type: 'value',
  81. // inverse: true
  82. }],
  83. // dataZoom: [{
  84. // type: 'slider',
  85. // xAxisIndex: 0,
  86. // filterMode: 'empty',
  87. // start: 0,
  88. // end: 100
  89. // }, {
  90. // type: 'inside',
  91. // xAxisIndex: 0,
  92. // filterMode: 'empty',
  93. // start: 0,
  94. // end: 100
  95. // }],
  96. series: [{
  97. name: tes2,
  98. type: 'line',
  99. stack: 'Pa',
  100. animation: false,
  101. data: this.bindData.water_data,
  102. color: ['#2699fb'],
  103. yAxisIndex: 0
  104. }, {
  105. name: "信号强度",
  106. type: 'line',
  107. stack: '%',
  108. animation: false,
  109. data: this.bindData.signal_data,
  110. color: ['#ffff00'],
  111. yAxisIndex: 1
  112. }, {
  113. name: "电池电量",
  114. type: 'line',
  115. stack: 'a',
  116. animation: false,
  117. data: this.bindData.battery_data,
  118. color: ['#ff0000'],
  119. yAxisIndex: 1
  120. }]
  121. }
  122. }
  123. },
  124. onLoad() {
  125. },
  126. methods: {
  127. changeOption() {
  128. const data = this.option.series[0].data
  129. // 随机更新示例数据
  130. data.forEach((item, index) => {
  131. data.splice(index, 1, Math.random() * 40)
  132. })
  133. },
  134. onViewClick(options) {
  135. console.log(options)
  136. }
  137. }
  138. }
  139. </script>
  140. <script module="echarts" lang="renderjs">
  141. let myChart
  142. export default {
  143. mounted() {
  144. console.log('this.bindData')
  145. console.log(this.bindData)
  146. if (typeof window.echarts === 'function') {
  147. this.initEcharts()
  148. } else {
  149. // 动态引入较大类库避免影响页面展示
  150. const script = document.createElement('script')
  151. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  152. script.src = 'static/echarts.min.js'
  153. script.onload = this.initEcharts.bind(this)
  154. document.head.appendChild(script)
  155. }
  156. },
  157. methods: {
  158. initEcharts() {
  159. myChart = echarts.init(document.getElementById('echarts2'))
  160. // 观测更新的数据在 view 层可以直接访问到
  161. myChart.setOption(this.option)
  162. },
  163. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  164. // 监听 service 层数据变更
  165. myChart.setOption(newValue)
  166. },
  167. onClick(event, ownerInstance) {
  168. // 调用 service 层的方法
  169. ownerInstance.callMethod('onViewClick', {
  170. test: 'test'
  171. })
  172. }
  173. }
  174. }
  175. </script>
  176. <style>
  177. .content {
  178. display: flex;
  179. flex-direction: column;
  180. align-items: center;
  181. justify-content: center;
  182. }
  183. .echarts {
  184. width: 100%;
  185. height: 500rpx;
  186. margin-bottom: 70rpx;
  187. }
  188. </style>