line-wv.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. html {
  10. height: 100%
  11. }
  12. body {
  13. height: 100%;
  14. margin: 0
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="main" style="width:100%;height:85%;margin:auto"></div>
  20. <script src="./echarts.min.js"></script>
  21. <script type="text/javascript" src="../luckysheet/dist/plugins/js/uni-webview-js0.0.3_index.js"></script>
  22. <script src="../jquery.min.js"></script>
  23. <script>
  24. function GetQueryString(name) {
  25. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  26. var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
  27. var context = "";
  28. if (r != null)
  29. context = decodeURIComponent(r[2]);
  30. reg = null;
  31. r = null;
  32. return context == null || context == "" || context == "undefined" ? "" : context;
  33. }
  34. function nowDate() {
  35. var date = new Date();
  36. var sign2 = ":";
  37. var year = date.getFullYear() // 年
  38. var month = date.getMonth() + 1; // 月
  39. var day = date.getDate(); // 日
  40. var hour = date.getHours(); // 时
  41. var minutes = date.getMinutes(); // 分
  42. var seconds = date.getSeconds() //秒
  43. var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  44. var week = weekArr[date.getDay()];
  45. // 给一位数的数据前面加 “0”
  46. if (month >= 1 && month <= 9) {
  47. month = "0" + month;
  48. }
  49. if (day >= 0 && day <= 9) {
  50. day = "0" + day;
  51. }
  52. if (hour >= 0 && hour <= 9) {
  53. hour = "0" + hour;
  54. }
  55. if (minutes >= 0 && minutes <= 9) {
  56. minutes = "0" + minutes;
  57. }
  58. if (seconds >= 0 && seconds <= 9) {
  59. seconds = "0" + seconds;
  60. }
  61. return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
  62. }
  63. </script>
  64. <script>
  65. var option = {
  66. title: {
  67. text: '',
  68. textStyle: {
  69. color: '#666666',
  70. fontWeight: 'normal',
  71. fontSize: '14'
  72. }
  73. },
  74. xAxis: {
  75. type: 'category',
  76. data: [],
  77. axisLabel: {
  78. formatter: function(value) {
  79. return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join('')
  80. }
  81. }
  82. },
  83. yAxis: {
  84. type: 'value'
  85. },
  86. legend: {
  87. type: 'scroll',
  88. top: 30
  89. },
  90. tooltip: {
  91. trigger: 'axis',
  92. axisPointer: {
  93. type: 'cross',
  94. lineStyle: {
  95. type: 'dashed'
  96. },
  97. label: {
  98. show: false
  99. },
  100. },
  101. confine: true,
  102. extraCssText: 'white-space: normal; word-break: break-all;',
  103. backgroundColor: 'rgba(255,255,255,0.1)', //通过设置rgba调节背景颜色与透明度
  104. position: 'inside',
  105. formatter: function(params) {
  106. let result = ''
  107. for (let i in params) {
  108. let value = '--'
  109. if (params[i].data !== null) {
  110. value = params[i].data
  111. }
  112. result += '<br>' + params[i].seriesName + ':' +
  113. value
  114. }
  115. return result
  116. }
  117. },
  118. dataZoom: [{
  119. type: 'inside',
  120. start: 90,
  121. end: 100
  122. },
  123. {
  124. type: 'slider',
  125. show: true,
  126. start: 90,
  127. end: 100,
  128. bottom: 10
  129. }
  130. ],
  131. series: []
  132. };
  133. </script>
  134. <script>
  135. document.addEventListener('UniAppJSBridgeReady', function() {
  136. uni.getEnv(function(res) {
  137. const id = GetQueryString('id')
  138. const startTime = GetQueryString('startTime')
  139. const endTime = nowDate()
  140. const token = window.localStorage.getItem('C_TOKEN')
  141. var echartsData
  142. let chartParams = {
  143. id: id
  144. }
  145. $.ajax({
  146. url: "",
  147. type: 'GET',
  148. beforeSend: function(xhr) {
  149. // this.url =
  150. // `http://192.168.0.41:8081/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`
  151. this.url = window.location.origin +
  152. `/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`
  153. xhr.setRequestHeader("Authorization", "Bearer " +
  154. token);
  155. xhr.setRequestHeader("token", token);
  156. },
  157. success: function(res) {
  158. if (res.code === 200) {
  159. option.title.text = res.data.chartName
  160. echartsData = res.data.chartItemList
  161. getServerData(echartsData)
  162. getEmit(res)
  163. }
  164. }
  165. })
  166. function initEcharts() {
  167. var chart = echarts.init(document.getElementById('main'));
  168. chart.setOption(option)
  169. }
  170. function getServerData(echartsData) {
  171. const chartData = echartsData
  172. const timeData = chartData[0].valueTimeList
  173. const series = chartData.map(item => {
  174. return {
  175. name: item.describe ? item.describe : item.itemName,
  176. data: item.valueList,
  177. type: 'line'
  178. }
  179. })
  180. var chart = echarts.init(document.getElementById('main'));
  181. var lastOption = chart.getOption();
  182. if (lastOption != undefined) {
  183. option.dataZoom[0].start = lastOption.dataZoom[0].start
  184. option.dataZoom[0].end = lastOption.dataZoom[0].end
  185. option.dataZoom[1].start = lastOption.dataZoom[1].start
  186. option.dataZoom[1].end = lastOption.dataZoom[1].end
  187. }
  188. option.xAxis.data = timeData
  189. option.series = series
  190. initEcharts()
  191. }
  192. function getEmit(res) {
  193. const {
  194. bucketType,
  195. bucketValue
  196. } = res.data
  197. var time = null
  198. switch (bucketType) {
  199. case 0:
  200. time = 86400000 * bucketValue
  201. break;
  202. case 1:
  203. time = 3600000 * bucketValue
  204. break;
  205. case 2:
  206. time = 60000 * bucketValue
  207. break;
  208. case 3:
  209. time = 1000 * bucketValue
  210. break;
  211. }
  212. var lineTime = setInterval(() => {
  213. const currentTime = nowDate();
  214. var lastTime = localStorage.getItem(id + 'sTime');
  215. localStorage.setItem(id + 'sTime', currentTime)
  216. $.ajax({
  217. url: "",
  218. type: 'GET',
  219. beforeSend: function(xhr) {
  220. // this.url =
  221. // `http://192.168.0.41:8081/chart/getChartById?id=${id}&startTime=${lastTime}&endTime=${currentTime}`
  222. this.url=window.location.origin+"/chart/getChartById?id="+id+"&startTime="+lastTime+"&endTime="+currentTime
  223. xhr.setRequestHeader("Authorization", "Bearer " +
  224. token);
  225. xhr.setRequestHeader("token", token);
  226. },
  227. success: function(res) {
  228. if (res.code === 200) {
  229. let chartItemList = res.data.chartItemList;
  230. chartItemList.forEach((item, index) => {
  231. if (item.itemId === echartsData[
  232. index].itemId) {
  233. if (item.valueList.length !== 0) {
  234. //执行将新数据添加进去,旧数据去除一个
  235. echartsData[index].valueList =
  236. echartsData[index].valueList
  237. .concat(item.valueList)
  238. echartsData[
  239. index]
  240. .valueList.splice(0, item
  241. .valueList.length)
  242. echartsData[index]
  243. .valueTimeList = echartsData[
  244. index]
  245. .valueTimeList.concat(item
  246. .valueTimeList)
  247. echartsData[
  248. index]
  249. .valueTimeList.splice(0,
  250. item
  251. .valueTimeList.length)
  252. }
  253. }
  254. })
  255. getServerData(echartsData)
  256. }
  257. }
  258. })
  259. }, time)
  260. }
  261. })
  262. })
  263. </script>
  264. </body>
  265. </html>