pie-wv.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. legend: {
  75. type: 'scroll',
  76. orient: 'vertical',
  77. left: 'left',
  78. top: 20
  79. },
  80. tooltip: {
  81. trigger: 'item',
  82. confine: true,
  83. extraCssText: 'white-space: normal; word-break: break-all;',
  84. },
  85. series: []
  86. };
  87. </script>
  88. <script>
  89. document.addEventListener('UniAppJSBridgeReady', function() {
  90. uni.getEnv(function(res) {
  91. const id = GetQueryString('id')
  92. const startTime = GetQueryString('startTime')
  93. const endTime = nowDate()
  94. const token = window.localStorage.getItem('C_TOKEN')
  95. var echartsData
  96. let chartParams = {
  97. id: id
  98. }
  99. $.ajax({
  100. url: "",
  101. type: 'GET',
  102. beforeSend: function(xhr) {
  103. // this.url =
  104. // `http://192.168.0.41:8081/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`
  105. this.url = window.location.origin +
  106. `/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`
  107. xhr.setRequestHeader("Authorization", "Bearer " +
  108. token);
  109. xhr.setRequestHeader("token", token);
  110. },
  111. success: function(res) {
  112. if (res.code === 200) {
  113. option.title.text = res.data.chartName
  114. echartsData = res.data.chartItemList
  115. getServerData(echartsData)
  116. getEmit(res)
  117. }
  118. }
  119. })
  120. function initEcharts() {
  121. var chart = echarts.init(document.getElementById('main'));
  122. chart.setOption(option)
  123. }
  124. function getServerData(echartsData) {
  125. const chartData = echartsData[0]
  126. chartData.valueTimeList = chartData.valueTimeList.map(item => {
  127. return item.substring(0, 19)
  128. })
  129. const {
  130. valueList,
  131. valueTimeList
  132. } = chartData
  133. let data = valueTimeList.map((item, i) => {
  134. return {
  135. name: item,
  136. value: valueList[i]
  137. }
  138. })
  139. option.series = [{
  140. type: 'pie',
  141. radius: '55%',
  142. label: {
  143. show: false
  144. },
  145. data: data,
  146. center: ['70%', '50%']
  147. }]
  148. var chart = echarts.init(document.getElementById('main'));
  149. initEcharts()
  150. }
  151. function getEmit(res) {
  152. const {
  153. bucketType,
  154. bucketValue
  155. } = res.data
  156. var time = null
  157. switch (bucketType) {
  158. case 0:
  159. time = 86400000 * bucketValue
  160. break;
  161. case 1:
  162. time = 3600000 * bucketValue
  163. break;
  164. case 2:
  165. time = 60000 * bucketValue
  166. break;
  167. case 3:
  168. time = 1000 * bucketValue
  169. break;
  170. }
  171. var lineTime = setInterval(() => {
  172. const currentTime = nowDate();
  173. var lastTime = localStorage.getItem(id + 'sTime');
  174. localStorage.setItem(id + 'sTime', currentTime)
  175. $.ajax({
  176. url: "",
  177. type: 'GET',
  178. beforeSend: function(xhr) {
  179. // this.url =
  180. // `http://192.168.0.41:8081/chart/getChartById?id=${id}&startTime=${lastTime}&endTime=${currentTime}`
  181. this.url = window.location.origin +
  182. "/chart/getChartById?id=" + id + "&startTime=" +
  183. lastTime + "&endTime=" + currentTime
  184. xhr.setRequestHeader("Authorization", "Bearer " +
  185. token);
  186. xhr.setRequestHeader("token", token);
  187. },
  188. success: function(res) {
  189. if (res.code === 200) {
  190. let chartItemList = res.data.chartItemList;
  191. chartItemList.forEach((item, index) => {
  192. if (item.itemId === echartsData[
  193. index].itemId) {
  194. if (item.valueList.length !== 0) {
  195. //执行将新数据添加进去,旧数据去除一个
  196. echartsData[index].valueList =
  197. echartsData[index].valueList
  198. .concat(item.valueList)
  199. echartsData[
  200. index]
  201. .valueList.splice(0, item
  202. .valueList.length)
  203. echartsData[index]
  204. .valueTimeList = echartsData[
  205. index]
  206. .valueTimeList.concat(item
  207. .valueTimeList)
  208. echartsData[
  209. index]
  210. .valueTimeList.splice(0,
  211. item
  212. .valueTimeList.length)
  213. }
  214. }
  215. })
  216. getServerData(echartsData)
  217. }
  218. }
  219. })
  220. }, time)
  221. }
  222. })
  223. })
  224. </script>
  225. </body>
  226. </html>