sensor_chart_D.html 35 KB

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