line-echarts.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view>
  3. <view id="echarts" class="echarts">
  4. </view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. echartsData: Object
  11. },
  12. data() {
  13. return {
  14. option: {
  15. padding: [15, 10, 0, 15],
  16. legend: {
  17. type: 'scroll',
  18. pageIconSize: 18,
  19. margin: 20,
  20. itemGap: 20,
  21. lineHeight: 20,
  22. top: 0,
  23. },
  24. grid: {
  25. bottom: '25%',
  26. top: "50px",
  27. left: "45px",
  28. right: "30px"
  29. },
  30. xAxis: {
  31. type: 'category',
  32. data: [],
  33. axisLabel: {
  34. // interval: 60, // 自定义显示X轴坐标显示间隔
  35. textStyle: {
  36. color: '#626262',
  37. fontSize: '10',
  38. },
  39. formatter: (value, index) => {
  40. return value.substring(0, 9);
  41. }
  42. }
  43. },
  44. yAxis: {
  45. type: 'value',
  46. axisPointer: {
  47. snap: true,
  48. show: true,
  49. type: 'line',
  50. lineStyle: {
  51. color: 'rgba(98, 98, 98, 0.7)',
  52. type: 'solid'
  53. }
  54. }
  55. },
  56. series: [],
  57. tooltip: {
  58. trigger: 'axis',
  59. axisPointer: {
  60. type: 'cross',
  61. label: {
  62. show: false,
  63. backgroundColor: 'rgba(98, 98, 98, 0.8)'
  64. }
  65. },
  66. backgroundColor: 'rgba(50,50,50,0.5)',
  67. position: function(pos, params, el, elRect, size) {
  68. var obj = {
  69. top: 10
  70. };
  71. obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
  72. return obj;
  73. },
  74. formatter: function(params) {
  75. let result = ''
  76. for (let i in params) {
  77. if (i == 0) {
  78. result += params[i].axisValueLabel.substring(9, 17)
  79. }
  80. let value = '--'
  81. if (params[i].data !== null) {
  82. value = params[i].data
  83. }
  84. // #ifdef H5
  85. result += '\n' + params[i].seriesName + ':' + value
  86. // #endif
  87. // #ifdef APP-PLUS
  88. result += '<br/>' + params[i].marker + params[i].seriesName + ':' + value
  89. // #endif
  90. }
  91. return result;
  92. }
  93. },
  94. dataZoom: [{
  95. type: 'slider',
  96. show: true,
  97. start: 95,
  98. end: 100,
  99. handleSize: 8
  100. },
  101. {
  102. type: 'inside',
  103. start: 95,
  104. end: 100
  105. },
  106. {
  107. type: 'slider',
  108. show: false,
  109. yAxisIndex: 0,
  110. filterMode: 'empty',
  111. width: 12,
  112. height: '70%',
  113. handleSize: 8,
  114. showDataShadow: false,
  115. left: '93%'
  116. }
  117. ]
  118. },
  119. lineTime: null
  120. }
  121. },
  122. onLoad() {
  123. },
  124. methods: {
  125. }
  126. }
  127. </script>
  128. <script module="echarts" lang="renderjs">
  129. import {
  130. nowDate,
  131. timeToDate
  132. } from '../../utils/date.js';
  133. let myChart
  134. export default {
  135. beforeDestroy() {
  136. clearInterval(this.lineTime)
  137. this.lineTime = null
  138. },
  139. mounted() {
  140. if (typeof window.echarts === 'function') {
  141. this.initEcharts()
  142. } else {
  143. // 动态引入较大类库避免影响页面展示
  144. const script = document.createElement('script')
  145. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  146. script.src = 'static/echarts.min.js'
  147. script.onload = this.initEcharts.bind(this)
  148. document.head.appendChild(script)
  149. }
  150. this.getEmit()
  151. },
  152. methods: {
  153. initEcharts() {
  154. myChart = echarts.init(document.getElementById('echarts'))
  155. myChart.setOption(this.option)
  156. },
  157. getServerData() {
  158. const chartData = this.echartsData.chartItemList
  159. const timeData = chartData[0].valueTimeList
  160. const time = timeData.map(item => {
  161. return item.substring(0, 19).replaceAll('-', "")
  162. })
  163. const series = chartData.map(item => {
  164. return {
  165. type: 'line',
  166. name: item.describe ? item.describe : item.itemName,
  167. data: item.valueList
  168. }
  169. })
  170. this.option.xAxis.data = time
  171. this.option.series = series
  172. },
  173. getEmit() {
  174. const {
  175. bucketType,
  176. bucketValue
  177. } = this.echartsData
  178. let time = null
  179. switch (bucketType) {
  180. case 0:
  181. time = 86400000 * bucketValue
  182. break;
  183. case 1:
  184. time = 3600000 * bucketValue
  185. break;
  186. case 2:
  187. time = 60000 * bucketValue
  188. break;
  189. case 3:
  190. time = 1000 * bucketValue
  191. break;
  192. }
  193. this.getServerData()
  194. this.lineTime = setInterval(() => {
  195. const currentTime = nowDate();
  196. let lastTime;
  197. uni.getStorage({
  198. key: 'sTime',
  199. success: (o) => {
  200. lastTime = o.data;
  201. }
  202. })
  203. let chartParams = {
  204. id: this.echartsData.id,
  205. startTime: lastTime,
  206. endTime: currentTime
  207. }
  208. uni.setStorage({
  209. key: 'sTime',
  210. data: currentTime
  211. })
  212. uni.$http.get('/chart/getChartById', chartParams).then(res => {
  213. let chartItemList = res.data.data.chartItemList;
  214. chartItemList.forEach((item, index) => {
  215. if (item.itemId === this.echartsData.chartItemList[index].itemId) {
  216. if (item.valueList.length !== 0) {
  217. //执行将新数据添加进去,旧数据去除一个
  218. this.echartsData.chartItemList[index].valueList.push(...item
  219. .valueList)
  220. // this.echartsData.chartItemList[index].valueList.slice(0, item
  221. // .valueList.length)
  222. this.echartsData.chartItemList[index].valueList.slice(item
  223. .valueList.length, this.echartsData.chartItemList[
  224. index].valueList.length)
  225. this.echartsData.chartItemList[index].valueTimeList.push(...
  226. item
  227. .valueTimeList)
  228. // this.echartsData.chartItemList[index].valueTimeList.splice(0,
  229. // item
  230. // .valueTimeList.length)
  231. this.echartsData.chartItemList[index].valueTimeList.splice(
  232. item
  233. .valueTimeList.length, this.echartsData.chartItemList[
  234. index].valueTimeList.length)
  235. }
  236. }
  237. })
  238. this.initEcharts()
  239. })
  240. }, time)
  241. },
  242. // updateEcharts(newValue, oldValue, ownerInstance, instance) {
  243. // // 监听 service 层数据变更
  244. // myChart.setOption(newValue)
  245. // },
  246. // onClick(event, ownerInstance) {
  247. // // 调用 service 层的方法
  248. // ownerInstance.callMethod('onViewClick', {
  249. // test: 'test'
  250. // })
  251. // }
  252. }
  253. }
  254. </script>
  255. <style>
  256. .echarts {
  257. width: 100%;
  258. height: 540rpx;
  259. }
  260. </style>