sensor_chart_F.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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>
  35. {% if tid == '2'%}
  36. <select id="tank_num">
  37. <option value="F1">發酵槽_1</option>
  38. <option value="F2" selected>發酵槽_2</option>
  39. <option value="F3">發酵槽_3</option>
  40. <option value="F4">發酵槽_4</option>
  41. <option value="F5">發酵槽_5</option>
  42. <option value="F6">發酵槽_6</option>
  43. <option value="F7">發酵槽_7</option>
  44. <option value="F8">發酵槽_8</option>
  45. <option value="F9">發酵槽_9</option>
  46. <option value="F10">發酵槽_10</option>
  47. <option value="F11">發酵槽_11</option>
  48. <option value="F12">發酵槽_12</option>
  49. </select>
  50. {% elif tid == '3'%}
  51. <select id="tank_num">
  52. <option value="F1">發酵槽_1</option>
  53. <option value="F2">發酵槽_2</option>
  54. <option value="F3" selected>發酵槽_3</option>
  55. <option value="F4">發酵槽_4</option>
  56. <option value="F5">發酵槽_5</option>
  57. <option value="F6">發酵槽_6</option>
  58. <option value="F7">發酵槽_7</option>
  59. <option value="F8">發酵槽_8</option>
  60. <option value="F9">發酵槽_9</option>
  61. <option value="F10">發酵槽_10</option>
  62. <option value="F11">發酵槽_11</option>
  63. <option value="F12">發酵槽_12</option>
  64. </select>
  65. {% elif tid == '4'%}
  66. <select id="tank_num">
  67. <option value="F1">發酵槽_1</option>
  68. <option value="F2">發酵槽_2</option>
  69. <option value="F3">發酵槽_3</option>
  70. <option value="F4" selected>發酵槽_4</option>
  71. <option value="F5">發酵槽_5</option>
  72. <option value="F6">發酵槽_6</option>
  73. <option value="F7">發酵槽_7</option>
  74. <option value="F8">發酵槽_8</option>
  75. <option value="F9">發酵槽_9</option>
  76. <option value="F10">發酵槽_10</option>
  77. <option value="F11">發酵槽_11</option>
  78. <option value="F12">發酵槽_12</option>
  79. </select>
  80. {% elif tid == '5'%}
  81. <select id="tank_num">
  82. <option value="F1">發酵槽_1</option>
  83. <option value="F2">發酵槽_2</option>
  84. <option value="F3">發酵槽_3</option>
  85. <option value="F4">發酵槽_4</option>
  86. <option value="F5" selected>發酵槽_5</option>
  87. <option value="F6">發酵槽_6</option>
  88. <option value="F7">發酵槽_7</option>
  89. <option value="F8">發酵槽_8</option>
  90. <option value="F9">發酵槽_9</option>
  91. <option value="F10">發酵槽_10</option>
  92. <option value="F11">發酵槽_11</option>
  93. <option value="F12">發酵槽_12</option>
  94. </select>
  95. {% elif tid == '6'%}
  96. <select id="tank_num">
  97. <option value="F1">發酵槽_1</option>
  98. <option value="F2">發酵槽_2</option>
  99. <option value="F3">發酵槽_3</option>
  100. <option value="F4">發酵槽_4</option>
  101. <option value="F5">發酵槽_5</option>
  102. <option value="F6" selected>發酵槽_6</option>
  103. <option value="F7">發酵槽_7</option>
  104. <option value="F8">發酵槽_8</option>
  105. <option value="F9">發酵槽_9</option>
  106. <option value="F10">發酵槽_10</option>
  107. <option value="F11">發酵槽_11</option>
  108. <option value="F12">發酵槽_12</option>
  109. </select>
  110. {% elif tid == '7'%}
  111. <select id="tank_num">
  112. <option value="F1">發酵槽_1</option>
  113. <option value="F2">發酵槽_2</option>
  114. <option value="F3">發酵槽_3</option>
  115. <option value="F4">發酵槽_4</option>
  116. <option value="F5">發酵槽_5</option>
  117. <option value="F6">發酵槽_6</option>
  118. <option value="F7" selected>發酵槽_7</option>
  119. <option value="F8">發酵槽_8</option>
  120. <option value="F9">發酵槽_9</option>
  121. <option value="F10">發酵槽_10</option>
  122. <option value="F11">發酵槽_11</option>
  123. <option value="F12">發酵槽_12</option>
  124. </select>
  125. {% elif tid == '8'%}
  126. <select id="tank_num">
  127. <option value="F1">發酵槽_1</option>
  128. <option value="F2">發酵槽_2</option>
  129. <option value="F3">發酵槽_3</option>
  130. <option value="F4">發酵槽_4</option>
  131. <option value="F5">發酵槽_5</option>
  132. <option value="F6">發酵槽_6</option>
  133. <option value="F7">發酵槽_7</option>
  134. <option value="F8" selected>發酵槽_8</option>
  135. <option value="F9">發酵槽_9</option>
  136. <option value="F10">發酵槽_10</option>
  137. <option value="F11">發酵槽_11</option>
  138. <option value="F12">發酵槽_12</option>
  139. </select>
  140. {% elif tid == '9'%}
  141. <select id="tank_num">
  142. <option value="F1">發酵槽_1</option>
  143. <option value="F2">發酵槽_2</option>
  144. <option value="F3">發酵槽_3</option>
  145. <option value="F4">發酵槽_4</option>
  146. <option value="F5">發酵槽_5</option>
  147. <option value="F6">發酵槽_6</option>
  148. <option value="F7">發酵槽_7</option>
  149. <option value="F8">發酵槽_8</option>
  150. <option value="F9" selected>發酵槽_9</option>
  151. <option value="F10">發酵槽_10</option>
  152. <option value="F11">發酵槽_11</option>
  153. <option value="F12">發酵槽_12</option>
  154. </select>
  155. {% elif tid == '10'%}
  156. <select id="tank_num">
  157. <option value="F1">發酵槽_1</option>
  158. <option value="F2">發酵槽_2</option>
  159. <option value="F3">發酵槽_3</option>
  160. <option value="F4">發酵槽_4</option>
  161. <option value="F5">發酵槽_5</option>
  162. <option value="F6">發酵槽_6</option>
  163. <option value="F7">發酵槽_7</option>
  164. <option value="F8">發酵槽_8</option>
  165. <option value="F9">發酵槽_9</option>
  166. <option value="F10" selected>發酵槽_10</option>
  167. <option value="F11">發酵槽_11</option>
  168. <option value="F12">發酵槽_12</option>
  169. </select>
  170. {% elif tid == '11'%}
  171. <select id="tank_num">
  172. <option value="F1">發酵槽_1</option>
  173. <option value="F2">發酵槽_2</option>
  174. <option value="F3">發酵槽_3</option>
  175. <option value="F4">發酵槽_4</option>
  176. <option value="F5">發酵槽_5</option>
  177. <option value="F6">發酵槽_6</option>
  178. <option value="F7">發酵槽_7</option>
  179. <option value="F8">發酵槽_8</option>
  180. <option value="F9">發酵槽_9</option>
  181. <option value="F10">發酵槽_10</option>
  182. <option value="F11" selected>發酵槽_11</option>
  183. <option value="F12">發酵槽_12</option>
  184. </select>
  185. {% elif tid == '12'%}
  186. <select id="tank_num">
  187. <option value="F1">發酵槽_1</option>
  188. <option value="F2">發酵槽_2</option>
  189. <option value="F3">發酵槽_3</option>
  190. <option value="F4">發酵槽_4</option>
  191. <option value="F5">發酵槽_5</option>
  192. <option value="F6">發酵槽_6</option>
  193. <option value="F7">發酵槽_7</option>
  194. <option value="F8">發酵槽_8</option>
  195. <option value="F9">發酵槽_9</option>
  196. <option value="F10">發酵槽_10</option>
  197. <option value="F11">發酵槽_11</option>
  198. <option value="F12" selected>發酵槽_12</option>
  199. </select>
  200. {% else %}
  201. <select id="tank_num">
  202. <option value="F1" selected>發酵槽_1</option>
  203. <option value="F2">發酵槽_2</option>
  204. <option value="F3">發酵槽_3</option>
  205. <option value="F4">發酵槽_4</option>
  206. <option value="F5">發酵槽_5</option>
  207. <option value="F6">發酵槽_6</option>
  208. <option value="F7">發酵槽_7</option>
  209. <option value="F8">發酵槽_8</option>
  210. <option value="F9">發酵槽_9</option>
  211. <option value="F10">發酵槽_10</option>
  212. <option value="F11">發酵槽_11</option>
  213. <option value="F12">發酵槽_12</option>
  214. </select>
  215. {% endif %}
  216. {% if sensor_name=='motorEncoder' %}
  217. <select id="sensors">
  218. <option value="UltraSonic">咖啡生豆高度</option>
  219. <option value="motorEncoder" selected>馬達編碼器</option>
  220. <option value="WaterLevel">保溫夾層水位</option>
  221. <option value="SHT11_Temp">溫度</option>
  222. <option value="SHT11_Humidity">濕度</option>
  223. <option value="CO2">二氧化碳</option>
  224. <option value="PH">水質酸鹼度</option>
  225. <option value="ORP">水質氧化還原電位</option>
  226. <option value="DO">水質溶氧量</option>
  227. <option value="EC">水質導電度</option>
  228. <option value="PA">大氣壓力</option>
  229. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  230. </select>
  231. {% elif sensor_name=='WaterLevel' %}
  232. <select id="sensors">
  233. <option value="UltraSonic">咖啡生豆高度</option>
  234. <option value="motorEncoder">馬達編碼器</option>
  235. <option value="WaterLevel" selected>保溫夾層水位</option>
  236. <option value="SHT11_Temp">溫度</option>
  237. <option value="SHT11_Humidity">濕度</option>
  238. <option value="CO2">二氧化碳</option>
  239. <option value="PH">水質酸鹼度</option>
  240. <option value="ORP">水質氧化還原電位</option>
  241. <option value="DO">水質溶氧量</option>
  242. <option value="EC">水質導電度</option>
  243. <option value="PA">大氣壓力</option>
  244. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  245. </select>
  246. {% elif sensor_name=='butterflyvalve' %}
  247. <select id="sensors">
  248. <option value="UltraSonic">咖啡生豆高度</option>
  249. <option value="motorEncoder">馬達編碼器</option>
  250. <option value="WaterLevel">保溫夾層水位</option>
  251. <option value="SHT11_Temp">溫度</option>
  252. <option value="SHT11_Humidity">濕度</option>
  253. <option value="CO2">二氧化碳</option>
  254. <option value="PH">水質酸鹼度</option>
  255. <option value="ORP">水質氧化還原電位</option>
  256. <option value="DO">水質溶氧量</option>
  257. <option value="EC">水質導電度</option>
  258. <option value="PA">大氣壓力</option>
  259. <option value="butterflyvalve" selected>蝴蝶閥編碼器</option>
  260. </select>
  261. {% elif sensor_name=='SHT11_Temp' %}
  262. <select id="sensors">
  263. <option value="UltraSonic">咖啡生豆高度</option>
  264. <option value="motorEncoder">馬達編碼器</option>
  265. <option value="WaterLevel">保溫夾層水位</option>
  266. <option value="SHT11_Temp" selected>溫度</option>
  267. <option value="SHT11_Humidity">濕度</option>
  268. <option value="CO2">二氧化碳</option>
  269. <option value="PH">水質酸鹼度</option>
  270. <option value="ORP">水質氧化還原電位</option>
  271. <option value="DO">水質溶氧量</option>
  272. <option value="EC">水質導電度</option>
  273. <option value="PA">大氣壓力</option>
  274. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  275. </select>
  276. {% elif sensor_name=='SHT11_Humidity' %}
  277. <select id="sensors">
  278. <option value="UltraSonic">咖啡生豆高度</option>
  279. <option value="motorEncoder">馬達編碼器</option>
  280. <option value="WaterLevel">保溫夾層水位</option>
  281. <option value="SHT11_Temp">溫度</option>
  282. <option value="SHT11_Humidity" selected>濕度</option>
  283. <option value="CO2">二氧化碳</option>
  284. <option value="PH">水質酸鹼度</option>
  285. <option value="ORP">水質氧化還原電位</option>
  286. <option value="DO">水質溶氧量</option>
  287. <option value="EC">水質導電度</option>
  288. <option value="PA">大氣壓力</option>
  289. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  290. </select>
  291. {% elif sensor_name=='CO2' %}
  292. <select id="sensors">
  293. <option value="UltraSonic">咖啡生豆高度</option>
  294. <option value="motorEncoder">馬達編碼器</option>
  295. <option value="WaterLevel">保溫夾層水位</option>
  296. <option value="SHT11_Temp">溫度</option>
  297. <option value="SHT11_Humidity">濕度</option>
  298. <option value="CO2" selected>二氧化碳</option>
  299. <option value="PH">水質酸鹼度</option>
  300. <option value="ORP">水質氧化還原電位</option>
  301. <option value="DO">水質溶氧量</option>
  302. <option value="EC">水質導電度</option>
  303. <option value="PA">大氣壓力</option>
  304. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  305. </select>
  306. {% elif sensor_name=='PH' %}
  307. <select id="sensors">
  308. <option value="UltraSonic">咖啡生豆高度</option>
  309. <option value="motorEncoder">馬達編碼器</option>
  310. <option value="WaterLevel">保溫夾層水位</option>
  311. <option value="SHT11_Temp">溫度</option>
  312. <option value="SHT11_Humidity">濕度</option>
  313. <option value="CO2">二氧化碳</option>
  314. <option value="PH" selected>水質酸鹼度</option>
  315. <option value="ORP">水質氧化還原電位</option>
  316. <option value="DO">水質溶氧量</option>
  317. <option value="EC">水質導電度</option>
  318. <option value="PA">大氣壓力</option>
  319. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  320. </select>
  321. {% elif sensor_name=='ORP' %}
  322. <select id="sensors">
  323. <option value="UltraSonic">咖啡生豆高度</option>
  324. <option value="motorEncoder">馬達編碼器</option>
  325. <option value="WaterLevel">保溫夾層水位</option>
  326. <option value="SHT11_Temp">溫度</option>
  327. <option value="SHT11_Humidity">濕度</option>
  328. <option value="CO2">二氧化碳</option>
  329. <option value="PH">水質酸鹼度</option>
  330. <option value="ORP" selected>水質氧化還原電位</option>
  331. <option value="DO">水質溶氧量</option>
  332. <option value="EC">水質導電度</option>
  333. <option value="PA">大氣壓力</option>
  334. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  335. </select>
  336. {% elif sensor_name=='DO' %}
  337. <select id="sensors">
  338. <option value="UltraSonic">咖啡生豆高度</option>
  339. <option value="motorEncoder">馬達編碼器</option>
  340. <option value="WaterLevel">保溫夾層水位</option>
  341. <option value="SHT11_Temp">溫度</option>
  342. <option value="SHT11_Humidity">濕度</option>
  343. <option value="CO2">二氧化碳</option>
  344. <option value="PH">水質酸鹼度</option>
  345. <option value="ORP">水質氧化還原電位</option>
  346. <option value="DO" selected>水質溶氧量</option>
  347. <option value="EC">水質導電度</option>
  348. <option value="PA">大氣壓力</option>
  349. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  350. </select>
  351. {% elif sensor_name=='EC' %}
  352. <select id="sensors">
  353. <option value="UltraSonic">咖啡生豆高度</option>
  354. <option value="motorEncoder">馬達編碼器</option>
  355. <option value="WaterLevel">保溫夾層水位</option>
  356. <option value="SHT11_Temp">溫度</option>
  357. <option value="SHT11_Humidity">濕度</option>
  358. <option value="CO2">二氧化碳</option>
  359. <option value="PH">水質酸鹼度</option>
  360. <option value="ORP">水質氧化還原電位</option>
  361. <option value="DO">水質溶氧量</option>
  362. <option value="EC" selected>水質導電度</option>
  363. <option value="PA">大氣壓力</option>
  364. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  365. </select>
  366. {% elif sensor_name=='PA' %}
  367. <select id="sensors">
  368. <option value="UltraSonic">咖啡生豆高度</option>
  369. <option value="motorEncoder">馬達編碼器</option>
  370. <option value="WaterLevel">保溫夾層水位</option>
  371. <option value="SHT11_Temp">溫度</option>
  372. <option value="SHT11_Humidity">濕度</option>
  373. <option value="CO2">二氧化碳</option>
  374. <option value="PH">水質酸鹼度</option>
  375. <option value="ORP">水質氧化還原電位</option>
  376. <option value="DO">水質溶氧量</option>
  377. <option value="EC">水質導電度</option>
  378. <option value="PA" selected>大氣壓力</option>
  379. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  380. </select>
  381. {% else %}
  382. <select id="sensors">
  383. <option value="UltraSonic" selected>咖啡生豆高度</option>
  384. <option value="motorEncoder">馬達編碼器</option>
  385. <option value="WaterLevel">保溫夾層水位</option>
  386. <option value="SHT11_Temp">溫度</option>
  387. <option value="SHT11_Humidity">濕度</option>
  388. <option value="CO2">二氧化碳</option>
  389. <option value="PH">水質酸鹼度</option>
  390. <option value="ORP">水質氧化還原電位</option>
  391. <option value="DO">水質溶氧量</option>
  392. <option value="EC">水質導電度</option>
  393. <option value="PA">大氣壓力</option>
  394. <option value="butterflyvalve">蝴蝶閥編碼器</option>
  395. </select>
  396. {% endif %}
  397. </td>
  398. </tr>
  399. <tr>
  400. <td style="border:1px gray solid;">
  401. 時間間隔:
  402. <select name="time-interval" class="text-right">
  403. <option value="month">逐月</option>
  404. <option value="day">逐日</option>
  405. <option value="hour">逐時</option>
  406. </select>
  407. &nbsp;
  408. <!--
  409. 從&nbsp;
  410. <input type="datetime-local" name="sensor_starttime" id="sensor_starttime" value={{starttime}}>
  411. &nbsp;到&nbsp;
  412. <input type="datetime-local" name="sensor_endtime" id="sensor_endtime" value={{endtime}}>
  413. &nbsp;為止
  414. -->
  415. <span>從</span>
  416. <label for="querydate-start"></label><input id="querydate-start" type="date">
  417. <span>到</span>
  418. <label for="querydate-end"></label><input id="querydate-end" type="date">
  419. <span>為止</span>
  420. </td>
  421. </tr>
  422. <tr>
  423. <td style="border:1px gray solid;">
  424. 資料類型:
  425. <label><input type="checkbox" name="data-type" value="max">最大值</label>
  426. <label><input type="checkbox" name="data-type" value="avg">平均值</label>
  427. <label><input type="checkbox" name="data-type" value="min">最小值</label>
  428. <br>
  429. </td>
  430. </tr>
  431. <tr>
  432. <td style="border:1px gray solid;">
  433. <!--
  434. <button type="submit" name="charting">繪製圖表</button>
  435. <button type="submit" onclick="" id="btn_save_chart" style="display: none;">儲存圖表</button>
  436. -->
  437. <input type="button" class="btn btn-secondary" value="查詢" onclick="ShowInfo()">
  438. <input type="button" class="btn btn-secondary" value="匯出" onclick="downloadFile()">
  439. <script>
  440. function ShowInfo() {
  441. // 取得桶號、感測器名稱
  442. tid = document.getElementById("tank_num").value
  443. console.log('tid: ' + tid)
  444. sensor_name = document.getElementById("sensors").value
  445. console.log('sensor_name: ' + sensor_name)
  446. // 數據整理
  447. Echart(sensor_name, tid)
  448. // 顯示圖表
  449. };
  450. function downloadFile() {
  451. tid = document.getElementById("tank_num").value
  452. sensor_name = document.getElementById("sensors").value
  453. //藉型別陣列建構的 blob 來建立 URL
  454. let fileName = "ChartData_" + tid + "_" + sensor_name + ".csv";
  455. //"\ufeff"解決打開CSV中文亂碼問題
  456. const data = "\ufeff" + getData();
  457. let blob = new Blob([data], {
  458. type: 'text/csv,charset=UTF-8'
  459. });
  460. var href = URL.createObjectURL(blob);
  461. // 從 Blob 取出資料
  462. var link = document.createElement("a");
  463. document.body.appendChild(link);
  464. link.href = href;
  465. link.download = fileName;
  466. link.click();
  467. };
  468. //所有資料函數
  469. function getData() {
  470. var header = "日期," + data_name + "\n";
  471. var data = "";
  472. var length = all_datetime.length;
  473. for (var i = 0; i < length; i++) {
  474. data = data + all_datetime[i] + ',' + all_data[i] + '\n';
  475. };
  476. return header + data;
  477. };
  478. //最大最小平均資料函數
  479. function getData() {
  480. var header = data_name + "\n日期,";
  481. if (max_data) {
  482. console.log(max_data);
  483. header = header + "最大值,";
  484. };
  485. if (avg_data) {
  486. header = header + "平均值,";
  487. };
  488. if (min_data) {
  489. header = header + "最小值,";
  490. };
  491. header = header + "\n";
  492. var data = "";
  493. var length = xAxis_data.length;
  494. for (var i = 0; i < length; i++) {
  495. data = data + xAxis_data[i] + ',';
  496. if (max_data) {
  497. data = data + max_data[i] + ',';
  498. };
  499. if (avg_data) {
  500. data = data + avg_data[i] + ',';
  501. };
  502. if (min_data) {
  503. data = data + min_data[i] + ',';
  504. };
  505. data = data + '\n';
  506. };
  507. return header + data;
  508. };
  509. </script>
  510. </td>
  511. </tr>
  512. <tr>
  513. <td>
  514. <!--
  515. <img src={{ url }}>
  516. -->
  517. <div id="" class="show-info" style="text-align: center;">
  518. <center>
  519. <div class="show-chart" style="height: 420px; width: 100%; text-align: center;">
  520. 圖表
  521. </div>
  522. </center>
  523. </div>
  524. </td>
  525. </tr>
  526. </table>
  527. </div>
  528. </div>
  529. </div>
  530. </div>
  531. </div>
  532. </div>
  533. </div>
  534. {% endblock %}
  535. {% block script2 %}
  536. <script>
  537. var all_datetime = 0; // 所有資料時間 ?
  538. var all_data = 0; // 所有感測器數據 ?
  539. var data_name = 0; // 感測器名稱 // "溫溼度"
  540. var xAxis_data = 0; // x 軸資料
  541. var max_data = 0; //
  542. var min_data = 0; //
  543. var avg_data = 0; //
  544. function Echart(sensor_name, tid) {
  545. // Echart(sensor_name, tid)
  546. //將echarts圖形銷毀
  547. $(".show-chart").removeAttr("_echarts_instance_").empty();
  548. // Rita:sensor 與 data_name 重複
  549. // var sensor = evt.substring(2);
  550. if (!$("input[value=avg]").prop('checked') && !$("input[value=min]").prop('checked') && !$("input[value=max]").prop('checked')) {
  551. alert("請至少選擇一種資料類型!");
  552. return false;
  553. };
  554. if ($("#querydate-start").val() == '' || $("#querydate-end").val() == '') {
  555. alert("請選擇日期範圍!");
  556. return false;
  557. };
  558. //基於準備好的dom,初始化echarts例項
  559. var myChart = echarts.init(document.getElementsByClassName('show-chart')[0]); // class="col-10 show-chart"
  560. var date_start = $("#querydate-start").val();
  561. var date_end = $("#querydate-end").val();
  562. var avg = 0;
  563. var max = 0;
  564. var min = 0;
  565. var time_interval = $("select[name=time-interval]").val();
  566. var legend = new Array();
  567. var color = new Array();
  568. var series = new Array();
  569. if ($("input[value=max]").prop('checked')) {
  570. max = 1;
  571. legend.push("最大值");
  572. color.push("#1e88e5"); // 水藍色
  573. };
  574. if ($("input[value=avg]").prop('checked')) {
  575. avg = 1;
  576. legend.push("平均值");
  577. color.push("#43a047"); // 草綠色
  578. };
  579. if ($("input[value=min]").prop('checked')) {
  580. min = 1;
  581. legend.push("最小值");
  582. color.push("#e64a19"); // 橘紅色
  583. };
  584. var json = {
  585. 'tid': tid,
  586. 'sensor_name': sensor_name,
  587. 'avg': avg,
  588. 'max': max,
  589. 'min': min,
  590. 'time-interval': time_interval,
  591. 'date-start': date_start,
  592. 'date-end': date_end
  593. };
  594. console.log('json___' +
  595. 'tid: ' + tid +
  596. 'sensor_name: ' + sensor_name +
  597. 'avg: ' + avg +
  598. 'max: ' + max +
  599. 'min: ' + min +
  600. 'time-interval: ' + time_interval +
  601. 'date-start: ' + date_start +
  602. 'date-end: ' + date_end
  603. )
  604. $.get('/history_data_new', json, function (resText) {
  605. if (resText.max) {
  606. var max_series = {
  607. 'name': '最大值',
  608. 'type': 'scatter',
  609. 'data': resText.max.map(function (item) { return item[1]; }),
  610. 'itemStyle': { 'normal': { 'lineStyle': { 'color': '#1e88e5' } } },
  611. };
  612. series.push(max_series); // 把上面的 dict 加入 series array 內
  613. xAxis_data = resText.max.map(function (item) { return item[0]; });
  614. max_data = resText.max.map(function (item) { return item[1]; });
  615. };
  616. if (resText.avg) {
  617. var avg_series = { 'name': '平均值', 'type': 'scatter', 'data': resText.avg.map(function (item) { return item[1]; }), 'itemStyle': { 'normal': { 'lineStyle': { 'color': '#43a047' } } }, };
  618. series.push(avg_series);
  619. xAxis_data = resText.avg.map(function (item) { return item[0]; });
  620. 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
  621. avg_data = resText.avg.map(function (item) { return item[1]; });
  622. console.log('avg_data: ' + avg_data) // 91.0,98.0,11.0,15.2,30.5
  623. };
  624. if (resText.min) {
  625. var min_series = { 'name': '最小值', 'type': 'scatter', 'data': resText.min.map(function (item) { return item[1]; }), 'itemStyle': { 'normal': { 'lineStyle': { 'color': '#e64a19' } } }, };
  626. series.push(min_series);
  627. xAxis_data = resText.min.map(function (item) { return item[0]; }); // all
  628. min_data = resText.min.map(function (item) { return item[1]; }); // min
  629. };
  630. //獲取日期內所有數據
  631. all_datetime = resText.all.map(function (item) { return item[0]; }); // all
  632. all_data = resText.all.map(function (item) { return item[1]; });
  633. //指定圖表的配置項和資料
  634. option = {
  635. title: {
  636. text: '', // 'Data History'
  637. left: '1%'
  638. },
  639. tooltip: {
  640. trigger: 'axis'
  641. },
  642. color: color,
  643. legend: {
  644. data: legend,
  645. },
  646. xAxis: {
  647. data: xAxis_data,
  648. },
  649. yAxis: {
  650. splitLine: {
  651. show: true,
  652. lineStyle: {
  653. color: ''
  654. }
  655. }
  656. },
  657. toolbox: {
  658. show: true,
  659. feature: {
  660. saveAsImage: {}
  661. }
  662. },
  663. dataZoom: [{
  664. startValue: '2021-01-01'
  665. }, {
  666. type: 'inside'
  667. }],
  668. series: series,
  669. /*
  670. series: [
  671. {
  672. name: '最大值',
  673. type: 'line',
  674. data: resText.max.map(function (item) {
  675. return item[1];
  676. }),
  677. itemStyle: {
  678. normal: {
  679. lineStyle: {
  680. color: '#1e88e5'
  681. }
  682. }
  683. },
  684. },
  685. {
  686. name: '平均值',
  687. type: 'line',
  688. data: [20.0, 20.0, 20.0, 20.0, 20.0, 20.0],
  689. itemStyle: {
  690. normal: {
  691. lineStyle: {
  692. color: '#43a047'
  693. }
  694. }
  695. },
  696. },
  697. {
  698. name: '最小值',
  699. type: 'line',
  700. data: [15.0, 15.0, 15.0, 15.0, 15.0, 15.0],
  701. itemStyle: {
  702. normal: {
  703. lineStyle: {
  704. color: '#e64a19'
  705. }
  706. }
  707. }
  708. },
  709. ]
  710. */
  711. };
  712. console.log('SHOW myChart')
  713. //使用剛指定的配置項和資料顯示圖表
  714. myChart.setOption(option);
  715. window.addEventListener('resize', myChart.resize);
  716. }, 'json');
  717. } // Echart(sensor_name, tid)
  718. </script>
  719. {% endblock %}