bar-wv.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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>柱状图</title>
  8. <link rel="stylesheet" href="css/chartpage.css">
  9. </head>
  10. <body>
  11. <div>
  12. <!-- 时间类型 -->
  13. <div id="moreTime" class="moreTime">
  14. </div>
  15. <!-- 时间间隔 -->
  16. <div id="childTime" class="childTime">
  17. <div style="" data-type=0>
  18. <div class="box" onclick="childTimeOnClick(this,900000)">15分</div>
  19. <div class="" onclick="childTimeOnClick(this,1800000)">30分</div>
  20. <div class="" onclick="childTimeOnClick(this,2700000)">45分</div>
  21. </div>
  22. <div style="display: none;" data-type=1>
  23. <div class="" onclick="childTimeOnClick(this,3600000)">1小时</div>
  24. <div class="" onclick="childTimeOnClick(this,21600000)">6小时</div>
  25. <div class="" onclick="childTimeOnClick(this,43200000)">12小时</div>
  26. <div class="" onclick="childTimeOnClick(this,64800000)">18小时</div>
  27. </div>
  28. <div style="display: none;" data-type=2>
  29. <div class="" onclick="childTimeOnClick(this,86400000)">1日</div>
  30. <div class="" onclick="childTimeOnClick(this,259200000)">3日</div>
  31. <div class="" onclick="childTimeOnClick(this,432000000)">5日</div>
  32. </div>
  33. <div style="display: none;" data-type=3>
  34. <div class="" onclick="childTimeOnClick(this,604800000)">1周</div>
  35. <div class="" onclick="childTimeOnClick(this,1209600000)">2周</div>
  36. <div class="" onclick="childTimeOnClick(this,1814400000)">3周</div>
  37. <div class="" onclick="childTimeOnClick(this,2419200000)">4周</div>
  38. </div>
  39. </div>
  40. </div>
  41. <div id="main" style="width:100%;height:85%;margin:auto"></div>
  42. <script src="./echarts.min.js"></script>
  43. <script src="./cqcyCode.js"></script>
  44. <script type="text/javascript" src="../luckysheet/dist/plugins/js/uni-webview-js0.0.3_index.js"></script>
  45. <script src="../jquery.min.js"></script>
  46. <script src="../date.js"></script>
  47. <script>
  48. </script>
  49. <script>
  50. var option = {
  51. title: {
  52. text:'',
  53. textStyle:{
  54. color:'#666666',
  55. fontWeight:'normal',
  56. fontSize:'14'
  57. }
  58. },
  59. xAxis: {
  60. type: 'category',
  61. data: [],
  62. axisLabel: {
  63. formatter: function(value) {
  64. return value.substring(11, 19) + "\n" + value.substring(0, 10).split('-').join('')
  65. }
  66. }
  67. },
  68. yAxis: {
  69. type: 'value'
  70. },
  71. legend: {
  72. type: 'scroll',
  73. top: 30
  74. },
  75. tooltip: {
  76. trigger: 'axis',
  77. axisPointer: {
  78. type: 'cross',
  79. lineStyle: {
  80. type: 'dashed'
  81. },
  82. label: {
  83. show: false
  84. }
  85. },
  86. textStyle:{
  87. color:'#ffffff'
  88. },
  89. confine: true,
  90. extraCssText: 'white-space: normal; word-break: break-all;',
  91. backgroundColor: 'rgba(108,106,120,0.9)',
  92. position: 'inside',
  93. formatter: function(params) {
  94. let result = ''
  95. for (let i in params) {
  96. let value = '--'
  97. if (params[i].data !== null) {
  98. value = params[i].data
  99. }
  100. result += '<br>' + params[i].seriesName + ':' +
  101. value
  102. }
  103. return result
  104. }
  105. },
  106. dataZoom: [{
  107. type: 'inside',
  108. start: 100,
  109. end: 100,
  110. zoomOnMouseWheel: true
  111. },
  112. {
  113. type: 'slider',
  114. show: true,
  115. start: 100,
  116. end: 100,
  117. bottom: 10
  118. }
  119. ],
  120. series: []
  121. };
  122. </script>
  123. <script>
  124. var selTimeValue = 900000;
  125. //初始化时间选择区域
  126. function initHeader(){
  127. var range = ["分", "时", "日", "周"];
  128. let html_moreTime = "";
  129. for(var i=0;i<range.length;i++){
  130. if(i==0){
  131. html_moreTime = html_moreTime + "<div class='bod' onclick=moreTimeOnClick(this,"+i+");>" + range[i] + "</div>"
  132. }else{
  133. html_moreTime = html_moreTime + "<div class='' onclick=moreTimeOnClick(this,"+i+");>" + range[i] + "</div>";
  134. }
  135. };
  136. $("#moreTime").html(html_moreTime);
  137. }
  138. initHeader();
  139. function moreTimeOnClick(e,index){
  140. $("#childTime [data-type][data-type!="+index+"]").hide();
  141. $("#childTime [data-type][data-type="+index+"]").show();
  142. $(e).parent().children().removeClass("bod");
  143. $(e).addClass("bod");
  144. }
  145. function childTimeOnClick(e,timeValue){
  146. $("#childTime").children().children().removeClass("box");
  147. $(e).addClass("box");
  148. //获得选择的时间间隔
  149. initData(timeValue)
  150. }
  151. function GetQueryString(name) {
  152. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  153. var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
  154. var context = "";
  155. if (r != null)
  156. context = decodeURIComponent(r[2]);
  157. reg = null;
  158. r = null;
  159. return context == null || context == "" || context == "undefined" ? "" : context;
  160. }
  161. function initData(timeValue){
  162. const id = GetQueryString('id')
  163. const startTime = timeToDate(new Date().getTime()-timeValue);//GetQueryString('startTime')
  164. const endTime = nowDate()
  165. const token = GetQueryString('token')//window.localStorage.getItem('C_TOKEN')
  166. var echartsData
  167. let chartParams = {
  168. id: id
  169. }
  170. $.ajax({
  171. url: "",
  172. type: 'GET',
  173. beforeSend: function(xhr) {
  174. this.url = baseUrl +
  175. `/chart/getChartById?id=${id}&startTime=${startTime}&endTime=${endTime}`
  176. xhr.setRequestHeader("Authorization", "Bearer " +
  177. token);
  178. xhr.setRequestHeader("token", token);
  179. },
  180. success: function(res) {
  181. if (res.code === 200) {
  182. option.title.text = res.data.chartName
  183. echartsData = res.data.chartItemList
  184. getServerData(echartsData)
  185. getEmit(res,token)
  186. }
  187. }
  188. })
  189. }
  190. function initEcharts() {
  191. var chart = echarts.init(document.getElementById('main'));
  192. chart.setOption(option)
  193. }
  194. function getServerData(echartsData) {
  195. const chartData = echartsData
  196. const timeData = chartData[0].valueTimeList
  197. const series = chartData.map(item => {
  198. return {
  199. // name: item.describe ? item.describe : item.itemName,
  200. name: item.describe ? item.describe : item.itemReadName,
  201. data: item.valueList,
  202. type: 'bar'
  203. }
  204. })
  205. var chart = echarts.init(document.getElementById('main'));
  206. var lastOption = chart.getOption();
  207. if (lastOption != undefined) {
  208. option.dataZoom[0].start = lastOption.dataZoom[0].start
  209. option.dataZoom[0].end = lastOption.dataZoom[0].end
  210. option.dataZoom[1].start = lastOption.dataZoom[1].start
  211. option.dataZoom[1].end = lastOption.dataZoom[1].end
  212. }
  213. option.xAxis.data = timeData
  214. option.series = series
  215. initEcharts()
  216. }
  217. function getEmit(res,token) {
  218. const {
  219. bucketType,
  220. bucketValue,
  221. id
  222. } = res.data
  223. echartsData = res.data.chartItemList
  224. var time = null
  225. switch (bucketType) {
  226. case 1:
  227. time = 86400000 * bucketValue
  228. break;
  229. case 2:
  230. time = 3600000 * bucketValue
  231. break;
  232. case 3:
  233. time = 60000 * bucketValue
  234. break;
  235. case 4:
  236. time = 1000 * bucketValue
  237. break;
  238. }
  239. var lineTime = setInterval(() => {
  240. const currentTime = nowDate();
  241. var lastTime = localStorage.getItem(id + 'sTime');
  242. //const token = window.localStorage.getItem('C_TOKEN')
  243. localStorage.setItem(id + 'sTime', currentTime)
  244. $.ajax({
  245. url: "",
  246. type: 'GET',
  247. beforeSend: function(xhr) {
  248. this.url = baseUrl +
  249. `/chart/getChartById?id=${id}&startTime=${lastTime}&endTime=${currentTime}`
  250. // this.url=window.location.origin+"/chart/getChartById?id="+id+"&startTime="+lastTime+"&endTime="+currentTime
  251. xhr.setRequestHeader("Authorization", "Bearer " +
  252. token);
  253. xhr.setRequestHeader("token", token);
  254. },
  255. success: function(res) {
  256. if (res.code === 200) {
  257. let chartItemList = res.data.chartItemList;
  258. chartItemList.forEach((item, index) => {
  259. if (item.itemId === echartsData[
  260. index].itemId) {
  261. if (item.valueList.length !== 0) {
  262. //执行将新数据添加进去,旧数据去除一个
  263. echartsData[index].valueList =
  264. echartsData[index].valueList
  265. .concat(item.valueList)
  266. echartsData[
  267. index]
  268. .valueList.splice(0, item
  269. .valueList.length)
  270. echartsData[index]
  271. .valueTimeList = echartsData[
  272. index]
  273. .valueTimeList.concat(item
  274. .valueTimeList)
  275. echartsData[
  276. index]
  277. .valueTimeList.splice(0,
  278. item
  279. .valueTimeList.length)
  280. }
  281. }
  282. })
  283. getServerData(echartsData)
  284. }
  285. }
  286. })
  287. }, time)
  288. }
  289. document.addEventListener('UniAppJSBridgeReady', function() {
  290. uni.getEnv(function(res) {
  291. initData(selTimeValue);
  292. })
  293. })
  294. </script>
  295. </body>
  296. </html>