text-to-chart.js 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  1. console.log('text-to-chart');
  2. var accessToken = localStorage.getItem("access_token");
  3. console.log(accessToken);
  4. if (accessToken === null) {
  5. alert('請先登入')
  6. window.location.href = "./login.html";
  7. }
  8. var usernameEmail = localStorage.getItem("username");
  9. var unit_table_value
  10. var obj = {
  11. _text: ''
  12. };
  13. // let username = usernameEmail.split('@')[0];
  14. Chart.register(ChartDataLabels);
  15. var userNameSpan = document.getElementById("userName");
  16. var lineYfontSize = document.getElementById("lineYfontSize");
  17. userNameSpan.textContent = usernameEmail;
  18. var inputField = document.getElementById("keyword_data");
  19. var inputField_compare = document.getElementById("keyword_data_compare");
  20. var compare_box = document.getElementById("compare");
  21. var sendButton = document.getElementById("send_data");
  22. var send_data_compare = document.getElementById("data_compare");
  23. var canvasPng = document.getElementById("textToChart");
  24. var img_box = document.getElementById("img_box");
  25. var sidebar = document.getElementById('style_setting');
  26. var input_text_value;
  27. var chartType = 'line';
  28. var unit = '';
  29. var myChart = null;
  30. var chartColor = '#912B2B';
  31. var chartColorBar = '#'
  32. var displayXaxes = true;
  33. var ctx;
  34. var chartlabels;
  35. var chartdata;
  36. var XfontSizeValue = 18;
  37. var YfontSizeValue = 18;
  38. var TitlefontSizeValue = 32;
  39. var pieFontSize = 14;
  40. var PieMaxWidth = 100;
  41. var pointRadiusValue = 0;
  42. var pointHoverRadiusValue = 0;
  43. var chart_bgimg_url = "url(./img/bg06.webp)";
  44. var vocab = [];
  45. var chartDiv = document.getElementById('chartdiv');
  46. var dataFirstValue;
  47. var dataLastValue;
  48. let cancelToken;
  49. let source;
  50. $('#chartdiv').hide();
  51. var clickExample = document.getElementById('example_box')
  52. document.addEventListener('DOMContentLoaded', function () {
  53. axios.get("https://cmm.ai:8080/vocab")
  54. .then(response => {
  55. // apiHideLoading();
  56. console.log(response);
  57. vocab = response.data;
  58. })
  59. .catch(error => {
  60. console.error('發生錯誤:', error);
  61. });
  62. });
  63. inputField.addEventListener('click', function () {
  64. // 確保輸入框處於焦點狀態
  65. inputField.focus();
  66. // 在控制台中顯示焦點狀態
  67. console.log('Input field focused');
  68. });
  69. document.addEventListener('DOMContentLoaded', function () {
  70. // const input = document.getElementById('stockInput');
  71. const suggestions = document.getElementById('suggestions');
  72. inputField.addEventListener('input', function () {
  73. const query = this.value.trim();
  74. suggestions.innerHTML = ''; // 清空之前的建議
  75. console.log('query', query);
  76. if (query) {
  77. // const filteredVocab = vocab.filter(item => item.toLowerCase().includes(query));
  78. const queryTerms = query.split(/\s+/); // 分割輸入的內容
  79. const lastQueryTerm = queryTerms[queryTerms.length - 1];
  80. const filteredVocab = vocab.filter(item => {
  81. // 對每個分割後的單詞進行匹配,只要其中有一個匹配成功就返回 true
  82. return item.toLowerCase().includes(lastQueryTerm.toLowerCase());
  83. });
  84. if (filteredVocab.length > 0) {
  85. suggestions.style.border = '1px solid #ccc';
  86. suggestions.style.display = 'block'; // 顯示建議容器
  87. } else {
  88. suggestions.style.border = 'none';
  89. suggestions.style.display = 'none'; // 隱藏建議容器
  90. }
  91. console.log(queryTerms[queryTerms.length - 1])
  92. filteredVocab.forEach(item => {
  93. const div = document.createElement('div');
  94. div.classList.add('autocomplete-suggestion');
  95. div.textContent = item;
  96. // div.addEventListener('click', function () {
  97. // inputField.value = item;
  98. // suggestions.innerHTML = ''; // 清空建議
  99. // suggestions.style.border = 'none';
  100. // suggestions.style.display = 'none'; // 隱藏建議容器
  101. // });
  102. // console.log('queryTerms', queryTerms);
  103. // console.log('filteredVocab', filteredVocab);
  104. div.addEventListener('click', function () {
  105. queryTerms.forEach((term, index) => {
  106. const regex = new RegExp(term, 'gi');
  107. const match = regex.exec(inputField.value);
  108. // console.log(queryTerms[queryTerms.length - 1]);
  109. // console.log(filteredVocab, queryTerms[queryTerms.length - 1])
  110. if (index === queryTerms.length - 1) {
  111. if (match) {
  112. var start = match.index;
  113. var end = start + match[0].length;
  114. console.log(start, end)
  115. const textBefore = inputField.value.substring(0, start);
  116. const textAfter = inputField.value.substring(end, inputField.value.length);
  117. console.log('start', start, 'end', end, 'textBefore', textBefore, 'textAfter', textAfter);
  118. // 捕獲被選中的部分並替換它
  119. const selectedText = inputField.value.substring(start, end);
  120. console.log('selectedText', selectedText);
  121. const newText = textBefore + item + textAfter.replace(selectedText, '');
  122. console.log('newText', newText);
  123. inputField.value = newText;
  124. // inputField.value = textBefore + item.replace(selectedText, '') + textAfter;
  125. suggestions.innerHTML = ''; // 清空建議
  126. suggestions.style.border = 'none';
  127. }
  128. }
  129. });
  130. // const start = inputField.selectionStart;
  131. // const end = inputField.selectionEnd;
  132. });
  133. suggestions.appendChild(div);
  134. });
  135. } else {
  136. suggestions.style.border = 'none';
  137. suggestions.style.display = 'none'; // 隱藏建議容器
  138. }
  139. });
  140. });
  141. document.addEventListener('DOMContentLoaded', function () {
  142. // const input = document.getElementById('stockInput');
  143. const suggestions_compare = document.getElementById('suggestions_compare');
  144. inputField_compare.addEventListener('input', function () {
  145. const query = this.value.trim();
  146. suggestions_compare.innerHTML = ''; // 清空之前的建議
  147. console.log('query', query);
  148. if (query) {
  149. // const filteredVocab = vocab.filter(item => item.toLowerCase().includes(query));
  150. const queryTerms = query.split(/\s+/); // 分割輸入的內容
  151. const lastQueryTerm = queryTerms[queryTerms.length - 1];
  152. const filteredVocab = vocab.filter(item => {
  153. // 對每個分割後的單詞進行匹配,只要其中有一個匹配成功就返回 true
  154. return item.toLowerCase().includes(lastQueryTerm.toLowerCase());
  155. });
  156. if (filteredVocab.length > 0) {
  157. suggestions_compare.style.border = '1px solid #ccc';
  158. suggestions_compare.style.display = 'block'; // 顯示建議容器
  159. } else {
  160. suggestions_compare.style.border = 'none';
  161. suggestions_compare.style.display = 'none'; // 隱藏建議容器
  162. }
  163. console.log(queryTerms[queryTerms.length - 1])
  164. filteredVocab.forEach(item => {
  165. const div = document.createElement('div');
  166. div.classList.add('autocomplete-suggestion');
  167. div.textContent = item;
  168. // div.addEventListener('click', function () {
  169. // inputField.value = item;
  170. // suggestions.innerHTML = ''; // 清空建議
  171. // suggestions.style.border = 'none';
  172. // suggestions.style.display = 'none'; // 隱藏建議容器
  173. // });
  174. // console.log('queryTerms', queryTerms);
  175. // console.log('filteredVocab', filteredVocab);
  176. div.addEventListener('click', function () {
  177. queryTerms.forEach((term, index) => {
  178. const regex = new RegExp(term, 'gi');
  179. const match = regex.exec(inputField_compare.value);
  180. // console.log(queryTerms[queryTerms.length - 1]);
  181. // console.log(filteredVocab, queryTerms[queryTerms.length - 1])
  182. if (index === queryTerms.length - 1) {
  183. if (match) {
  184. var start = match.index;
  185. var end = start + match[0].length;
  186. console.log(start, end)
  187. const textBefore = inputField_compare.value.substring(0, start);
  188. const textAfter = inputField_compare.value.substring(end, inputField_compare.value.length);
  189. console.log('start', start, 'end', end, 'textBefore', textBefore, 'textAfter', textAfter);
  190. // 捕獲被選中的部分並替換它
  191. const selectedText = inputField_compare.value.substring(start, end);
  192. console.log('selectedText', selectedText);
  193. const newText = textBefore + item + textAfter.replace(selectedText, '');
  194. console.log('newText', newText);
  195. inputField_compare.value = newText;
  196. // inputField.value = textBefore + item.replace(selectedText, '') + textAfter;
  197. suggestions_compare.innerHTML = ''; // 清空建議
  198. suggestions_compare.style.border = 'none';
  199. }
  200. }
  201. });
  202. // const start = inputField.selectionStart;
  203. // const end = inputField.selectionEnd;
  204. });
  205. suggestions_compare.appendChild(div);
  206. });
  207. } else {
  208. suggestions_compare.style.border = 'none';
  209. suggestions_compare.style.display = 'none'; // 隱藏建議容器
  210. }
  211. });
  212. });
  213. // 監聽輸入框的鍵盤事件
  214. // document.getElementById("keyword_data").addEventListener("keyup", function (event) {
  215. // // 判斷是否按下 Enter 鍵 (key code: 13)
  216. // if (event.keyCode === 13) {
  217. // // 觸發送出按鈕的點擊事件
  218. // document.getElementById("send_data").click();
  219. // }
  220. // });
  221. // 定義按鈕點擊事件處理函數
  222. function sendButtonClickHandler() {
  223. // inputField.value = "";
  224. console.log(inputField.value);
  225. var input_text_value = inputField.value;
  226. compare_box.style.display = "none";
  227. $('#chartdiv').hide();
  228. sidebar.classList.remove('show');
  229. sidebar.classList.add('hidden');
  230. // 在這裡添加你希望在按下按鈕時執行的其他代碼
  231. get_data(input_text_value);
  232. }
  233. sendButton.addEventListener("click", function () {
  234. sendButtonClickHandler();
  235. inputField.value = ''
  236. chartTypeBtn.style.display = "none";
  237. });
  238. let labels2 = [];
  239. let data2 = [];
  240. // 漲幅or單價
  241. const checkbox1 = document.getElementById('checkbox1');
  242. const checkbox2 = document.getElementById('checkbox2');
  243. var compare_type_input = document.querySelector('.checkbox-group')
  244. var compare_type = "";
  245. checkbox1.addEventListener('change', () => {
  246. if (checkbox1.checked) {
  247. checkbox2.checked = false;
  248. compare_type = '漲幅'
  249. console.log(compare_type);
  250. }
  251. });
  252. checkbox2.addEventListener('change', () => {
  253. if (checkbox2.checked) {
  254. checkbox1.checked = false;
  255. compare_type = '單價'
  256. console.log(compare_type);
  257. }
  258. });
  259. send_data_compare.addEventListener("click", function () {
  260. console.log('比較', inputField_compare.value);
  261. const stockName = inputField_compare.value;
  262. axios
  263. .get(`https://cmm.ai:8080/get_data_from_date?start_date=${dataFirstValue}&end_date=${dataLastValue}&stock_name=${inputField_compare.value}`)
  264. .then((response) => {
  265. console.log(response);
  266. var compare_data = response.data.data;
  267. labels2 = []; // Clear previous labels
  268. data2 = []; // Clear previous data
  269. if (compare_type === "") {
  270. alert('請輸入比較類型');
  271. return
  272. }
  273. // console.log('比較送出後 ', chartType)
  274. if (response.data.data === "無法產生圖表") {
  275. alert('無法產生圖表')
  276. return
  277. }
  278. if (chartType === 'line') {
  279. const colors = [
  280. '#82a33f', // rgba already
  281. '#2f487b', // rgba already
  282. '#df8c49', // hex #df8c49
  283. '#72598f', // hex #72598f
  284. '#489fb6', // hex #489fb6
  285. '#99b0d5' // hex #99b0d5
  286. ];
  287. let colorIndex = 0;
  288. for (var i = 0; i < compare_data.length; i++) {
  289. var item = compare_data[i];
  290. // 遍历当前项的属性
  291. for (var key in item) {
  292. // 如果属性名不在 labels 数组中,并且属性值不是对象,则将属性名添加到 labels 数组中
  293. if (typeof item[key] === 'string') {
  294. labels2.push(item[key]);
  295. }
  296. // 如果属性值是数值类型,则将其添加到 data 数组中
  297. if (typeof item[key] === 'number') {
  298. data2.push(item[key]);
  299. }
  300. }
  301. }
  302. function getRandomColor() {
  303. const randomIndex = Math.floor(Math.random() * colors.length);
  304. const selectedColor = colors[randomIndex];
  305. colors.splice(randomIndex, 1); // 从数组中移除已选颜色
  306. return selectedColor;
  307. }
  308. const normalize = (prices) => {
  309. const initialPrice = prices[0];
  310. // return prices.map(price => (price - initialPrice) / initialPrice * 100);
  311. return prices.map(price => {
  312. // Calculate the normalized price
  313. const normalized = (price - initialPrice) / initialPrice * 100;
  314. // Round to 2 decimal places and convert back to a number
  315. return parseFloat(normalized.toFixed(2));
  316. });
  317. };
  318. var stock2Normalized;
  319. var stock1Normalized;
  320. if (compare_type === '漲幅') {
  321. console.log('漲幅')
  322. stock2Normalized = normalize(data2);
  323. stock1Normalized = normalize(data);
  324. unitInput.value = '%';
  325. unit_value.textContent = '%'
  326. } else {
  327. console.log('單價')
  328. unitInput.value = unitInput.value;
  329. unit_value.textContent = unitInput.value;
  330. stock2Normalized = data2;
  331. stock1Normalized = data;
  332. }
  333. const color = getRandomColor();
  334. if (response.data.data.length < 15) {
  335. pointRadiusValue = 5;
  336. pointHoverRadiusValue = 8;
  337. togglePoints.checked = true;
  338. }
  339. const newDataset = {
  340. label: stockName,
  341. backgroundColor: color,
  342. pointRadius: pointRadiusValue,
  343. pointHoverRadius: 0,
  344. pointBorderColor: '#fff',
  345. pointBorderWidth: 3,
  346. pointHoverRadiusValue: 8,
  347. borderColor: color,
  348. data: stock2Normalized,
  349. fill: false
  350. };
  351. colorIndex = (colorIndex + 1) % colors.length;
  352. myChart.data.datasets.push(newDataset);
  353. myChart.data.datasets[0].data = stock1Normalized;
  354. console.log('多筆資料', myChart.data.datasets);
  355. // console.log('比較', myChart.options.plugins.legend);
  356. myChart.options.plugins.legend.display = true;
  357. // myChart.defaults.global.legend = {
  358. // display: true,
  359. // position: 'top', // 默认图例位置为顶部
  360. // labels: {
  361. // fontColor: 'rgb(255, 99, 132)', // 默认图例标签颜色
  362. // fontSize: 14 // 默认图例标签字体大小
  363. // }
  364. // };
  365. myChart.update();
  366. inputField_compare.value = '';
  367. compare_type_input.style.display = 'none'
  368. } else if (chartType === 'bar') {
  369. }
  370. })
  371. .catch((error) =>
  372. console.log(error)
  373. );
  374. });
  375. // sendButton.addEventListener("click", function () {
  376. // console.log(inputField.value);
  377. // input_text_value = inputField.value;
  378. // // if (myChart) {
  379. // // console.log('已存在')
  380. // // data = [];
  381. // // labels = [];
  382. // // myChart.removePlugin(Chart.pluginService.getPlugin('afterDraw'));
  383. // // myChart.destroy();
  384. // // }
  385. // get_data(input_text_value);
  386. // });
  387. var lastKeyPressTime = 0;
  388. var enterCount = 0;
  389. var ENTER_THRESHOLD = 500; // 設定連續按下 Enter 的時間閾值(毫秒)
  390. inputField.addEventListener("keyup", function (event) {
  391. // 判斷是否按下 Enter 鍵 (key code: 13)
  392. if (event.key === "Enter") {
  393. var currentTime = new Date().getTime();
  394. // 計算與上一次按鍵按下時間的差值
  395. var timeDiff = currentTime - lastKeyPressTime;
  396. lastKeyPressTime = currentTime;
  397. // 如果兩次 Enter 鍵按下的時間差小於閾值,則增加計數器
  398. if (timeDiff <= ENTER_THRESHOLD) {
  399. enterCount++;
  400. // 如果計數器為2,則觸發 API 請求並重置計數器
  401. if (enterCount === 2) {
  402. sendButtonClickHandler();
  403. enterCount = 0;
  404. }
  405. } else {
  406. // 如果時間差大於閾值,則重置計數器
  407. enterCount = 0;
  408. }
  409. } else {
  410. // 如果按下的不是 Enter 鍵,則重置計數器
  411. enterCount = 0;
  412. }
  413. });
  414. function number_format(number, decimals, dec_point, thousands_sep) {
  415. // * example: number_format(1234.56, 2, ',', ' ');
  416. // * return: '1 234,56'
  417. number = (number + '').replace(',', '').replace(' ', '');
  418. var n = !isFinite(+number) ? 0 : +number,
  419. prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
  420. sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
  421. dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
  422. s = '',
  423. toFixedFix = function (n, prec) {
  424. var k = Math.pow(10, prec);
  425. return '' + Math.round(n * k) / k;
  426. };
  427. // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  428. s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  429. if (s[0].length > 3) {
  430. s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  431. }
  432. if ((s[1] || '').length < prec) {
  433. s[1] = s[1] || '';
  434. s[1] += new Array(prec - s[1].length + 1).join('0');
  435. }
  436. return s.join(dec);
  437. }
  438. var labels = [];
  439. var data = [];
  440. function apiLoading() {
  441. document.getElementById('chartDataLoading').style.display = 'block';
  442. }
  443. function apiHideLoading() {
  444. document.getElementById('chartDataLoading').style.display = 'none';
  445. }
  446. const DISPLAY = true;
  447. const BORDER = true;
  448. const CHART_AREA = true;
  449. const TICKS = true;
  450. var labels = [];
  451. var data = [];
  452. var dataArray;
  453. function generateChart(dataArray) {
  454. if (dataArray.length > 10) {
  455. lineYfontSize.style.display = 'none'
  456. } else {
  457. lineYfontSize.style.display = 'block'
  458. }
  459. // 创建一个空数组来存储 labels 和 data
  460. console.log(dataArray)
  461. // 遍历数据数组
  462. for (var i = 0; i < dataArray.length; i++) {
  463. var item = dataArray[i];
  464. // 遍历当前项的属性
  465. for (var key in item) {
  466. // 如果属性名不在 labels 数组中,并且属性值不是对象,则将属性名添加到 labels 数组中
  467. if (typeof item[key] === 'string') {
  468. labels.push(item[key]);
  469. } else {
  470. if (key === 'x') {
  471. labels.push(String(item[key]));
  472. }
  473. }
  474. if (key === 'y') {
  475. data.push(item[key]);
  476. }
  477. // 如果属性值是数值类型,则将其添加到 data 数组中
  478. // if (typeof item[key] === 'number') {
  479. // data.push(item[key]);
  480. // }
  481. }
  482. }
  483. console.log(data);
  484. dataFirstValue = labels[0];
  485. dataLastValue = labels[labels.length - 1]
  486. console.log('dataFirstValue', dataFirstValue);
  487. console.log('dataLastValue', dataLastValue);
  488. if (myChart) {
  489. console.log('已存在')
  490. myChart.destroy();
  491. }
  492. if (chartType === "doughnut") {
  493. // createChartDoughnut(chartType, data, labels);
  494. // createChartDoughnut2(data, labels)
  495. } else {
  496. createChart(chartType, data, labels);
  497. }
  498. // console.log(data);
  499. console.log(labels);
  500. }
  501. var amChartInstance;
  502. function extractAndGenerateChart(dataArray) {
  503. for (var i = 0; i < dataArray.length; i++) {
  504. var item = dataArray[i];
  505. // 遍历当前项的属性
  506. for (var key in item) {
  507. // 如果属性名不在 labels 数组中,并且属性值不是对象,则将属性名添加到 labels 数组中
  508. if (typeof item[key] === 'string') {
  509. labels.push(item[key]);
  510. }
  511. // 如果属性值是数值类型,则将其添加到 data 数组中
  512. if (typeof item[key] === 'number') {
  513. data.push(item[key]);
  514. }
  515. }
  516. }
  517. // console.log('key', key);
  518. generateBarChart(key, dataArray);
  519. }
  520. // 圓餅圖
  521. function generatePieChart(dataArray, pieFontSize, PieMaxWidth) {
  522. compare_box.style.display = "none";
  523. $('#chartdiv').show();
  524. bgImgelement.style.backgroundImage = chart_bgimg_url;
  525. // chartDiv.style.width = "100%"
  526. chartWhitelement.style.background = "rgba(255,255,255,0.5)"
  527. // Themes begin
  528. am4core.useTheme(am4themes_animated);
  529. // Themes end
  530. // Create chart instance
  531. var chart = am4core.create("chartdiv", am4charts.PieChart);
  532. chart.data = dataArray;
  533. chart.innerRadius = am4core.percent(50);
  534. // Add and configure Series
  535. var pieSeries = chart.series.push(new am4charts.PieSeries());
  536. pieSeries.dataFields.value = "y";
  537. pieSeries.dataFields.category = "x";
  538. pieSeries.slices.template.stroke = am4core.color("#fff");
  539. pieSeries.slices.template.strokeWidth = 2;
  540. pieSeries.slices.template.strokeOpacity = 1;
  541. // Configure labels
  542. pieSeries.labels.template.wrap = true;
  543. console.log('PieMaxWidth', PieMaxWidth);
  544. pieSeries.labels.template.maxWidth = PieMaxWidth;
  545. pieSeries.labels.template.truncate = false;
  546. pieSeries.labels.template.fontSize = 14;
  547. if (typeof PieMaxWidth === 'string') {
  548. console.log('The variable is a string.');
  549. } else {
  550. console.log('The variable is a number.');
  551. }
  552. // Configure label text to wrap and show percentages
  553. // pieSeries.labels.template.adapter.add("textOutput", function (text, target) {
  554. // if (target.dataItem && target.dataItem.values.value.percent) {
  555. // return "[font-size: 14px]" + target.dataItem.category + ": " + target.dataItem.values.value.percent.toFixed(1) + "%";
  556. // }
  557. // return text;
  558. // });
  559. pieSeries.labels.template.adapter.add("textOutput", function (text, target) {
  560. if (target.dataItem && target.dataItem.values.value.percent) {
  561. return "[font-size: " + pieFontSize + "px]" + target.dataItem.category + ": " + target.dataItem.values.value.percent.toFixed(1) + "%";
  562. }
  563. return text;
  564. });
  565. pieSeries.colors.list = [
  566. am4core.color('rgb(171, 51, 49)'),
  567. am4core.color('rgb(34, 83, 149)'),
  568. am4core.color('rgb(79, 148, 65)'),
  569. am4core.color('rgb(217, 195, 105)'),
  570. am4core.color('rgb(142, 124, 180)'),
  571. am4core.color('rgb(211, 183, 144)'),
  572. am4core.color('rgb(83, 84, 84)'),
  573. am4core.color('rgb(229, 147, 152)')
  574. ];
  575. // Ensure labels have background
  576. // pieSeries.labels.template.background.fillOpacity = 1;
  577. console.log(pieSeries.labels.template)
  578. // Set label background color to match corresponding slice color
  579. pieSeries.labels.template.adapter.add("background.fill", function (fill, target) {
  580. return target.dataItem.slice.fill;
  581. });
  582. // This creates initial animation
  583. pieSeries.hiddenState.properties.opacity = 1;
  584. pieSeries.hiddenState.properties.endAngle = -90;
  585. pieSeries.hiddenState.properties.startAngle = -90;
  586. // console.log('調位置')
  587. downloadButton.style.display = "inline-block";
  588. chartTypeBtn.style.display = "inline-block";
  589. }
  590. // var colorEven='#288D97';
  591. // var colorOdd='#427D7E'
  592. function adjustColorBrightness(color, amount) {
  593. let usePound = false;
  594. if (color[0] == "#") {
  595. color = color.slice(1);
  596. usePound = true;
  597. }
  598. let num = parseInt(color, 16);
  599. let r = (num >> 16) + amount;
  600. let g = ((num >> 8) & 0x00FF) + amount;
  601. let b = (num & 0x0000FF) + amount;
  602. if (r > 255) r = 255;
  603. else if (r < 0) r = 0;
  604. if (g > 255) g = 255;
  605. else if (g < 0) g = 0;
  606. if (b > 255) b = 255;
  607. else if (b < 0) b = 0;
  608. return (usePound ? "#" : "") + (r << 16 | g << 8 | b).toString(16).padStart(6, '0');
  609. }
  610. // 柱狀圖
  611. function generateBarChart(key, dataArray) {
  612. console.log('generateBarChart', dataArray)
  613. chartTypeBtn.style.display = "inline-block";
  614. compare_box.style.display = "none";
  615. $('#chartdiv').show();
  616. // var colorEven = document.getElementById('colorEven').value;
  617. // var colorOdd = document.getElementById('colorOdd').value;
  618. var baseColor = document.getElementById('borderColorInputBar').value;
  619. var categoryAxisFzValue = document.getElementById('XfontSizeBar').value;
  620. var valueAxisFzValue = document.getElementById('YfontSizeBar').value;
  621. var colorEven = am4core.color(baseColor);
  622. var colorOdd = adjustColorBrightness(baseColor, -20);
  623. chartDiv.style.fontFamily = "Arial, sans-serif";
  624. chartDiv.style.fontWeight = "900";
  625. bgImgelement.style.backgroundImage = chart_bgimg_url;
  626. chartWhitelement.style.background = "rgba(255,255,255,0.5)"
  627. console.log('labelDependent', labelDependent);
  628. // Themes begin
  629. am4core.useTheme(am4themes_animated);
  630. // Themes end
  631. // Create chart instance
  632. var chart = am4core.create("chartdiv", am4charts.XYChart3D);
  633. chart.data = dataArray;
  634. chart.angle = 30; // 圖表角度
  635. chart.depth = 25; // 圖表深度
  636. let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
  637. categoryAxis.dataFields.category = "x";
  638. categoryAxis.renderer.labels.template.rotation = 0;
  639. categoryAxis.renderer.labels.template.hideOversized = false;
  640. categoryAxis.renderer.minGridDistance = 20; // 間距
  641. // categoryAxis.renderer.labels.template.horizontalCenter = "left";
  642. // categoryAxis.renderer.labels.template.verticalCenter = "middle";
  643. // categoryAxis.tooltip.label.rotation = 90;
  644. // categoryAxis.tooltip.label.horizontalCenter = "right";
  645. // categoryAxis.tooltip.label.verticalCenter = "middle";
  646. // label 換行
  647. let label = categoryAxis.renderer.labels.template;
  648. label.wrap = true;
  649. if (dataArray.length > 5) {
  650. console.log('資料長度5')
  651. label.maxWidth = 60;
  652. } else if (dataArray.length < 7) {
  653. label.maxWidth = 70;
  654. } else {
  655. label.maxWidth = 100;
  656. console.log('資料長度<5')
  657. }
  658. // x軸字體大小
  659. categoryAxis.renderer.labels.template.fontSize = categoryAxisFzValue;
  660. categoryAxis.renderer.cellEndLocation = 0.5; // 減少值可以增加間距
  661. let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
  662. valueAxis.title.text = labelDependent; // Y軸
  663. valueAxis.title.fontWeight = "bold";
  664. // y軸字體大小
  665. valueAxis.renderer.labels.template.fontSize = valueAxisFzValue;
  666. // Create series
  667. var series = chart.series.push(new am4charts.ColumnSeries3D());
  668. series.dataFields.valueY = 'y';
  669. series.dataFields.categoryX = "x";
  670. series.name = "y";
  671. series.tooltipText = "{categoryX}: [bold]{valueY}[/]";
  672. series.columns.template.fillOpacity = .8;
  673. let columnTemplate = series.columns.template;
  674. columnTemplate.strokeWidth = 2;
  675. columnTemplate.strokeOpacity = 1;
  676. columnTemplate.stroke = am4core.color("#FFFFFF");
  677. // columnTemplate.adapter.add("fill", function (fill, target) {
  678. // return chart.colors.getIndex(target.dataItem.index);
  679. // })
  680. // 設置基數和偶數顏色
  681. columnTemplate.adapter.add("fill", function (fill, target) {
  682. return target.dataItem.index % 2 === 0 ? am4core.color(colorEven) : am4core.color(colorOdd);
  683. });
  684. columnTemplate.adapter.add("stroke", function (stroke, target) {
  685. return chart.colors.getIndex(target.dataItem.index);
  686. })
  687. // // 加上白色線條
  688. // series.columns.template.events.on("validated", function (event) {
  689. // let column = event.target;
  690. // let whiteLine = column.createChild(am4core.Line);
  691. // whiteLine.stroke = am4core.color("#ffffff");
  692. // whiteLine.strokeWidth = 1.3; // 線條寬度
  693. // whiteLine.strokeOpacity = 0.5;// 透明度
  694. // whiteLine.x1 = column.pixelWidth / 1;
  695. // whiteLine.y1 = 0;
  696. // whiteLine.x2 = column.pixelWidth / 1;
  697. // whiteLine.y2 = column.pixelHeight;
  698. // });
  699. chart.cursor = new am4charts.XYCursor();
  700. chart.cursor.lineX.strokeOpacity = 0;
  701. chart.cursor.lineY.strokeOpacity = 0;
  702. categoryAxis.renderer.grid.template.strokeOpacity = 0; // 隱藏 X 軸
  703. // valueAxis.renderer.grid.template.strokeOpacity = 0; // 隱藏 Y 軸
  704. // valueAxis.renderer.line.strokeOpacity = 0;
  705. // LineSeries 底部線條
  706. var lineSeries = chart.series.push(new am4charts.LineSeries());
  707. lineSeries.dataFields.valueY = "bottom";
  708. lineSeries.dataFields.categoryX = "0";
  709. lineSeries.stroke = am4core.color("#FFFFFF")
  710. lineSeries.strokeWidth = 2;
  711. lineSeries.strokeOpacity = 1
  712. var bottomData = [];
  713. for (var i = 0; i < dataArray.length; i++) {
  714. bottomData.push({ "x": dataArray[i].x, "bottom": 0 });
  715. }
  716. lineSeries.data = bottomData;
  717. downloadButton.style.display = "inline-block";
  718. chartTypeBtn.style.display = "inline-block";
  719. chartTypeBtn.style.opacity = "1";
  720. // 底部線條
  721. // let rectElements = document.querySelectorAll("rect");
  722. // // let widthValue = 0;
  723. // let fifthRectElement = rectElements[3];
  724. // // fifthRectElement.style.position = "relative";
  725. // setTimeout(() => {
  726. // let width = fifthRectElement.getAttribute("width");
  727. // // widthValue = parseFloat(width);
  728. // console.log('rectElements', rectElements);
  729. // console.log('fifthRectElement', fifthRectElement);
  730. // console.log('width', width);
  731. // let line = document.createElement("div");
  732. // line.style.position = "absolute";
  733. // line.style.bottom = "162px";
  734. // line.style.right = "40px";
  735. // line.style.width = `${parseFloat(width)}px`;
  736. // line.style.height = "2px";
  737. // line.style.backgroundColor = "#000000";
  738. // document.getElementById("chartdiv").appendChild(line);
  739. // }, 10)
  740. // end am4core.ready()
  741. }
  742. function handleClick(element) {
  743. const Clickvalue = element.dataset.value;
  744. console.log(Clickvalue); // 123
  745. get_data(Clickvalue);
  746. }
  747. // 圖表類型
  748. var chartTypeBtn = document.getElementById("chartType");
  749. chartTypeBtn.addEventListener('change', function () {
  750. const selectedValue = this.value;
  751. console.log('選擇的圖表類型:', selectedValue);
  752. obj.text = selectedValue;
  753. // 在此处添加处理选定图表类型的代码
  754. if (selectedValue === 'line') {
  755. // 处理折线图
  756. console.log('處理折線圖', dataArray);
  757. $('#chartdiv').hide();
  758. chartType = 'line';
  759. if (myChart) {
  760. data = [];
  761. labels = [];
  762. myChart.destroy();
  763. }
  764. sidebar.classList.add('hidden');
  765. generateChart(dataArray);
  766. } else if (selectedValue === 'bar') {
  767. chartType = 'bar';
  768. $('#textToChart').hide();
  769. // 处理柱状图
  770. console.log('處理柱狀圖', dataArray);
  771. sidebar.classList.add('hidden');
  772. extractAndGenerateChart(dataArray);
  773. } else if (selectedValue === 'doughnut') {
  774. chartType = 'doughnut';
  775. console.log('處理圓餅圖', dataArray);
  776. unit_value.textContent = "";
  777. $('#textToChart').hide();
  778. sidebar.classList.add('hidden');
  779. generatePieChart(dataArray, pieFontSize, PieMaxWidth)
  780. // 处理圆饼图
  781. }
  782. });
  783. var downloadButton = document.getElementById("downloadButton");
  784. // downloadButton.style.display = 'none';
  785. // 添加點擊事件監聽器
  786. downloadButton.addEventListener('click', function () {
  787. setTimeout(function () {
  788. // html2canvas(document.getElementById('data_chart_box')).then(function (canvas) {
  789. // // // 獲取圖片資料 URL
  790. // // var imageData = canvasPng.toDataURL("image/png");
  791. // // // 創建下載連結
  792. // // var link = document.createElement('a');
  793. // // link.download = 'chart.png'; // 下載的檔案名稱
  794. // // link.href = imageData; // 圖片資料 URL
  795. // // // 模擬點擊下載連結
  796. // // link.click();
  797. // // 創建一個&lt;a&gt;元素
  798. // const link = document.createElement('a');
  799. // // 將 Canvas 轉換為 Data URL
  800. // link.href = canvas.toDataURL('image/png');
  801. // // 設置下載文件名
  802. // link.download = 'chart.png';
  803. // // 模擬點擊下載
  804. // link.click();
  805. // });
  806. html2canvas(document.getElementById('data_chart_box'), {
  807. backgroundColor: null, // 確保背景透明
  808. scale: 2,
  809. useCORS: true,
  810. scrollX: 0,
  811. scrollY: 0
  812. }).then(function (canvas) {
  813. // 創建一個 <a> 元素
  814. const link = document.createElement('a');
  815. // 將 Canvas 轉換為 Data URL
  816. link.href = canvas.toDataURL('image/png');
  817. // 設置下載文件名
  818. link.download = 'chart.png';
  819. // 模擬點擊下載
  820. link.click();
  821. });
  822. }, 1000); // 等待 1 秒钟
  823. });
  824. var pieChartColors = [
  825. 'rgb(171, 51, 49)',
  826. 'rgb(34, 83, 149)',
  827. 'rgb(79, 148, 65)',
  828. 'rgb(217, 195, 105)',
  829. 'rgb(142, 124, 180)',
  830. 'rgb(211, 183, 144)',
  831. 'rgb(83, 84, 84)',
  832. 'rgb(229, 147, 152)'
  833. ];
  834. function createChartDoughnut2(data, labels) {
  835. const chartData = {
  836. type: 'outlabeledPie',
  837. data: {
  838. labels: labels,
  839. datasets: [
  840. {
  841. data: data,
  842. borderWidth: borderWidthValue,
  843. fill: false,
  844. backgroundColor: pieChartColors,
  845. },
  846. ],
  847. },
  848. options: {
  849. plugins: {
  850. legend: false,
  851. outlabels: {
  852. text: '%l %p',
  853. color: 'white',
  854. stretch: 35,
  855. font: {
  856. resizable: true,
  857. family: 'Arial',
  858. size: 48,
  859. weight: 'bold',
  860. style: 'italic',
  861. },
  862. },
  863. },
  864. },
  865. };
  866. const devicePixelRatio = 4;
  867. var chartUrl = `https://quickchart.io/chart?c=${encodeURIComponent(JSON.stringify(chartData))}&devicePixelRatio=${devicePixelRatio}&f=png&fontsize=20`;
  868. // 获取目标 div 元素
  869. const targetDiv = document.getElementById('data_chartJs_box');
  870. // 创建一个 img 元素来显示图表
  871. const chartImg = document.createElement('img');
  872. chartImg.src = chartUrl;
  873. chartImg.classList.add('outlabeled_img');
  874. // 清空目标 div 元素
  875. targetDiv.innerHTML = '';
  876. // 将图表 img 元素插入到目标 div 中
  877. targetDiv.appendChild(chartImg);
  878. console.log(chart_bgimg_url);
  879. bgImgelement.style.backgroundImage = chart_bgimg_url;
  880. chartWhitelement.style.background = "rgba(255,255,255,0.5)"
  881. }
  882. // 版型二源餅圖
  883. // // 创建 Chart.js 图表
  884. function createChartDoughnut(chartType, data, labels) {
  885. $('#textToChart').show();
  886. canvasPng.style.maxWidth = '450px';
  887. canvasPng.style.maxHeight = '450px';
  888. var windowWidth = window.innerWidth || document.documentElement.clientWidth;
  889. var canvasWidth = Math.min(500, windowWidth); // 限制最大宽度为 500px
  890. canvasPng.style.width = canvasWidth + 'px'; // 设置 canvas 宽度
  891. canvasPng.style.marginLeft = 'auto'; // 左外边距自动计算
  892. canvasPng.style.marginRight = 'auto'; // 右外边距自动计算
  893. var ctx = document.getElementById("textToChart").getContext('2d');
  894. myChart = new Chart(ctx, {
  895. type: chartType,
  896. data: {
  897. labels: labels,
  898. datasets: [{
  899. strokeColor: "rgba(220,220,220,1)",
  900. StrokeWidth: 5,
  901. data: data,
  902. borderWidth: borderWidthValue,
  903. pointRadius: 0,
  904. pointBorderColor: '#fff',
  905. pointBorderWidth: 3,
  906. pointHoverRadius: 0,
  907. fill: false,
  908. backgroundColor: pieChartColors,
  909. // barPercentage: 0.5,
  910. // categoryPercentage: 0.5
  911. }]
  912. },
  913. options: {
  914. // responsive: true, // 啟用響應式
  915. maintainAspectRatio: true,
  916. responsive: true,
  917. aspectRatio: 1,
  918. plugins: {
  919. legend: {
  920. display: false,
  921. },
  922. datalabels: {
  923. color: '#fff', // 数据标签的颜色
  924. anchor: 'end', // 数据标签的位置
  925. align: 'start', // 数据标签的对齐方式
  926. font: {
  927. size: 14, // 数据标签的字体大小
  928. },
  929. }
  930. },
  931. scales: {
  932. x: {
  933. display: false, // 显示 x 轴
  934. grid: {
  935. display: false,
  936. lineWidth: 5,
  937. color: '#584B3D'
  938. },
  939. ticks: {
  940. textStrokeColor: '#fff',
  941. textStrokeWidth: 5,
  942. font: {
  943. display: false,
  944. family: 'Arial', // 设置字体
  945. size: XfontSizeValue, // 设置字体大小
  946. weight: 'bold', // 设置字体粗细
  947. fontColor: 'rgba(255,255,255,0.8)', // X-axis font color
  948. shadowColor: 'rgba(0, 0, 0, 0.5)', // Shadow color
  949. shadowBlur: 10, // Shadow blur level
  950. shadowOffsetX: 5, // Horizontal shadow offset
  951. shadowOffsetY: 5
  952. },
  953. },
  954. },
  955. y: {
  956. display: false,
  957. beginAtZero: false,
  958. grid: {
  959. display: false,
  960. lineWidth: 5,
  961. color: '#584B3D'
  962. },
  963. ticks: {
  964. textStrokeColor: '#fff',
  965. textStrokeWidth: 5,
  966. font: {
  967. family: 'Arial', // 設置字體
  968. size: XfontSizeValue, // 設置字體大小
  969. weight: 'bold', // 設置字體粗細
  970. color: 'rgba(255,255,255,0.8)', // Y-axis 字體顏色
  971. shadowColor: 'rgba(0, 0, 0, 0.5)', // 陰影顏色
  972. shadowBlur: 10, // 陰影模糊級別
  973. shadowOffsetX: 5, // 水平陰影偏移
  974. shadowOffsetY: 5 // 垂直陰影偏移
  975. },
  976. }
  977. }
  978. },
  979. layout: {
  980. padding: {
  981. left: 50, // 调整图表左边距
  982. right: 50, // 调整图表右边距
  983. top: 10,
  984. bottom: 10,
  985. }
  986. },
  987. },
  988. });
  989. console.log(chart_bgimg_url);
  990. bgImgelement.style.backgroundImage = chart_bgimg_url;
  991. chartWhitelement.style.background = "rgba(255,255,255,0.5)"
  992. }
  993. var chartJsBox = document.getElementById('data_chartJs_box');
  994. function createTable(dataArray, unit, Label_dependent_variable, Label_independent_variable,) {
  995. $('#chartdiv').hide();
  996. $('#textToChart').hide();
  997. chartTitle.style.padding = "15px 0px 30px 30px";
  998. bgImgelement.style.paddingTop = "10px";
  999. var table = document.createElement('table');
  1000. table.classList.add('table', 'table_template', 'dynamic-table'); // 添加 Bootstrap 的 table 样式,如果你在使用 Bootstrap
  1001. // 创建表头
  1002. var thead = table.createTHead();
  1003. var headerRow = thead.insertRow();
  1004. // Object.keys(dataArray[0]).forEach(function (key) {
  1005. // var header = headerRow.insertCell();
  1006. // header.textContent = key;
  1007. // });
  1008. var yearHeader = headerRow.insertCell();
  1009. yearHeader.textContent = Label_independent_variable;
  1010. var avgPriceHeader = headerRow.insertCell();
  1011. avgPriceHeader.textContent = Label_dependent_variable;
  1012. // 创建表格主体
  1013. var tbody = table.createTBody();
  1014. dataArray.forEach(function (item) {
  1015. console.log(item)
  1016. var row = tbody.insertRow();
  1017. // var cell1 = row.insertCell();
  1018. // cell1.textContent = item.year;
  1019. // cell1.classList.add('year-class'); // 为年份单元格添加类
  1020. // var cell2 = row.insertCell();
  1021. Object.keys(item).forEach(function (key, index) {
  1022. var cell = row.insertCell();
  1023. if (index === 0) { // If it's the first cell, add 'year-class'
  1024. cell.textContent = item[key];
  1025. cell.classList.add('year-class');
  1026. } else { // Otherwise, add 'average-class'
  1027. if (key === 'Average_Close') {
  1028. // cell.textContent = item[key].toFixed(2) + unit;
  1029. cell.innerHTML = item[key].toFixed(2) + '<span class="unit_table">' + unit + '</span>';
  1030. } else {
  1031. // cell.textContent = item[key];
  1032. // cell.textContent = item[key].toFixed(2) + unit;
  1033. cell.innerHTML = item[key].toFixed(2) + '<span class="unit_table">' + unit + '</span>';
  1034. }
  1035. cell.classList.add('average-class');
  1036. }
  1037. });
  1038. // console.log(item.avg_price.toFixed(2))
  1039. // cell.textContent = item[key].toFixed(2) + unit;
  1040. // cell2.classList.add('average-class'); // 为年份单元格添加类
  1041. });
  1042. // 获取包含表格的 div 元素
  1043. bgImgelement.style.backgroundImage = chart_bgimg_url;
  1044. img_box.style.bottom = '60px';
  1045. img_box.style.right = '5px';
  1046. document.getElementById('img_box_url').style.width = "180px";
  1047. // chartWhitelement.style.background = "rgba(255,255,255,0.5)"
  1048. // 将表格添加到指定的 div 元素中
  1049. chartJsBox.appendChild(table);
  1050. unit_table_value = document.querySelectorAll('.unit_table');
  1051. downloadButton.style.display = "inline-block";
  1052. chartTypeBtn.style.display = "inline-block";
  1053. chartTypeBtn.style.opacity = "0";
  1054. }
  1055. function getDatalabelsConfig(dataLength) {
  1056. if (dataLength < 10) {
  1057. return {
  1058. formatter: function (value, context) {
  1059. return value + ' unit'; // Add unit if data length is less than 10
  1060. },
  1061. textStrokeColor: '#fff',
  1062. textStrokeWidth: 5,
  1063. color: '#584B3D',
  1064. font: {
  1065. size: YfontSizeValue,
  1066. },
  1067. anchor: 'end',
  1068. align: 'end',
  1069. };
  1070. } else {
  1071. return null; // No datalabels if data length is 10 or more
  1072. }
  1073. }
  1074. function getDatalabelsConfig(isEnabled) {
  1075. if (!isEnabled) {
  1076. return null;
  1077. }
  1078. return data.length <= 15 ? {
  1079. formatter: function (value, context) {
  1080. return value;
  1081. },
  1082. textStrokeColor: '#fff',
  1083. textStrokeWidth: 5,
  1084. color: '#584B3D',
  1085. font: {
  1086. size: YfontSizeValue,
  1087. },
  1088. anchor: 'end',
  1089. align: 'top'
  1090. } : null;
  1091. }
  1092. // 版型一折線圖
  1093. // // 创建 Chart.js 图表
  1094. function createChart(chartType, data, labels) {
  1095. $('#textToChart').show();
  1096. canvasPng.style.maxWidth = '';
  1097. canvasPng.style.maxHeight = '';
  1098. console.log('繪製圖表', data, labels);
  1099. if (data.length < 15) {
  1100. pointRadiusValue = 5;
  1101. pointHoverRadiusValue = 8;
  1102. togglePoints.checked = true;
  1103. toggleValue.checked = true;
  1104. } else {
  1105. togglePoints.checked = false;
  1106. toggleValue.checked = false;
  1107. }
  1108. const datalabelsConfig = data.length <= 15 ? {
  1109. formatter: function (value, context) {
  1110. // if (context.dataIndex === 0) {
  1111. // return '';
  1112. // }
  1113. // if (typeof value === 'number' && value.toString().includes('.')) {
  1114. // value = value.toFixed(4);
  1115. // }
  1116. // return value;
  1117. return value;
  1118. },
  1119. textStrokeColor: '#fff',
  1120. textStrokeWidth: 5,
  1121. color: '#584B3D',
  1122. font: {
  1123. size: YfontSizeValue,
  1124. },
  1125. anchor: 'end',
  1126. align: 'top'
  1127. } : null;
  1128. ctx = document.getElementById("textToChart");
  1129. myChart = new Chart(ctx, {
  1130. type: chartType,
  1131. data: {
  1132. labels: labels,
  1133. datasets: [{
  1134. label: stocknumber,
  1135. strokeColor: "rgba(220,220,220,1)",
  1136. StrokeWidth: 5,
  1137. data: data,
  1138. borderWidth: borderWidthValue,
  1139. pointRadius: pointRadiusValue,
  1140. pointBorderColor: '#fff',
  1141. pointBorderWidth: 3,
  1142. pointHoverRadius: pointHoverRadiusValue,
  1143. fill: false,
  1144. backgroundColor: chartColor,
  1145. borderColor: chartColor,
  1146. // barPercentage: 0.5,
  1147. // categoryPercentage: 0.5
  1148. }]
  1149. },
  1150. options: {
  1151. // responsive: true, // 啟用響應式
  1152. maintainAspectRatio: false,
  1153. plugins: {
  1154. chart3d: {
  1155. enabled: true, // 啟用 3D 插件
  1156. alpha: 25, // 圖表繞 x 軸的旋轉角度
  1157. beta: 25, // 圖表繞 y 軸的旋轉角度
  1158. depth: 15 // 圖表的深度
  1159. },
  1160. datalabels: datalabelsConfig,
  1161. backgroundColor: 'transparent',
  1162. // datalabels: {
  1163. // formatter: function (value, context) {
  1164. // // 添加单位
  1165. // return value;
  1166. // },
  1167. // textStrokeColor: '#fff',
  1168. // textStrokeWidth: 5,
  1169. // color: '#584B3D',
  1170. // font: {
  1171. // size: YfontSizeValue,
  1172. // },
  1173. // anchor: 'end',
  1174. // align: 'end',
  1175. // },
  1176. // datalabels: null,
  1177. // datalabels: data.length <= 10 ? {
  1178. // formatter: function (value, context) {
  1179. // // 添加单位
  1180. // return value + ' ' + unit;
  1181. // },
  1182. // textStrokeColor: '#fff',
  1183. // textStrokeWidth: 5,
  1184. // color: '#584B3D',
  1185. // font: {
  1186. // size: YfontSizeValue,
  1187. // },
  1188. // anchor: 'end',
  1189. // align: 'end',
  1190. // } : null,
  1191. customLabels: null,
  1192. legend: {
  1193. display: false,
  1194. fullSize: true
  1195. },
  1196. },
  1197. scales: {
  1198. x: {
  1199. display: displayXaxes, // 显示 x 轴
  1200. grid: {
  1201. display: false,
  1202. lineWidth: 5,
  1203. color: '#584B3D'
  1204. },
  1205. ticks: {
  1206. textStrokeColor: '#fff',
  1207. textStrokeWidth: 5,
  1208. font: {
  1209. display: false,
  1210. family: 'Arial', // 设置字体
  1211. size: XfontSizeValue, // 设置字体大小
  1212. weight: 'bold', // 设置字体粗细
  1213. fontColor: 'rgba(255,255,255,0.8)', // X-axis font color
  1214. shadowColor: 'rgba(0, 0, 0, 0.5)', // Shadow color
  1215. shadowBlur: 10, // Shadow blur level
  1216. shadowOffsetX: 5, // Horizontal shadow offset
  1217. shadowOffsetY: 5
  1218. },
  1219. },
  1220. },
  1221. y: {
  1222. display: true,
  1223. beginAtZero: false,
  1224. grid: {
  1225. grid: {
  1226. display: true, // 显示 y 轴网格线
  1227. lineWidth: 1, // 网格线宽度
  1228. color: '#C0C5CC' // 网格线颜色
  1229. },
  1230. },
  1231. ticks: {
  1232. textStrokeColor: '#fff',
  1233. textStrokeWidth: 5,
  1234. font: {
  1235. family: 'Arial', // 設置字體
  1236. size: XfontSizeValue, // 設置字體大小
  1237. weight: 'bold', // 設置字體粗細
  1238. color: 'rgba(255,255,255,0.8)', // Y-axis 字體顏色
  1239. shadowColor: 'rgba(0, 0, 0, 0.5)', // 陰影顏色
  1240. shadowBlur: 10, // 陰影模糊級別
  1241. shadowOffsetX: 5, // 水平陰影偏移
  1242. shadowOffsetY: 5 // 垂直陰影偏移
  1243. },
  1244. }
  1245. }
  1246. },
  1247. layout: {
  1248. padding: {
  1249. left: 50, // 调整图表左边距
  1250. right: 50, // 调整图表右边距
  1251. top: 40,
  1252. bottom: 10,
  1253. }
  1254. },
  1255. onClick: function (event, elements) {
  1256. console.log()
  1257. if (elements.length > 0) {
  1258. const element = elements[0];
  1259. selectedDatasetIndex = element.datasetIndex;
  1260. const currentColor = myChart.data.datasets[selectedDatasetIndex].borderColor;
  1261. const colorValue = currentColor;
  1262. colorInput.value = colorValue;
  1263. // 获取当前线条颜色并更新 colorPicker
  1264. // colorInput.value = rgbToHex(currentColor);
  1265. // console.log('當前顏色', colorValue);
  1266. colorInput.disabled = false; // 启用 colorPicker
  1267. }
  1268. }
  1269. },
  1270. });
  1271. console.log('第一次', myChart.options.legend)
  1272. console.log(chart_bgimg_url);
  1273. bgImgelement.style.backgroundImage = chart_bgimg_url;
  1274. chartWhitelement.style.background = "rgba(255,255,255,0.5)";
  1275. downloadButton.style.display = "inline-block";
  1276. chartTypeBtn.style.display = "inline-block";
  1277. chartTypeBtn.style.opacity = "1";
  1278. compare_box.style.display = "flex";
  1279. }
  1280. // createChart(chartType);
  1281. // createChart(chartType)
  1282. const ChartOptions = document.querySelectorAll('input[name="ChartOptions"]');
  1283. ChartOptions.forEach(button => {
  1284. button.addEventListener('click', function () {
  1285. // console.log(this.value);
  1286. chartType = this.value;
  1287. console.log(chartType);
  1288. if (myChart !== null) {
  1289. myChart.destroy();
  1290. data = [];
  1291. labels = [];
  1292. }
  1293. if (chartType === 'line') {
  1294. $('#chartdiv').hide();
  1295. generateChart(dataArray);
  1296. } else {
  1297. $('#textToChart').hide();
  1298. extractAndGenerateChart(dataArray);
  1299. }
  1300. // createChart(chartType, data, labels);
  1301. });
  1302. });
  1303. // 表格字體
  1304. var fontSizeInput = document.getElementById('table_fontSize');
  1305. // 添加事件监听器
  1306. fontSizeInput.addEventListener('input', function () {
  1307. // 获取输入框的值
  1308. var fontSize = fontSizeInput.value;
  1309. // 获取动态创建的表格元素
  1310. var dynamicTable = document.querySelector('.dynamic-table');
  1311. // 如果表格存在,则调整其字体大小
  1312. if (dynamicTable) {
  1313. dynamicTable.style.fontSize = fontSize + 'px';
  1314. }
  1315. });
  1316. // 圓餅圖字體
  1317. var pieFontSizeInput = document.getElementById('pie_fontSize');
  1318. // 添加事件监听器
  1319. pieFontSizeInput.addEventListener('input', function () {
  1320. // 获取输入框的值
  1321. pieFontSize = pieFontSizeInput.value;
  1322. console.log('圓餅圖字體', pieFontSize);
  1323. if (pieFontSize) {
  1324. generatePieChart(dataArray, pieFontSize, PieMaxWidth);
  1325. } else {
  1326. alert("Please enter a valid font size.");
  1327. }
  1328. // 如果表格存在,则调整其字体大小
  1329. // if (dynamicTable) {
  1330. // dynamicTable.style.fontSize = fontSize + 'px';
  1331. // }
  1332. });
  1333. document.getElementById('PieMaxWidthRange').addEventListener('input', function () {
  1334. PieMaxWidth = Number(document.getElementById('PieMaxWidthRange').value);
  1335. console.log('圓餅圖', PieMaxWidth)
  1336. generatePieChart(dataArray, pieFontSize, PieMaxWidth);
  1337. });
  1338. // 是否顯示point
  1339. var togglePoints = document.getElementById('togglePoints');
  1340. togglePoints.addEventListener('change', function () {
  1341. console.log('checked', "change")
  1342. // 遍历所有数据集,应用相应的pointRadius和pointHoverRadius
  1343. myChart.data.datasets.forEach(function (dataset) {
  1344. if (togglePoints.checked) {
  1345. dataset.pointRadius = 5;
  1346. dataset.pointHoverRadius = 8;
  1347. } else {
  1348. dataset.pointRadius = 0;
  1349. dataset.pointHoverRadius = 0;
  1350. }
  1351. });
  1352. myChart.update();
  1353. });
  1354. // 是否顯示數值
  1355. var toggleValue = document.getElementById('toggleValue');
  1356. let datalabelsConfig = getDatalabelsConfig(toggleValue.checked);
  1357. toggleValue.addEventListener('change', function () {
  1358. console.log('切換數值')
  1359. // 根据复选框状态更新 datalabelsConfig
  1360. datalabelsConfig = getDatalabelsConfig(this.checked);
  1361. // 更新图表
  1362. myChart.options.plugins.datalabels = datalabelsConfig;
  1363. myChart.update();
  1364. });
  1365. // 是否顯示單位
  1366. var toggleUnit = document.getElementById('toggleUnit');
  1367. toggleUnit.addEventListener('change', function () {
  1368. if (this.checked) {
  1369. unit_value_box.style.display = 'block';
  1370. } else {
  1371. unit_value_box.style.display = 'none';
  1372. }
  1373. });
  1374. // 單位
  1375. // =========================
  1376. var unitInput = document.getElementById('unit_data');
  1377. var unit_value = document.querySelector('.unit');
  1378. var unit_value_box = document.querySelector('#unit_box');
  1379. // console.log('單位', unitInput)
  1380. unitInput.addEventListener('input', function () {
  1381. // unitInput.value = unit_value.textContent;
  1382. if (chartType === 'table') {
  1383. // unit_table_value.textContent = this.value;
  1384. unit_table_value.forEach(span => {
  1385. span.textContent = this.value;
  1386. });
  1387. } else {
  1388. unit_value.textContent = this.value;
  1389. }
  1390. });
  1391. // ==========================
  1392. // y軸字體大小-折線圖
  1393. // ==========================
  1394. var YfontSize = document.getElementById('YfontSize');
  1395. YfontSize.addEventListener('input', function () {
  1396. YfontSizeValue = YfontSize.value;
  1397. if (myChart) {
  1398. myChart.destroy();
  1399. }
  1400. createChart(chartType, data, labels);
  1401. });
  1402. // ==========================
  1403. // x軸字體大小-折線圖
  1404. // ==========================
  1405. var XfontSize = document.getElementById('XfontSize');
  1406. XfontSize.addEventListener('input', function () {
  1407. XfontSizeValue = XfontSize.value;
  1408. // console.log(XfontSizeValue)
  1409. // if (myChart) {
  1410. // myChart.destroy();
  1411. // }
  1412. // 修改X轴标签字体大小
  1413. myChart.options.scales.x.ticks.font.size = parseInt(XfontSizeValue);
  1414. myChart.options.scales.y.ticks.font.size = parseInt(XfontSizeValue);
  1415. myChart.update();
  1416. // createChart(chartType, data, labels);
  1417. });
  1418. // ==========================
  1419. // x軸字體大小-柱狀圖
  1420. // ==========================
  1421. var XfontSizeBar = document.getElementById('XfontSizeBar');
  1422. XfontSizeBar.addEventListener('input', function () {
  1423. extractAndGenerateChart(dataArray);
  1424. });
  1425. // ==========================
  1426. // y軸字體大小-柱狀圖
  1427. // ==========================
  1428. var YfontSizeBar = document.getElementById('YfontSizeBar');
  1429. YfontSizeBar.addEventListener('input', function () {
  1430. extractAndGenerateChart(dataArray);
  1431. });
  1432. // ==========================
  1433. // 線條顏色-柱狀圖
  1434. // =========================
  1435. const colorInputBar = document.getElementById('borderColorInputBar');
  1436. // 添加事件监听器,当颜色选择发生变化时触发
  1437. colorInputBar.addEventListener('input', function () {
  1438. extractAndGenerateChart(dataArray);
  1439. });
  1440. // =========================
  1441. // 線條粗細
  1442. // =========================
  1443. function updateLineWidth(borderWidthValue) {
  1444. myChart.data.datasets.forEach(dataset => {
  1445. dataset.borderWidth = parseFloat(borderWidthValue);
  1446. });
  1447. myChart.update();
  1448. }
  1449. var rangeInput = document.getElementById('borderWidthRange');
  1450. // var rangeInputSpan = document.getElementById('borderWidthRangeValue');
  1451. var borderWidthValue;
  1452. // rangeInputSpan.textContent = rangeInput.value;
  1453. // 添加事件监听器,当滑动条的值发生改变时更新显示的值
  1454. rangeInput.addEventListener('input', function () {
  1455. // 获取滑动条的当前值
  1456. borderWidthValue = rangeInput.value;
  1457. console.log(borderWidthValue);
  1458. // rangeInputSpan.textContent = borderWidthValue;
  1459. updateLineWidth(borderWidthValue);
  1460. // myChart.data.datasets[0].borderWidth = borderWidthValue;
  1461. });
  1462. // =========================
  1463. // 線條顏色-折線圖
  1464. // =========================
  1465. const colorInput = document.getElementById('borderColorInput');
  1466. let selectedDatasetIndex = null;
  1467. // 添加事件监听器,当颜色选择发生变化时触发
  1468. colorInput.addEventListener('input', function () {
  1469. // 获取当前选择的颜色值
  1470. // chartColor = colorInput.value;
  1471. // if (myChart) {
  1472. // myChart.destroy();
  1473. // }
  1474. if (selectedDatasetIndex !== null) {
  1475. const newColor = colorInput.value;
  1476. myChart.data.datasets[selectedDatasetIndex].borderColor = newColor;
  1477. myChart.data.datasets[selectedDatasetIndex].backgroundColor = newColor;
  1478. myChart.update(); // 更新图表
  1479. // createChart(chartType, data, labels);
  1480. }
  1481. // console.log(chartColor);
  1482. // createChart(chartType, data, labels);
  1483. });
  1484. function rgbToHex(rgb) {
  1485. const rgbArr = rgb.match(/\d+/g);
  1486. return `#${((1 << 24) + (parseInt(rgbArr[0]) << 16) + (parseInt(rgbArr[1]) << 8) + parseInt(rgbArr[2])).toString(16).slice(1)}`;
  1487. }
  1488. function hexToRgba(hex, alpha = 1) {
  1489. // 移除 HEX 颜色值的 # 符号
  1490. hex = hex.replace(/^#/, '');
  1491. // 处理 3 位 HEX 颜色值(如 #fff)
  1492. if (hex.length === 3) {
  1493. hex = hex.split('').map(function (hex) {
  1494. return hex + hex;
  1495. }).join('');
  1496. }
  1497. // 解析 HEX 颜色值
  1498. const r = parseInt(hex.substring(0, 2), 16);
  1499. const g = parseInt(hex.substring(2, 4), 16);
  1500. const b = parseInt(hex.substring(4, 6), 16);
  1501. // 返回 RGBA 颜色值
  1502. return `rgba(${r}, ${g}, ${b}, ${alpha})`;
  1503. }
  1504. // =========================
  1505. // 模板樣式
  1506. // =========================
  1507. // JavaScript
  1508. document.addEventListener("DOMContentLoaded", function () {
  1509. // 獲取所有帶有 templateImg class 的圖像元素
  1510. var templateImgs = document.querySelectorAll('.templateImg');
  1511. // 為每個圖像元素添加點擊事件監聽器
  1512. templateImgs.forEach(function (img) {
  1513. img.addEventListener('click', function () {
  1514. // 在點擊時印出圖像元素的 value 屬性值
  1515. console.log(this.getAttribute('value'));
  1516. });
  1517. });
  1518. });
  1519. // =========================
  1520. var bgImgelement = document.getElementById('data_chart_box');
  1521. var chartWhitelement = document.getElementById('data_chartJs_box');
  1522. // 背景樣式
  1523. // =========================
  1524. // JavaScript
  1525. document.addEventListener("DOMContentLoaded", function () {
  1526. // 獲取所有帶有 templateImg class 的圖像元素
  1527. var bgImgs = document.querySelectorAll('.bgImg');
  1528. // 為每個圖像元素添加點擊事件監聽器
  1529. bgImgs.forEach(function (img) {
  1530. img.addEventListener('click', function () {
  1531. // 在點擊時印出圖像元素的 value 屬性值
  1532. console.log(this.getAttribute('value'));
  1533. var bgImgUrl = this.getAttribute('value');
  1534. chart_bgimg_url = 'url(' + bgImgUrl + ')';
  1535. if (bgImgUrl === './img/bg07.webp') {
  1536. chartTitle.style.color = "#ffff";
  1537. chartTitle.style.padding = '0px'
  1538. }
  1539. else {
  1540. chartTitle.style.color = "#1c2d6d";
  1541. }
  1542. if (bgImgUrl === "") {
  1543. chartWhitelement.style.background = "";
  1544. console.log('無背景');
  1545. bgImgelement.style.backgroundImage = "";
  1546. chartWhitelement.style.background = 'rgba(255, 255, 255, 0) !important;'
  1547. } else {
  1548. bgImgelement.style.backgroundImage = chart_bgimg_url;
  1549. chartWhitelement.style.background = "rgba(255, 255, 255, 0.5)"
  1550. }
  1551. // bgImgelement.style.backgroundSize = 'cover'; // 調整背景圖片大小
  1552. // bgImgelement.style.backgroundPosition = 'center'; // 調整背景圖片位置
  1553. // bgImgelement.style.backgroundRepeat = 'no-repeat'; // 調整背景圖片重複
  1554. });
  1555. });
  1556. });
  1557. // =========================
  1558. const radioButtons = document.querySelectorAll('input[name="inlineRadioOptions"]');
  1559. var chartTitle = document.querySelector('.chart_title');
  1560. radioButtons.forEach(button => {
  1561. button.addEventListener('click', function () {
  1562. if (this.value === '0') {
  1563. chartTitle.style.textAlign = 'left';
  1564. } else if (this.value === '1') {
  1565. chartTitle.style.textAlign = 'center';
  1566. } else if (this.value === '2') {
  1567. chartTitle.style.textAlign = 'right';
  1568. }
  1569. });
  1570. });
  1571. // 圖表標題
  1572. var keywordInput = document.getElementById('title_data');
  1573. // 初始时将输入框的值设置为标题的文本内容
  1574. keywordInput.value = chartTitle.textContent;
  1575. // 监听输入框的输入事件,实现内容同步更新
  1576. keywordInput.addEventListener('input', function () {
  1577. chartTitle.textContent = this.value;
  1578. });
  1579. // 標題字體大小
  1580. // ==========================
  1581. var TitlefontSize = document.getElementById('TitlefontSize');
  1582. TitlefontSize.addEventListener('input', function () {
  1583. var TitlefontSizeValue = TitlefontSize.value;
  1584. chartTitle.style.fontSize = TitlefontSizeValue + 'px';;
  1585. });
  1586. // 標題字體顏色
  1587. var stocknumber;
  1588. // ==========================
  1589. let labelDependent = ""; // 圖表左側 Label
  1590. function get_data(input_text_value) {
  1591. clickExample.style.display = 'none'
  1592. // 重新创建取消令牌和源
  1593. cancelToken = axios.CancelToken;
  1594. source = cancelToken.source();
  1595. // inputField.value = "";
  1596. apiLoading();
  1597. var existingTable = chartJsBox.querySelector('table');
  1598. downloadButton.style.display = "none";
  1599. if (chartDiv.childElementCount != 0) {
  1600. data = [];
  1601. labels = [];
  1602. console.log('已存在')
  1603. chartTitle.textContent = "";
  1604. unit_value.textContent = "";
  1605. bgImgelement.style.backgroundImage = "";
  1606. chartWhitelement.style.background = "";
  1607. document.getElementById('img_box_url').src = "";
  1608. }
  1609. if (existingTable) {
  1610. unit_value.textContent = "";
  1611. chartJsBox.removeChild(existingTable);
  1612. chartTitle.textContent = "";
  1613. bgImgelement.style.backgroundImage = "";
  1614. chartWhitelement.style.background = "";
  1615. document.getElementById('img_box_url').src = "";
  1616. }
  1617. if (myChart) {
  1618. unit_value_box.style.display = 'none';
  1619. unit_value.textContent = "";
  1620. data = [];
  1621. labels = [];
  1622. chartTitle.textContent = "";
  1623. bgImgelement.style.backgroundImage = "";
  1624. chartWhitelement.style.background = "";
  1625. myChart.destroy();
  1626. document.getElementById('img_box_url').src = "";
  1627. }
  1628. const apiUrl = `https://cmm.ai:8080/answer_with_token?token=${accessToken}&question=${input_text_value}`;
  1629. axios.get(apiUrl, {
  1630. cancelToken: source.token // 将取消令牌传递给 Axios 请求
  1631. })
  1632. .then(response => {
  1633. apiHideLoading();
  1634. console.log(response);
  1635. if (response.data.data == "未達相似度搜尋標準") {
  1636. alert('無法生成圖表')
  1637. return
  1638. } else {
  1639. unit_value_box.style.display = "block";
  1640. var chart_info = response.data.chart_info.Title;
  1641. chartType = response.data.chart_info.Chart_type;
  1642. var finance_img_url = response.data.imageUrl_info.imageUrl;
  1643. if (response.data.imageUrl_info != null) {
  1644. console.log(finance_img_url);
  1645. document.getElementById('img_box_url').src = finance_img_url;
  1646. }
  1647. keywordInput.value = chart_info;
  1648. chartTitle.textContent = chart_info;
  1649. unit = response.data.chart_info.Unit_of_dependent_variable;
  1650. var Label_dependent_variable = response.data.chart_info.Label_dependent_variable;
  1651. var Label_independent_variable = response.data.chart_info.Label_independent_variable;
  1652. stocknumber = response.data.chart_info.args;
  1653. chartTypeBtn.value = chartType;
  1654. unit_data.value = response.data.chart_info.Unit_of_dependent_variable;
  1655. if (response.data.chart_info.Unit_of_dependent_variable == "") {
  1656. unit_value.textContent = ""
  1657. } else {
  1658. unit_value.textContent = response.data.chart_info.Unit_of_dependent_variable;
  1659. }
  1660. obj.text = chartType;
  1661. dataArray = response.data.data;
  1662. if (chartType === "table") {
  1663. unit_value.textContent = "";
  1664. unit_value_box.style.display = "none"
  1665. createTable(dataArray, unit, Label_dependent_variable, Label_independent_variable);
  1666. // document.getElementById('img_box_url').src = "";
  1667. } else if (chartType === "bar") {
  1668. unit_value_box.style.display = 'block';
  1669. console.log('圖表類型bar')
  1670. $('#textToChart').hide();
  1671. extractAndGenerateChart(dataArray);
  1672. } else if (chartType === "doughnut") {
  1673. unit_value.textContent = "";
  1674. $('#textToChart').hide();
  1675. generatePieChart(dataArray, pieFontSize, PieMaxWidth)
  1676. } else {
  1677. unit_value_box.style.display = "block"
  1678. $('#chartdiv').hide();
  1679. generateChart(dataArray);
  1680. }
  1681. // }
  1682. // 在這裡處理成功獲取 JSON 的情況
  1683. // console.log(response);
  1684. // switch (chartType) {
  1685. // case "line":
  1686. // document.getElementById("inlineRadio4").checked = true; // 折线图
  1687. // break;
  1688. // case "bar":
  1689. // document.getElementById("inlineRadio5").checked = true; // 柱状图
  1690. // labelDependent = response.data.chart_info.Label_dependent_variable;
  1691. // break;
  1692. // case "pie":
  1693. // document.getElementById("inlineRadio6").checked = true; // 圆饼图
  1694. // break;
  1695. // case "table":
  1696. // document.getElementById("inlineRadio7").checked = true; // 表格
  1697. // break;
  1698. // default:
  1699. // // 默认情况
  1700. }
  1701. // generateChart(dataArray);
  1702. })
  1703. .catch(error => {
  1704. // 错误处理逻辑,包括取消请求的情况
  1705. apiHideLoading();
  1706. if (axios.isCancel(error)) {
  1707. console.log('请求被取消:', error.message);
  1708. } else {
  1709. console.error('发生错误:', error);
  1710. }
  1711. });
  1712. }
  1713. // 取消请求的函数
  1714. function cancelRequest() {
  1715. if (source) {
  1716. source.cancel('取消了 API 请求');
  1717. apiHideLoading(); // 隐藏加载状态
  1718. }
  1719. }
  1720. document.getElementById('cancel_send_data').addEventListener('click', cancelRequest);
  1721. $(document).ready(function () {
  1722. $('#dataTable').DataTable();
  1723. });
  1724. function resizeChart() {
  1725. console.log(myChart)
  1726. if (myChart != null) {
  1727. myChart.resize();
  1728. }
  1729. }
  1730. document.addEventListener('visibilitychange', function () {
  1731. console.log('Visibility changed:', document.visibilityState);
  1732. if (document.visibilityState === 'visible') {
  1733. resizeChart();
  1734. }
  1735. });
  1736. // 监听窗口resize事件
  1737. window.addEventListener('resize', resizeChart);
  1738. var setting_button = document.getElementById('chart_seeting_button');
  1739. var closeButton = document.getElementById('style_setting_close');
  1740. var closeButton_table = document.getElementById('style_setting_close_table');
  1741. // var borderSetting = document.getElementById('borderSetting');
  1742. var lineChartSetting = document.getElementById('lineChartSetting');
  1743. var barChartSetting = document.getElementById('barChartSetting');
  1744. var table_setting = document.getElementById('table_setting');
  1745. var chart_category = document.getElementById('chart_category');
  1746. var pie_setting = document.getElementById('pie_setting');
  1747. // 获取 chartDiv 元素
  1748. setting_button.addEventListener('click', () => {
  1749. console.log('setting_button')
  1750. if (myChart === null && !chartJsBox.querySelector('table') && chartDiv.childElementCount === 0) {
  1751. alert('請先生成圖表');
  1752. return
  1753. }
  1754. if (chartType === 'table') {
  1755. console.log('表格');
  1756. sidebar.classList.toggle('show');
  1757. sidebar.classList.toggle('hidden');
  1758. barChartSetting.style.display = 'none';
  1759. lineChartSetting.style.display = 'none';
  1760. table_setting.style.display = 'block';
  1761. chart_category.style.display = 'none';
  1762. pie_setting.style.display = 'none';
  1763. } else if (chartType === "bar") {
  1764. console.log('柱狀圖')
  1765. sidebar.classList.toggle('show');
  1766. sidebar.classList.toggle('hidden');
  1767. barChartSetting.style.display = 'block';
  1768. lineChartSetting.style.display = 'none';
  1769. table_setting.style.display = 'none';
  1770. pie_setting.style.display = 'none';
  1771. } else if (chartType === "doughnut") {
  1772. console.log('快捷功能圓餅圖')
  1773. sidebar.classList.toggle('show');
  1774. sidebar.classList.toggle('hidden');
  1775. barChartSetting.style.display = 'none';
  1776. lineChartSetting.style.display = 'none';
  1777. pie_setting.style.display = 'block';
  1778. table_setting.style.display = 'none';
  1779. chart_category.style.display = 'none';
  1780. document.getElementById('unit_input').style.display = 'none';
  1781. } else {
  1782. sidebar.classList.toggle('show');
  1783. sidebar.classList.toggle('hidden');
  1784. table_setting.style.display = 'none';
  1785. barChartSetting.style.display = 'none';
  1786. lineChartSetting.style.display = 'block';
  1787. pie_setting.style.display = 'none';
  1788. }
  1789. });
  1790. closeButton.addEventListener('click', () => {
  1791. sidebar.classList.remove('show');
  1792. sidebar.classList.add('hidden');
  1793. });
  1794. const finance_options = [
  1795. { name: "原油油桶", imageUrl: "./img/finance/01.webp" },
  1796. { name: "黃金", imageUrl: "./img/finance/02.webp" },
  1797. { name: "房屋", imageUrl: "./img/finance/03.webp" },
  1798. { name: "車", imageUrl: "./img/finance/04.webp" },
  1799. { name: "股市", imageUrl: "./img/finance/05.webp" },
  1800. { name: "晶圓", imageUrl: "./img/finance/06.webp" },
  1801. { name: "TSMC 台積電", imageUrl: "./img/finance/07.webp" },
  1802. { name: "台幣", imageUrl: "./img/finance/08.webp" },
  1803. { name: "美金", imageUrl: "./img/finance/09.webp" },
  1804. { name: "購物車", imageUrl: "./img/finance/10.webp" },
  1805. { name: "美中貿易", imageUrl: "./img/finance/11.webp" },
  1806. { name: "世界地圖", imageUrl: "./img/finance/12.webp" },
  1807. { name: "小麥", imageUrl: "./img/finance/13.webp" },
  1808. { name: "玉米", imageUrl: "./img/finance/14.webp" },
  1809. { name: "智慧產線", imageUrl: "./img/finance/15.webp" }
  1810. ];
  1811. const selectOptions = document.getElementById('finance_options');
  1812. const selectOptions_table = document.getElementById('finance_options_table');
  1813. finance_options.forEach((option, index) => {
  1814. const optionElement = document.createElement('option');
  1815. optionElement.value = index + 1; // 值从 1 开始
  1816. optionElement.textContent = option.name;
  1817. optionElement.setAttribute('data-image-url', option.imageUrl); // 存储图片 URL
  1818. selectOptions.appendChild(optionElement);
  1819. });
  1820. selectOptions.addEventListener('change', function () {
  1821. const selectedOption = selectOptions.options[selectOptions.selectedIndex];
  1822. const imageUrl = selectedOption.getAttribute('data-image-url');
  1823. document.getElementById('img_box_url').src = imageUrl;
  1824. });
  1825. // finance_options.forEach((option, index) => {
  1826. // const optionElement = document.createElement('option');
  1827. // optionElement.value = index + 1; // 值从 1 开始
  1828. // optionElement.textContent = option.name;
  1829. // optionElement.setAttribute('data-image-url', option.imageUrl); // 存储图片 URL
  1830. // selectOptions_table.appendChild(optionElement);
  1831. // });
  1832. // selectOptions_table.addEventListener('change', function () {
  1833. // const selectedOption = selectOptions_table.options[selectOptions_table.selectedIndex];
  1834. // const imageUrl = selectedOption.getAttribute('data-image-url');
  1835. // document.getElementById('img_box_url').src = imageUrl;
  1836. // });
  1837. document.querySelectorAll('input[name="ChartOptions"]').forEach(function (radio) {
  1838. radio.addEventListener('change', function () {
  1839. // 檢查哪個單選按鈕被選中
  1840. if (this.value === 'line') {
  1841. // 如果選擇了折線圖,執行相應的操作
  1842. console.log("選擇了折線圖");
  1843. // 在這裡執行顯示折線圖的相關代碼
  1844. } else if (this.value === 'doughnut') {
  1845. // 如果選擇了柱狀圖,執行相應的操作
  1846. console.log("選擇了柱狀圖");
  1847. // 在這裡執行顯示柱狀圖的相關代碼
  1848. }
  1849. });
  1850. });
  1851. function tokencheck() {
  1852. if (!localStorage.getItem("access_token")) {
  1853. window.location.href = "./login.html";
  1854. } else {
  1855. console.log("存在");
  1856. }
  1857. }
  1858. tokencheck();
  1859. function logout() {
  1860. localStorage.removeItem("access_token");
  1861. }
  1862. $(document).on("click", "#logout", function (event) {
  1863. alert("登出成功");
  1864. logout();
  1865. location.reload();
  1866. });
  1867. // speech_to_text
  1868. var closeRecord = document.getElementById('recording_block_close');
  1869. var recording_block = document.getElementById('recording_block');
  1870. var PageLM = "2024-05-07 18:33";
  1871. var recording_button = document.getElementById('recording_button');
  1872. var stopButton = document.getElementById('stop');
  1873. var audio = document.getElementById('audio');
  1874. var recorder, audioBlob;
  1875. closeRecord.addEventListener('click', () => {
  1876. recPause();
  1877. console.log('record-click')
  1878. recording_block.classList.remove('show');
  1879. recording_block.classList.add('hidden');
  1880. });
  1881. function reclog(s, color) {
  1882. var now = new Date();
  1883. var t = ("0" + now.getHours()).substr(-2)
  1884. + ":" + ("0" + now.getMinutes()).substr(-2)
  1885. + ":" + ("0" + now.getSeconds()).substr(-2);
  1886. var div = document.createElement("div");
  1887. var elem = document.querySelector(".reclog");
  1888. elem.insertBefore(div, elem.firstChild);
  1889. div.innerHTML = '<div style="color:' + (!color ? "" : color == 1 ? "red" : color == 2 ? "#0b1" : color) + '">[' + t + ']' + s + '</div>';
  1890. };
  1891. window.onerror = function (message, url, lineNo, columnNo, error) {
  1892. //https://www.cnblogs.com/xianyulaodi/p/6201829.html
  1893. reclog('<span style="color:red">【Uncaught Error】' + message + '<pre>' + "at:" + lineNo + ":" + columnNo + " url:" + url + "\n" + (error && error.stack || Html_$T("kBaF::不能获得错误堆栈")) + '</pre></span>');
  1894. };
  1895. if (!window.Html_$T) {//没有提供本页面用的国际化多语言支持时 返回中文文本
  1896. window.Html_$T = function () {
  1897. var a = arguments, txt = a[0].replace(/^.+?::/, ""), n = 0;
  1898. for (var i = 0; i < a.length; i++) { if (typeof a[i] == "number") { n = i; break } }
  1899. txt = txt.replace(/\{(\d+)\}/g, function (v, f) { v = a[+f + n]; return v == null ? "" : v });
  1900. return txt;
  1901. }
  1902. window.Html_xT = function (v) { return v }
  1903. }
  1904. if (window.Recorder) {
  1905. // reclog(Html_$T('BL9u::頁面已準備好,請先點擊打開錄音,然後點擊錄製'), 2);
  1906. alert('錄音已準備好,請先點擊右下角打開錄音,即可開始錄製')
  1907. } else {
  1908. reclog(Html_$T("YzPd::js文件加载失败,请刷新重试!"), "#f00;font-size:50px");
  1909. console.log('js文件加载失敗,请刷新重试!')
  1910. }
  1911. recording_button.addEventListener('click', async () => {
  1912. recording_block.classList.toggle('show');
  1913. recording_block.classList.toggle('hidden');
  1914. recStart();
  1915. });
  1916. function recStart() {//打开了录音后才能进行start、stop调用
  1917. rec.start();
  1918. };
  1919. stopButton.addEventListener('click', async () => {
  1920. // recorder.stop();
  1921. // uploadAudio(audioBlob);
  1922. recStop();
  1923. });
  1924. window.onload = function () {
  1925. recOpen();
  1926. };
  1927. // 錄音程式
  1928. // =======================
  1929. var rec, wave, recBlob;
  1930. /**调用open打开录音请求好录音权限 Call open to open the recording and request the recording permission**/
  1931. var recOpen = function () {//一般在显示出录音按钮或相关的录音界面时进行此方法调用,后面用户点击开始录音时就能畅通无阻了
  1932. rec = null;
  1933. wave = null;
  1934. recBlob = null;
  1935. var newRec = Recorder({
  1936. type: "mp3", sampleRate: 16000, bitRate: 16 //mp3格式,指定采样率hz、比特率kbps,其他参数使用默认配置;注意:是数字的参数必须提供数字,不要用字符串;需要使用的type类型,需提前把格式支持文件加载进来,比如使用wav格式需要提前加载wav.js编码引擎
  1937. , onProcess: function (buffers, powerLevel, bufferDuration, bufferSampleRate, newBufferIdx, asyncEnd) {
  1938. // // //录音实时回调,大约1秒调用12次本回调
  1939. document.querySelector(".recpowerx").style.width = powerLevel + "%";
  1940. document.querySelector(".recpowert").innerText = formatMs(bufferDuration, 1) + " / " + powerLevel;
  1941. // //可视化图形绘制
  1942. // wave.input(buffers[buffers.length - 1], powerLevel, bufferSampleRate);
  1943. }
  1944. });
  1945. newRec.open(function () {//打开麦克风授权获得相关资源
  1946. rec = newRec;
  1947. //此处创建这些音频可视化图形绘制浏览器支持妥妥的
  1948. // wave = Recorder.FrequencyHistogramView({ elem: ".recwave" });
  1949. reclog(Html_$T("GVCa::已打開錄音,可以點擊錄制開始錄音"), 2);
  1950. }, function (msg, isUserNotAllow) {//用户拒绝未授权或不支持
  1951. reclog((isUserNotAllow ? "UserNotAllow, " : "") + Html_$T("TOOV::打開錄音失敗:") + msg, 1);
  1952. });
  1953. };
  1954. /**关闭录音,释放资源 Close recording, release resources**/
  1955. function recClose() {
  1956. if (rec) {
  1957. rec.close();
  1958. reclog(Html_$T("jqOs::已关闭"));
  1959. } else {
  1960. reclog(Html_$T("VOOw::未打开录音"), 1);
  1961. };
  1962. };
  1963. /**开始录音 Start recording**/
  1964. function recStart() {//打开了录音后才能进行start、stop调用
  1965. if (rec && Recorder.IsOpen()) {
  1966. recBlob = null;
  1967. rec.start();
  1968. reclog(Html_$T("CGdy::已經開始錄音...") + " " + rec.set.type + " " + rec.set.sampleRate + " " + rec.set.bitRate + "kbps");
  1969. } else {
  1970. reclog(Html_$T("ajKR::未打开錄音"), 1);
  1971. };
  1972. };
  1973. /**暂停录音 Passing recording**/
  1974. function recPause() {
  1975. if (rec && Recorder.IsOpen()) {
  1976. rec.pause();
  1977. reclog(Html_$T("GvCy::已暂停"));
  1978. } else {
  1979. reclog(Html_$T("gCAR::未打開錄音"), 1);
  1980. };
  1981. };
  1982. /**恢复录音 Resume recording**/
  1983. function recResume() {
  1984. if (rec && Recorder.IsOpen()) {
  1985. rec.resume();
  1986. reclog(Html_$T("5q1K::继续录音中..."));
  1987. } else {
  1988. reclog(Html_$T("Ob6S::未打开录音"), 1);
  1989. };
  1990. };
  1991. /**结束录音,得到音频文件 Stop recording and get audio files**/
  1992. function recStop() {
  1993. if (!(rec && Recorder.IsOpen())) {
  1994. reclog(Html_$T("5JuL::未打開錄音"), 1);
  1995. return;
  1996. };
  1997. rec.stop(function (blob, duration) {
  1998. console.log(blob, (window.URL || webkitURL).createObjectURL(blob), Html_xT(Html_$T("gOix::時長:{1}ms", 0, duration)));
  1999. recBlob = blob;
  2000. var file = new File([blob], "recording.mp3", { type: "audio/mp3" });
  2001. // console.log(file);
  2002. handleAudioToText(file);
  2003. reclog(Html_$T("0LHf::已錄製mp3:{1}ms {2}字節,可以點擊播放、上傳、本地下载了", 0, formatMs(duration), blob.size), 2);
  2004. }, function (msg) {
  2005. reclog(Html_$T("kGZO::錄音失敗:") + msg, 1);
  2006. });
  2007. };
  2008. /**播放 Play**/
  2009. function recPlay() {
  2010. if (!recBlob) {
  2011. reclog(Html_$T("tIke::请先錄音,然后停止後再播放"), 1);
  2012. return;
  2013. };
  2014. var cls = ("a" + Math.random()).replace(".", "");
  2015. reclog(Html_$T('GlWb::播放中: ') + '<span class="' + cls + '"></span>');
  2016. var audio = document.createElement("audio");
  2017. audio.controls = true;
  2018. document.querySelector("." + cls).appendChild(audio);
  2019. //简单利用URL生成播放地址,注意不用了时需要revokeObjectURL,否则霸占内存
  2020. audio.src = (window.URL || webkitURL).createObjectURL(recBlob);
  2021. audio.play();
  2022. setTimeout(function () {
  2023. (window.URL || webkitURL).revokeObjectURL(audio.src);
  2024. }, 5000);
  2025. };
  2026. /**上传 Upload**/
  2027. function recUpload() {
  2028. var blob = recBlob;
  2029. if (!blob) {
  2030. reclog(Html_$T("DUTn::请先录音,然后停止后再上传"), 1);
  2031. return;
  2032. };
  2033. //本例子假设使用原始XMLHttpRequest请求方式,实际使用中自行调整为自己的请求方式
  2034. //录音结束时拿到了blob文件对象,可以用FileReader读取出内容,或者用FormData上传
  2035. var api = "http://127.0.0.1:9528";
  2036. var onreadystatechange = function (xhr, title) {
  2037. return function () {
  2038. if (xhr.readyState == 4) {
  2039. if (xhr.status == 200) {
  2040. reclog(title + Html_$T("G2MU::上传成功") + ' <span style="color:#999">response: ' + xhr.responseText + '</span>', 2);
  2041. } else {
  2042. reclog(title + Html_$T("TUdi::没有完成上传,演示上传地址无需关注上传结果,只要浏览器控制台内Network面板内看到的请求数据结构是预期的就ok了。"), "#d8c1a0");
  2043. console.error(Html_xT(title + Html_$T("HjDi::上传失败")), xhr.status, xhr.responseText);
  2044. };
  2045. };
  2046. };
  2047. };
  2048. reclog(Html_$T("QnSI::开始上传到{1},请稍候... (你可以先到源码 /assets/node-localServer 目录内执行 npm run start 来运行本地测试服务器)", 0, api));
  2049. /***方式一:将blob文件转成base64纯文本编码,使用普通application/x-www-form-urlencoded表单上传***/
  2050. var reader = new FileReader();
  2051. reader.onloadend = function () {
  2052. var postData = "";
  2053. postData += "mime=" + encodeURIComponent(blob.type);//告诉后端,这个录音是什么格式的,可能前后端都固定的mp3可以不用写
  2054. postData += "&upfile_b64=" + encodeURIComponent((/.+;\s*base64\s*,\s*(.+)$/i.exec(reader.result) || [])[1]) //录音文件内容,后端进行base64解码成二进制
  2055. //...其他表单参数
  2056. var xhr = new XMLHttpRequest();
  2057. xhr.open("POST", api + "/uploadBase64");
  2058. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  2059. xhr.onreadystatechange = onreadystatechange(xhr, Html_$T("gG1f::上传方式一【Base64】"));
  2060. xhr.send(postData);
  2061. };
  2062. reader.readAsDataURL(blob);
  2063. /***方式二:使用FormData用multipart/form-data表单上传文件***/
  2064. var form = new FormData();
  2065. form.append("upfile", blob, "recorder.mp3"); //和普通form表单并无二致,后端接收到upfile参数的文件,文件名为recorder.mp3
  2066. //...其他表单参数
  2067. var xhr = new XMLHttpRequest();
  2068. xhr.open("POST", api + "/upload");
  2069. xhr.onreadystatechange = onreadystatechange(xhr, Html_$T("vDzB::上传方式二【FormData】"));
  2070. xhr.send(form);
  2071. };
  2072. /**本地下载 Local download**/
  2073. function recLocalDown() {
  2074. if (!recBlob) {
  2075. reclog(Html_$T("M86h::请先录音,然后停止后再下载"), 1);
  2076. return;
  2077. };
  2078. var cls = ("a" + Math.random()).replace(".", "");
  2079. recdown64.lastCls = cls;
  2080. reclog(Html_$T('vJPl::点击 ') + '<span class="' + cls + '"></span>' + Html_$T('Whtc:: 下载,或复制文本')
  2081. + '<button onclick="recdown64(\'' + cls + '\')">' + Html_$T('XK4l::生成Base64文本') + '</button><span class="' + cls + '_b64"></span>');
  2082. var fileName = "recorder-" + Date.now() + ".mp3";
  2083. var downA = document.createElement("A");
  2084. downA.innerHTML = Html_$T("g8Fy::下载 ") + fileName;
  2085. downA.href = (window.URL || webkitURL).createObjectURL(recBlob);
  2086. downA.download = fileName;
  2087. document.querySelector("." + cls).appendChild(downA);
  2088. if (/mobile/i.test(navigator.userAgent)) {
  2089. alert(Html_xT(Html_$T("DIEK::因移动端绝大部分国产浏览器未适配Blob Url的下载,所以本demo代码在移动端未调用downA.click()。请尝试点击日志中显示的下载链接下载")));
  2090. } else {
  2091. downA.click();
  2092. }
  2093. //不用了时需要revokeObjectURL,否则霸占内存
  2094. //(window.URL||webkitURL).revokeObjectURL(downA.href);
  2095. };
  2096. function recdown64(cls) {
  2097. var el = document.querySelector("." + cls + "_b64");
  2098. if (recdown64.lastCls != cls) {
  2099. el.innerHTML = '<span style="color:red">' + Html_$T("eKKx::老的数据没有保存,只支持最新的一条") + '</span>';
  2100. return;
  2101. }
  2102. var reader = new FileReader();
  2103. reader.onloadend = function () {
  2104. el.innerHTML = '<textarea></textarea>';
  2105. el.querySelector("textarea").value = reader.result;
  2106. };
  2107. reader.readAsDataURL(recBlob);
  2108. };
  2109. var formatMs = function (ms, all) {
  2110. var ss = ms % 1000; ms = (ms - ss) / 1000;
  2111. var s = ms % 60; ms = (ms - s) / 60;
  2112. var m = ms % 60; ms = (ms - m) / 60;
  2113. var h = ms;
  2114. var t = (h ? h + ":" : "")
  2115. + (all || h + m ? ("0" + m).substr(-2) + ":" : "")
  2116. + (all || h + m + s ? ("0" + s).substr(-2) + "″" : "")
  2117. + ("00" + ss).substr(-3);
  2118. return t;
  2119. };
  2120. // =======================
  2121. let mediaRecorder;
  2122. let audioChunks = [];
  2123. var recordText = document.getElementById('record_text');
  2124. async function handleAudioToText(file) {
  2125. console.log('tts')
  2126. let audioLang = "cmn-Hant-TW"; // 音訊語言
  2127. // let lang = localStorage.getItem("lang");
  2128. let lang = "zh-tw";
  2129. console.log("lang", lang);
  2130. switch (lang) {
  2131. case "zh-tw":
  2132. audioLang = "cmn-Hant-TW";
  2133. break;
  2134. case "en-us":
  2135. audioLang = "en-US";
  2136. break;
  2137. case "ja-jp":
  2138. audioLang = "ja-JP";
  2139. break;
  2140. case "ko-kr":
  2141. audioLang = "ko-KR";
  2142. break;
  2143. default:
  2144. break;
  2145. }
  2146. let url = `https://cmm.ai:9001/gcp/speech-to-text?language_code=${audioLang}`;
  2147. const formData = new FormData();
  2148. formData.append("file", file);
  2149. try {
  2150. const response = await axios.post(url, formData);
  2151. console.log("response", response);
  2152. input_text_value = response.data;
  2153. recording_block.classList.remove('show');
  2154. recording_block.classList.add('hidden');
  2155. if (input_text_value.length === 0) {
  2156. alert('請再說一次')
  2157. return
  2158. }
  2159. console.log(input_text_value[0]);
  2160. inputField.value = input_text_value[0];
  2161. // Show the result for a short time before executing get_data
  2162. get_data(input_text_value);
  2163. // get_data(input_text_value);
  2164. } catch (error) {
  2165. console.log("error", error);
  2166. }
  2167. }
  2168. Object.defineProperty(obj, 'text', {
  2169. get() {
  2170. return this._text;
  2171. },
  2172. set(newValue) {
  2173. console.log(`字符串从 "${this._text}" 变为 "${newValue}"`);
  2174. this._text = newValue;
  2175. }
  2176. });