actuator_chart_R.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <!-- 宣告我們要套用模板 -->
  2. {% extends "base.html" %}
  3. {% block title %}{{ title }}{% endblock %}
  4. {% block script %}
  5. <!--引入 echarts.js -->
  6. <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  7. <script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js'></script>
  8. <script src='https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.js'></script>
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-zoom/0.3.0/Chart.Zoom.min.js"></script>
  10. <script language="JavaScript">
  11. var tank = '{{tank}}';
  12. var sensor_name = '{{sensor_name}}';
  13. var status = '{{status}}';
  14. var username = '{{username}}';
  15. var tid = '{{tid}}';
  16. $(document).ready(function(){
  17. $("#coffee_title_pc").text(' 中水槽致動器圖表');
  18. $("#coffee_title_phone").text(' 致動器');
  19. $('#navbarDropdown_user_pc').text(username);
  20. $('#navbarDropdown_user_phone').text(username);
  21. });
  22. </script>
  23. {% endblock %}
  24. {% block main %}
  25. <div class="album py-5 bg-light">
  26. <div class="container">
  27. <div class="row row-cols-1 row-cols-sm-1 row-cols-md-1 g-3">
  28. <div class="col">
  29. <div class="card shadow-sm">
  30. <div class="card-body">
  31. <div class="d-flex justify-content-between align-items-center">
  32. <table style="margin: auto; border:3px gray solid; width: 80%; text-align: center;" cellpadding="6" rules='ALL'>
  33. <tr>
  34. <td style="border:1px gray solid;">
  35. <select id="tank_num">
  36. <option value="R1" selected>中水槽_1</option>
  37. </select>
  38. {% if sensor_name=='solenoid_water_in' %}
  39. <select id="sensors">
  40. <option value="solenoid_water_in" selected>清洗入水電磁閥</option>
  41. <option value="solenoid_reclaimed_out">排水中水電磁閥</option>
  42. <option value="solenoid_water_out">排水廢水電磁閥</option>
  43. <option value="pump_reclaimed_out">入水清洗槽幫浦</option>
  44. </select>
  45. {% elif sensor_name=='solenoid_reclaimed_out' %}
  46. <select id="sensors">
  47. <option value="solenoid_water_in">清洗入水電磁閥</option>
  48. <option value="solenoid_reclaimed_out"selected>排水中水電磁閥</option>
  49. <option value="solenoid_water_out">排水廢水電磁閥</option>
  50. <option value="pump_reclaimed_out">入水清洗槽幫浦</option>
  51. </select>
  52. {% elif sensor_name=='solenoid_water_out' %}
  53. <select id="sensors">
  54. <option value="solenoid_water_in">清洗入水電磁閥</option>
  55. <option value="solenoid_reclaimed_out">排水中水電磁閥</option>
  56. <option value="solenoid_water_out"selected>排水廢水電磁閥</option>
  57. <option value="pump_reclaimed_out">入水清洗槽幫浦</option>
  58. </select>
  59. {% elif sensor_name=='pump_reclaimed_out' %}
  60. <select id="sensors">
  61. <option value="solenoid_water_in">清洗入水電磁閥</option>
  62. <option value="solenoid_reclaimed_out">排水中水電磁閥</option>
  63. <option value="solenoid_water_out">排水廢水電磁閥</option>
  64. <option value="pump_reclaimed_out"selected>入水清洗槽幫浦</option>
  65. </select>
  66. {% else %}
  67. <select id="sensors">
  68. <option value="solenoid_water_in" selected>清洗入水電磁閥</option>
  69. <option value="solenoid_reclaimed_out">排水中水電磁閥</option>
  70. <option value="solenoid_water_out">排水廢水電磁閥</option>
  71. <option value="pump_reclaimed_out">入水清洗槽幫浦</option>
  72. </select>
  73. {% endif %}
  74. </td>
  75. </tr>
  76. <tr>
  77. <td style="border:1px gray solid;">
  78. 時間間隔:
  79. <select name="time-interval" class="text-right">
  80. <option value="month">逐月</option>
  81. <option value="day">逐日</option>
  82. <option value="hour">逐時</option>
  83. </select>
  84. &nbsp;
  85. <!--
  86. 從&nbsp;
  87. <input type="datetime-local" name="sensor_starttime" id="sensor_starttime" value={{starttime}}>
  88. &nbsp;到&nbsp;
  89. <input type="datetime-local" name="sensor_endtime" id="sensor_endtime" value={{endtime}}>
  90. &nbsp;為止
  91. -->
  92. <span>從</span>
  93. <label for="querydate-start"></label><input id="querydate-start" type="date">
  94. <span>到</span>
  95. <label for="querydate-end"></label><input id="querydate-end" type="date">
  96. <span>為止</span>
  97. </td>
  98. </tr>
  99. <tr>
  100. <td style="border:1px gray solid;">
  101. 資料類型:
  102. <label><input type="checkbox" name="data-type" value="max">最大值</label>
  103. <label><input type="checkbox" name="data-type" value="avg">平均值</label>
  104. <label><input type="checkbox" name="data-type" value="min">最小值</label>
  105. <br>
  106. </td>
  107. </tr>
  108. <tr>
  109. <td style="border:1px gray solid;">
  110. <!--
  111. <button type="submit" name="charting">繪製圖表</button>
  112. <button type="submit" onclick="" id="btn_save_chart" style="display: none;">儲存圖表</button>
  113. -->
  114. <input type="button" class="btn btn-secondary" value="查詢" onclick="ShowInfo()">
  115. <input type="button" class="btn btn-secondary" value="匯出" onclick="downloadFile()">
  116. <script>
  117. function ShowInfo() {
  118. // 取得桶號、感測器名稱
  119. tid = document.getElementById("tank_num").value
  120. console.log('tid: ' + tid)
  121. sensor_name = document.getElementById("sensors").value
  122. console.log('sensor_name: ' + sensor_name)
  123. // 數據整理
  124. Echart(sensor_name, tid)
  125. // 顯示圖表
  126. };
  127. function downloadFile() {
  128. tid = document.getElementById("tank_num").value
  129. sensor_name = document.getElementById("sensors").value
  130. //藉型別陣列建構的 blob 來建立 URL
  131. let fileName = "ChartData_" + tid + "_" + sensor_name + ".csv";
  132. //"\ufeff"解決打開CSV中文亂碼問題
  133. const data = "\ufeff" + getData();
  134. let blob = new Blob([data], {
  135. type: 'text/csv,charset=UTF-8'
  136. });
  137. var href = URL.createObjectURL(blob);
  138. // 從 Blob 取出資料
  139. var link = document.createElement("a");
  140. document.body.appendChild(link);
  141. link.href = href;
  142. link.download = fileName;
  143. link.click();
  144. };
  145. //所有資料函數
  146. function getData() {
  147. var header = "日期," + data_name + "\n";
  148. var data = "";
  149. var length = all_datetime.length;
  150. for (var i = 0; i < length; i++) {
  151. data = data + all_datetime[i] + ',' + all_data[i] + '\n';
  152. };
  153. return header + data;
  154. };
  155. //最大最小平均資料函數
  156. function getData() {
  157. var header = data_name + "\n日期,";
  158. if (max_data) {
  159. console.log(max_data);
  160. header = header + "最大值,";
  161. };
  162. if (avg_data) {
  163. header = header + "平均值,";
  164. };
  165. if (min_data) {
  166. header = header + "最小值,";
  167. };
  168. header = header + "\n";
  169. var data = "";
  170. var length = xAxis_data.length;
  171. for (var i = 0; i < length; i++) {
  172. data = data + xAxis_data[i] + ',';
  173. if (max_data) {
  174. data = data + max_data[i] + ',';
  175. };
  176. if (avg_data) {
  177. data = data + avg_data[i] + ',';
  178. };
  179. if (min_data) {
  180. data = data + min_data[i] + ',';
  181. };
  182. data = data + '\n';
  183. };
  184. return header + data;
  185. };
  186. </script>
  187. </td>
  188. </tr>
  189. <tr>
  190. <td>
  191. <!--
  192. <img src={{ url }}>
  193. -->
  194. <div id="" class="show-info" style="text-align: center;">
  195. <center>
  196. <div class="show-chart" style="height: 420px; width: 100%; text-align: center;">
  197. 圖表
  198. </div>
  199. </center>
  200. </div>
  201. </td>
  202. </tr>
  203. </table>
  204. </div>
  205. </div>
  206. </div>
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. {% endblock %}
  212. {% block script2 %}
  213. <script>
  214. var all_datetime = 0; // 所有資料時間 ?
  215. var all_data = 0; // 所有感測器數據 ?
  216. var data_name = 0; // 感測器名稱 // "溫溼度"
  217. var xAxis_data = 0; // x 軸資料
  218. var max_data = 0; //
  219. var min_data = 0; //
  220. var avg_data = 0; //
  221. function Echart(sensor_name, tid) {
  222. // Echart(sensor_name, tid)
  223. //將echarts圖形銷毀
  224. $(".show-chart").removeAttr("_echarts_instance_").empty();
  225. // Rita:sensor 與 data_name 重複
  226. // var sensor = evt.substring(2);
  227. if (!$("input[value=avg]").prop('checked') && !$("input[value=min]").prop('checked') && !$("input[value=max]").prop('checked')) {
  228. alert("請至少選擇一種資料類型!");
  229. return false;
  230. };
  231. if ($("#querydate-start").val() == '' || $("#querydate-end").val() == '') {
  232. alert("請選擇日期範圍!");
  233. return false;
  234. };
  235. //基於準備好的dom,初始化echarts例項
  236. var myChart = echarts.init(document.getElementsByClassName('show-chart')[0]); // class="col-10 show-chart"
  237. var date_start = $("#querydate-start").val();
  238. var date_end = $("#querydate-end").val();
  239. var avg = 0;
  240. var max = 0;
  241. var min = 0;
  242. var time_interval = $("select[name=time-interval]").val();
  243. var legend = new Array();
  244. var color = new Array();
  245. var series = new Array();
  246. if ($("input[value=max]").prop('checked')) {
  247. max = 1;
  248. legend.push("最大值");
  249. color.push("#1e88e5"); // 水藍色
  250. };
  251. if ($("input[value=avg]").prop('checked')) {
  252. avg = 1;
  253. legend.push("平均值");
  254. color.push("#43a047"); // 草綠色
  255. };
  256. if ($("input[value=min]").prop('checked')) {
  257. min = 1;
  258. legend.push("最小值");
  259. color.push("#e64a19"); // 橘紅色
  260. };
  261. var json = {
  262. 'tid': tid,
  263. 'sensor_name': sensor_name,
  264. 'avg': avg,
  265. 'max': max,
  266. 'min': min,
  267. 'time-interval': time_interval,
  268. 'date-start': date_start,
  269. 'date-end': date_end
  270. };
  271. console.log('json___' +
  272. 'tid: ' + tid +
  273. 'sensor_name: ' + sensor_name +
  274. 'avg: ' + avg +
  275. 'max: ' + max +
  276. 'min: ' + min +
  277. 'time-interval: ' + time_interval +
  278. 'date-start: ' + date_start +
  279. 'date-end: ' + date_end
  280. )
  281. $.get('/history_data_new', json, function (resText) {
  282. if (resText.max) {
  283. var max_series = {
  284. 'name': '最大值',
  285. 'type': 'scatter',
  286. 'data': resText.max.map(function (item) { return item[1]; }),
  287. 'itemStyle': { 'normal': { 'lineStyle': { 'color': '#1e88e5' } } },
  288. };
  289. series.push(max_series); // 把上面的 dict 加入 series array 內
  290. xAxis_data = resText.max.map(function (item) { return item[0]; });
  291. max_data = resText.max.map(function (item) { return item[1]; });
  292. };
  293. if (resText.avg) {
  294. var avg_series = { 'name': '平均值', 'type': 'scatter', 'data': resText.avg.map(function (item) { return item[1]; }), 'itemStyle': { 'normal': { 'lineStyle': { 'color': '#43a047' } } }, };
  295. series.push(avg_series);
  296. xAxis_data = resText.avg.map(function (item) { return item[0]; });
  297. console.log('xAxis_data' + xAxis_data) // 2021-07-09 10:00:00,2021-07-20 09:00:00,2021-07-22 15:00:00,2021-07-26 15:00:00,2021-07-26 16:00:00
  298. avg_data = resText.avg.map(function (item) { return item[1]; });
  299. console.log('avg_data' + avg_data) // 91.0,98.0,11.0,15.2,30.5
  300. };
  301. if (resText.min) {
  302. var min_series = { 'name': '最小值', 'type': 'scatter', 'data': resText.min.map(function (item) { return item[1]; }), 'itemStyle': { 'normal': { 'lineStyle': { 'color': '#e64a19' } } }, };
  303. series.push(min_series);
  304. xAxis_data = resText.min.map(function (item) { return item[0]; }); // all
  305. min_data = resText.min.map(function (item) { return item[1]; }); // min
  306. };
  307. //獲取日期內所有數據
  308. all_datetime = resText.all.map(function (item) { return item[0]; }); // all
  309. all_data = resText.all.map(function (item) { return item[1]; });
  310. //指定圖表的配置項和資料
  311. option = {
  312. title: {
  313. text: '', // 'Data History'
  314. left: '1%'
  315. },
  316. tooltip: {
  317. trigger: 'axis'
  318. },
  319. color: color,
  320. legend: {
  321. data: legend,
  322. },
  323. xAxis: {
  324. data: xAxis_data,
  325. },
  326. yAxis: {
  327. splitLine: {
  328. show: true,
  329. lineStyle: {
  330. color: ''
  331. }
  332. }
  333. },
  334. toolbox: {
  335. show: true,
  336. feature: {
  337. saveAsImage: {}
  338. }
  339. },
  340. dataZoom: [{
  341. startValue: '2021-01-01'
  342. }, {
  343. type: 'inside'
  344. }],
  345. series: series,
  346. /*
  347. series: [
  348. {
  349. name: '最大值',
  350. type: 'line',
  351. data: resText.max.map(function (item) {
  352. return item[1];
  353. }),
  354. itemStyle: {
  355. normal: {
  356. lineStyle: {
  357. color: '#1e88e5'
  358. }
  359. }
  360. },
  361. },
  362. {
  363. name: '平均值',
  364. type: 'line',
  365. data: [20.0, 20.0, 20.0, 20.0, 20.0, 20.0],
  366. itemStyle: {
  367. normal: {
  368. lineStyle: {
  369. color: '#43a047'
  370. }
  371. }
  372. },
  373. },
  374. {
  375. name: '最小值',
  376. type: 'line',
  377. data: [15.0, 15.0, 15.0, 15.0, 15.0, 15.0],
  378. itemStyle: {
  379. normal: {
  380. lineStyle: {
  381. color: '#e64a19'
  382. }
  383. }
  384. }
  385. },
  386. ]
  387. */
  388. };
  389. console.log('SHOW myChart')
  390. //使用剛指定的配置項和資料顯示圖表
  391. myChart.setOption(option);
  392. window.addEventListener('resize', myChart.resize);
  393. }, 'json');
  394. } // Echart(sensor_name, tid)
  395. </script>
  396. {% endblock %}