pie-wv.html 6.4 KB

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