actuator_chart_S.html 17 KB

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