text-to-chart.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935
  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. createChart(chartType, data, labels);
  1413. });
  1414. // ==========================
  1415. // x軸字體大小-柱狀圖
  1416. // ==========================
  1417. var XfontSizeBar = document.getElementById('XfontSizeBar');
  1418. XfontSizeBar.addEventListener('input', function () {
  1419. extractAndGenerateChart(dataArray);
  1420. });
  1421. // ==========================
  1422. // y軸字體大小-柱狀圖
  1423. // ==========================
  1424. var YfontSizeBar = document.getElementById('YfontSizeBar');
  1425. YfontSizeBar.addEventListener('input', function () {
  1426. extractAndGenerateChart(dataArray);
  1427. });
  1428. // ==========================
  1429. // 線條顏色-柱狀圖
  1430. // =========================
  1431. const colorInputBar = document.getElementById('borderColorInputBar');
  1432. // 添加事件监听器,当颜色选择发生变化时触发
  1433. colorInputBar.addEventListener('input', function () {
  1434. extractAndGenerateChart(dataArray);
  1435. });
  1436. // =========================
  1437. // 線條粗細
  1438. // =========================
  1439. function updateLineWidth(borderWidthValue) {
  1440. myChart.data.datasets.forEach(dataset => {
  1441. dataset.borderWidth = parseFloat(borderWidthValue);
  1442. });
  1443. myChart.update();
  1444. }
  1445. var rangeInput = document.getElementById('borderWidthRange');
  1446. // var rangeInputSpan = document.getElementById('borderWidthRangeValue');
  1447. var borderWidthValue;
  1448. // rangeInputSpan.textContent = rangeInput.value;
  1449. // 添加事件监听器,当滑动条的值发生改变时更新显示的值
  1450. rangeInput.addEventListener('input', function () {
  1451. // 获取滑动条的当前值
  1452. borderWidthValue = rangeInput.value;
  1453. console.log(borderWidthValue);
  1454. // rangeInputSpan.textContent = borderWidthValue;
  1455. updateLineWidth(borderWidthValue);
  1456. // myChart.data.datasets[0].borderWidth = borderWidthValue;
  1457. });
  1458. // =========================
  1459. // 線條顏色-折線圖
  1460. // =========================
  1461. const colorInput = document.getElementById('borderColorInput');
  1462. let selectedDatasetIndex = null;
  1463. // 添加事件监听器,当颜色选择发生变化时触发
  1464. colorInput.addEventListener('input', function () {
  1465. // 获取当前选择的颜色值
  1466. // chartColor = colorInput.value;
  1467. // if (myChart) {
  1468. // myChart.destroy();
  1469. // }
  1470. if (selectedDatasetIndex !== null) {
  1471. const newColor = colorInput.value;
  1472. myChart.data.datasets[selectedDatasetIndex].borderColor = newColor;
  1473. myChart.data.datasets[selectedDatasetIndex].backgroundColor = newColor;
  1474. myChart.update(); // 更新图表
  1475. // createChart(chartType, data, labels);
  1476. }
  1477. // console.log(chartColor);
  1478. // createChart(chartType, data, labels);
  1479. });
  1480. function rgbToHex(rgb) {
  1481. const rgbArr = rgb.match(/\d+/g);
  1482. return `#${((1 << 24) + (parseInt(rgbArr[0]) << 16) + (parseInt(rgbArr[1]) << 8) + parseInt(rgbArr[2])).toString(16).slice(1)}`;
  1483. }
  1484. function hexToRgba(hex, alpha = 1) {
  1485. // 移除 HEX 颜色值的 # 符号
  1486. hex = hex.replace(/^#/, '');
  1487. // 处理 3 位 HEX 颜色值(如 #fff)
  1488. if (hex.length === 3) {
  1489. hex = hex.split('').map(function (hex) {
  1490. return hex + hex;
  1491. }).join('');
  1492. }
  1493. // 解析 HEX 颜色值
  1494. const r = parseInt(hex.substring(0, 2), 16);
  1495. const g = parseInt(hex.substring(2, 4), 16);
  1496. const b = parseInt(hex.substring(4, 6), 16);
  1497. // 返回 RGBA 颜色值
  1498. return `rgba(${r}, ${g}, ${b}, ${alpha})`;
  1499. }
  1500. // =========================
  1501. // 模板樣式
  1502. // =========================
  1503. // JavaScript
  1504. document.addEventListener("DOMContentLoaded", function () {
  1505. // 獲取所有帶有 templateImg class 的圖像元素
  1506. var templateImgs = document.querySelectorAll('.templateImg');
  1507. // 為每個圖像元素添加點擊事件監聽器
  1508. templateImgs.forEach(function (img) {
  1509. img.addEventListener('click', function () {
  1510. // 在點擊時印出圖像元素的 value 屬性值
  1511. console.log(this.getAttribute('value'));
  1512. });
  1513. });
  1514. });
  1515. // =========================
  1516. var bgImgelement = document.getElementById('data_chart_box');
  1517. var chartWhitelement = document.getElementById('data_chartJs_box');
  1518. // 背景樣式
  1519. // =========================
  1520. // JavaScript
  1521. document.addEventListener("DOMContentLoaded", function () {
  1522. // 獲取所有帶有 templateImg class 的圖像元素
  1523. var bgImgs = document.querySelectorAll('.bgImg');
  1524. // 為每個圖像元素添加點擊事件監聽器
  1525. bgImgs.forEach(function (img) {
  1526. img.addEventListener('click', function () {
  1527. // 在點擊時印出圖像元素的 value 屬性值
  1528. console.log(this.getAttribute('value'));
  1529. var bgImgUrl = this.getAttribute('value');
  1530. chart_bgimg_url = 'url(' + bgImgUrl + ')';
  1531. if (bgImgUrl === './img/bg07.webp') {
  1532. chartTitle.style.color = "#ffff";
  1533. chartTitle.style.padding = '0px'
  1534. }
  1535. else {
  1536. chartTitle.style.color = "#1c2d6d";
  1537. }
  1538. if (bgImgUrl === "") {
  1539. chartWhitelement.style.background = "";
  1540. console.log('無背景');
  1541. bgImgelement.style.backgroundImage = "";
  1542. chartWhitelement.style.background = 'rgba(255, 255, 255, 0) !important;'
  1543. } else {
  1544. bgImgelement.style.backgroundImage = chart_bgimg_url;
  1545. chartWhitelement.style.background = "rgba(255, 255, 255, 0.5)"
  1546. }
  1547. // bgImgelement.style.backgroundSize = 'cover'; // 調整背景圖片大小
  1548. // bgImgelement.style.backgroundPosition = 'center'; // 調整背景圖片位置
  1549. // bgImgelement.style.backgroundRepeat = 'no-repeat'; // 調整背景圖片重複
  1550. });
  1551. });
  1552. });
  1553. // =========================
  1554. const radioButtons = document.querySelectorAll('input[name="inlineRadioOptions"]');
  1555. var chartTitle = document.querySelector('.chart_title');
  1556. radioButtons.forEach(button => {
  1557. button.addEventListener('click', function () {
  1558. if (this.value === '0') {
  1559. chartTitle.style.textAlign = 'left';
  1560. } else if (this.value === '1') {
  1561. chartTitle.style.textAlign = 'center';
  1562. } else if (this.value === '2') {
  1563. chartTitle.style.textAlign = 'right';
  1564. }
  1565. });
  1566. });
  1567. // 圖表標題
  1568. var keywordInput = document.getElementById('title_data');
  1569. // 初始时将输入框的值设置为标题的文本内容
  1570. keywordInput.value = chartTitle.textContent;
  1571. // 监听输入框的输入事件,实现内容同步更新
  1572. keywordInput.addEventListener('input', function () {
  1573. chartTitle.textContent = this.value;
  1574. });
  1575. // 標題字體大小
  1576. // ==========================
  1577. var TitlefontSize = document.getElementById('TitlefontSize');
  1578. TitlefontSize.addEventListener('input', function () {
  1579. var TitlefontSizeValue = TitlefontSize.value;
  1580. chartTitle.style.fontSize = TitlefontSizeValue + 'px';;
  1581. });
  1582. // 標題字體顏色
  1583. var stocknumber;
  1584. // ==========================
  1585. let labelDependent = ""; // 圖表左側 Label
  1586. function get_data(input_text_value) {
  1587. clickExample.style.display = 'none'
  1588. // 重新创建取消令牌和源
  1589. cancelToken = axios.CancelToken;
  1590. source = cancelToken.source();
  1591. // inputField.value = "";
  1592. apiLoading();
  1593. var existingTable = chartJsBox.querySelector('table');
  1594. downloadButton.style.display = "none";
  1595. if (chartDiv.childElementCount != 0) {
  1596. data = [];
  1597. labels = [];
  1598. console.log('已存在')
  1599. chartTitle.textContent = "";
  1600. unit_value.textContent = "";
  1601. bgImgelement.style.backgroundImage = "";
  1602. chartWhitelement.style.background = "";
  1603. document.getElementById('img_box_url').src = "";
  1604. }
  1605. if (existingTable) {
  1606. unit_value.textContent = "";
  1607. chartJsBox.removeChild(existingTable);
  1608. chartTitle.textContent = "";
  1609. bgImgelement.style.backgroundImage = "";
  1610. chartWhitelement.style.background = "";
  1611. document.getElementById('img_box_url').src = "";
  1612. }
  1613. if (myChart) {
  1614. unit_value_box.style.display = 'none';
  1615. unit_value.textContent = "";
  1616. data = [];
  1617. labels = [];
  1618. chartTitle.textContent = "";
  1619. bgImgelement.style.backgroundImage = "";
  1620. chartWhitelement.style.background = "";
  1621. myChart.destroy();
  1622. document.getElementById('img_box_url').src = "";
  1623. }
  1624. const apiUrl = `https://cmm.ai:8080/answer_with_token?token=${accessToken}&question=${input_text_value}`;
  1625. axios.get(apiUrl, {
  1626. cancelToken: source.token // 将取消令牌传递给 Axios 请求
  1627. })
  1628. .then(response => {
  1629. apiHideLoading();
  1630. console.log(response);
  1631. if (response.data.data == "未達相似度搜尋標準") {
  1632. alert('無法生成圖表')
  1633. return
  1634. } else {
  1635. unit_value_box.style.display = "block";
  1636. var chart_info = response.data.chart_info.Title;
  1637. chartType = response.data.chart_info.Chart_type;
  1638. var finance_img_url = response.data.imageUrl_info.imageUrl;
  1639. if (response.data.imageUrl_info != null) {
  1640. console.log(finance_img_url);
  1641. document.getElementById('img_box_url').src = finance_img_url;
  1642. }
  1643. keywordInput.value = chart_info;
  1644. chartTitle.textContent = chart_info;
  1645. unit = response.data.chart_info.Unit_of_dependent_variable;
  1646. var Label_dependent_variable = response.data.chart_info.Label_dependent_variable;
  1647. var Label_independent_variable = response.data.chart_info.Label_independent_variable;
  1648. stocknumber = response.data.chart_info.args;
  1649. chartTypeBtn.value = chartType;
  1650. unit_data.value = response.data.chart_info.Unit_of_dependent_variable;
  1651. if (response.data.chart_info.Unit_of_dependent_variable == "") {
  1652. unit_value.textContent = ""
  1653. } else {
  1654. unit_value.textContent = response.data.chart_info.Unit_of_dependent_variable;
  1655. }
  1656. obj.text = chartType;
  1657. dataArray = response.data.data;
  1658. if (chartType === "table") {
  1659. unit_value.textContent = "";
  1660. unit_value_box.style.display = "none"
  1661. createTable(dataArray, unit, Label_dependent_variable, Label_independent_variable);
  1662. // document.getElementById('img_box_url').src = "";
  1663. } else if (chartType === "bar") {
  1664. unit_value_box.style.display = 'block';
  1665. console.log('圖表類型bar')
  1666. $('#textToChart').hide();
  1667. extractAndGenerateChart(dataArray);
  1668. } else if (chartType === "doughnut") {
  1669. unit_value.textContent = "";
  1670. $('#textToChart').hide();
  1671. generatePieChart(dataArray, pieFontSize, PieMaxWidth)
  1672. } else {
  1673. unit_value_box.style.display = "block"
  1674. $('#chartdiv').hide();
  1675. generateChart(dataArray);
  1676. }
  1677. // }
  1678. // 在這裡處理成功獲取 JSON 的情況
  1679. // console.log(response);
  1680. // switch (chartType) {
  1681. // case "line":
  1682. // document.getElementById("inlineRadio4").checked = true; // 折线图
  1683. // break;
  1684. // case "bar":
  1685. // document.getElementById("inlineRadio5").checked = true; // 柱状图
  1686. // labelDependent = response.data.chart_info.Label_dependent_variable;
  1687. // break;
  1688. // case "pie":
  1689. // document.getElementById("inlineRadio6").checked = true; // 圆饼图
  1690. // break;
  1691. // case "table":
  1692. // document.getElementById("inlineRadio7").checked = true; // 表格
  1693. // break;
  1694. // default:
  1695. // // 默认情况
  1696. }
  1697. // generateChart(dataArray);
  1698. })
  1699. .catch(error => {
  1700. // 错误处理逻辑,包括取消请求的情况
  1701. apiHideLoading();
  1702. if (axios.isCancel(error)) {
  1703. console.log('请求被取消:', error.message);
  1704. } else {
  1705. console.error('发生错误:', error);
  1706. }
  1707. });
  1708. }
  1709. // 取消请求的函数
  1710. function cancelRequest() {
  1711. if (source) {
  1712. source.cancel('取消了 API 请求');
  1713. apiHideLoading(); // 隐藏加载状态
  1714. }
  1715. }
  1716. document.getElementById('cancel_send_data').addEventListener('click', cancelRequest);
  1717. $(document).ready(function () {
  1718. $('#dataTable').DataTable();
  1719. });
  1720. function resizeChart() {
  1721. console.log(myChart)
  1722. if (myChart != null) {
  1723. myChart.resize();
  1724. }
  1725. }
  1726. document.addEventListener('visibilitychange', function () {
  1727. console.log('Visibility changed:', document.visibilityState);
  1728. if (document.visibilityState === 'visible') {
  1729. resizeChart();
  1730. }
  1731. });
  1732. // 监听窗口resize事件
  1733. window.addEventListener('resize', resizeChart);
  1734. var setting_button = document.getElementById('chart_seeting_button');
  1735. var closeButton = document.getElementById('style_setting_close');
  1736. var closeButton_table = document.getElementById('style_setting_close_table');
  1737. // var borderSetting = document.getElementById('borderSetting');
  1738. var lineChartSetting = document.getElementById('lineChartSetting');
  1739. var barChartSetting = document.getElementById('barChartSetting');
  1740. var table_setting = document.getElementById('table_setting');
  1741. var chart_category = document.getElementById('chart_category');
  1742. var pie_setting = document.getElementById('pie_setting');
  1743. // 获取 chartDiv 元素
  1744. setting_button.addEventListener('click', () => {
  1745. console.log('setting_button')
  1746. if (myChart === null && !chartJsBox.querySelector('table') && chartDiv.childElementCount === 0) {
  1747. alert('請先生成圖表');
  1748. return
  1749. }
  1750. if (chartType === 'table') {
  1751. console.log('表格');
  1752. sidebar.classList.toggle('show');
  1753. sidebar.classList.toggle('hidden');
  1754. barChartSetting.style.display = 'none';
  1755. lineChartSetting.style.display = 'none';
  1756. table_setting.style.display = 'block';
  1757. chart_category.style.display = 'none';
  1758. pie_setting.style.display = 'none';
  1759. } else if (chartType === "bar") {
  1760. console.log('柱狀圖')
  1761. sidebar.classList.toggle('show');
  1762. sidebar.classList.toggle('hidden');
  1763. barChartSetting.style.display = 'block';
  1764. lineChartSetting.style.display = 'none';
  1765. table_setting.style.display = 'none';
  1766. pie_setting.style.display = 'none';
  1767. } else if (chartType === "doughnut") {
  1768. console.log('快捷功能圓餅圖')
  1769. sidebar.classList.toggle('show');
  1770. sidebar.classList.toggle('hidden');
  1771. barChartSetting.style.display = 'none';
  1772. lineChartSetting.style.display = 'none';
  1773. pie_setting.style.display = 'block';
  1774. table_setting.style.display = 'none';
  1775. chart_category.style.display = 'none';
  1776. document.getElementById('unit_input').style.display = 'none';
  1777. } else {
  1778. sidebar.classList.toggle('show');
  1779. sidebar.classList.toggle('hidden');
  1780. table_setting.style.display = 'none';
  1781. barChartSetting.style.display = 'none';
  1782. lineChartSetting.style.display = 'block';
  1783. pie_setting.style.display = 'none';
  1784. }
  1785. });
  1786. closeButton.addEventListener('click', () => {
  1787. sidebar.classList.remove('show');
  1788. sidebar.classList.add('hidden');
  1789. });
  1790. const finance_options = [
  1791. { name: "原油油桶", imageUrl: "./img/finance/01.webp" },
  1792. { name: "黃金", imageUrl: "./img/finance/02.webp" },
  1793. { name: "房屋", imageUrl: "./img/finance/03.webp" },
  1794. { name: "車", imageUrl: "./img/finance/04.webp" },
  1795. { name: "股市", imageUrl: "./img/finance/05.webp" },
  1796. { name: "晶圓", imageUrl: "./img/finance/06.webp" },
  1797. { name: "TSMC 台積電", imageUrl: "./img/finance/07.webp" },
  1798. { name: "台幣", imageUrl: "./img/finance/08.webp" },
  1799. { name: "美金", imageUrl: "./img/finance/09.webp" },
  1800. { name: "購物車", imageUrl: "./img/finance/10.webp" },
  1801. { name: "美中貿易", imageUrl: "./img/finance/11.webp" },
  1802. { name: "世界地圖", imageUrl: "./img/finance/12.webp" },
  1803. { name: "小麥", imageUrl: "./img/finance/13.webp" },
  1804. { name: "玉米", imageUrl: "./img/finance/14.webp" },
  1805. { name: "智慧產線", imageUrl: "./img/finance/15.webp" }
  1806. ];
  1807. const selectOptions = document.getElementById('finance_options');
  1808. const selectOptions_table = document.getElementById('finance_options_table');
  1809. finance_options.forEach((option, index) => {
  1810. const optionElement = document.createElement('option');
  1811. optionElement.value = index + 1; // 值从 1 开始
  1812. optionElement.textContent = option.name;
  1813. optionElement.setAttribute('data-image-url', option.imageUrl); // 存储图片 URL
  1814. selectOptions.appendChild(optionElement);
  1815. });
  1816. selectOptions.addEventListener('change', function () {
  1817. const selectedOption = selectOptions.options[selectOptions.selectedIndex];
  1818. const imageUrl = selectedOption.getAttribute('data-image-url');
  1819. document.getElementById('img_box_url').src = imageUrl;
  1820. });
  1821. // finance_options.forEach((option, index) => {
  1822. // const optionElement = document.createElement('option');
  1823. // optionElement.value = index + 1; // 值从 1 开始
  1824. // optionElement.textContent = option.name;
  1825. // optionElement.setAttribute('data-image-url', option.imageUrl); // 存储图片 URL
  1826. // selectOptions_table.appendChild(optionElement);
  1827. // });
  1828. // selectOptions_table.addEventListener('change', function () {
  1829. // const selectedOption = selectOptions_table.options[selectOptions_table.selectedIndex];
  1830. // const imageUrl = selectedOption.getAttribute('data-image-url');
  1831. // document.getElementById('img_box_url').src = imageUrl;
  1832. // });
  1833. document.querySelectorAll('input[name="ChartOptions"]').forEach(function (radio) {
  1834. radio.addEventListener('change', function () {
  1835. // 檢查哪個單選按鈕被選中
  1836. if (this.value === 'line') {
  1837. // 如果選擇了折線圖,執行相應的操作
  1838. console.log("選擇了折線圖");
  1839. // 在這裡執行顯示折線圖的相關代碼
  1840. } else if (this.value === 'doughnut') {
  1841. // 如果選擇了柱狀圖,執行相應的操作
  1842. console.log("選擇了柱狀圖");
  1843. // 在這裡執行顯示柱狀圖的相關代碼
  1844. }
  1845. });
  1846. });
  1847. function tokencheck() {
  1848. if (!localStorage.getItem("access_token")) {
  1849. window.location.href = "./login.html";
  1850. } else {
  1851. console.log("存在");
  1852. }
  1853. }
  1854. tokencheck();
  1855. function logout() {
  1856. localStorage.removeItem("access_token");
  1857. }
  1858. $(document).on("click", "#logout", function (event) {
  1859. alert("登出成功");
  1860. logout();
  1861. location.reload();
  1862. });
  1863. // speech_to_text
  1864. var closeRecord = document.getElementById('recording_block_close');
  1865. var recording_block = document.getElementById('recording_block');
  1866. var PageLM = "2024-05-07 18:33";
  1867. var recording_button = document.getElementById('recording_button');
  1868. var stopButton = document.getElementById('stop');
  1869. var audio = document.getElementById('audio');
  1870. var recorder, audioBlob;
  1871. closeRecord.addEventListener('click', () => {
  1872. recPause();
  1873. console.log('record-click')
  1874. recording_block.classList.remove('show');
  1875. recording_block.classList.add('hidden');
  1876. });
  1877. function reclog(s, color) {
  1878. var now = new Date();
  1879. var t = ("0" + now.getHours()).substr(-2)
  1880. + ":" + ("0" + now.getMinutes()).substr(-2)
  1881. + ":" + ("0" + now.getSeconds()).substr(-2);
  1882. var div = document.createElement("div");
  1883. var elem = document.querySelector(".reclog");
  1884. elem.insertBefore(div, elem.firstChild);
  1885. div.innerHTML = '<div style="color:' + (!color ? "" : color == 1 ? "red" : color == 2 ? "#0b1" : color) + '">[' + t + ']' + s + '</div>';
  1886. };
  1887. window.onerror = function (message, url, lineNo, columnNo, error) {
  1888. //https://www.cnblogs.com/xianyulaodi/p/6201829.html
  1889. reclog('<span style="color:red">【Uncaught Error】' + message + '<pre>' + "at:" + lineNo + ":" + columnNo + " url:" + url + "\n" + (error && error.stack || Html_$T("kBaF::不能获得错误堆栈")) + '</pre></span>');
  1890. };
  1891. if (!window.Html_$T) {//没有提供本页面用的国际化多语言支持时 返回中文文本
  1892. window.Html_$T = function () {
  1893. var a = arguments, txt = a[0].replace(/^.+?::/, ""), n = 0;
  1894. for (var i = 0; i < a.length; i++) { if (typeof a[i] == "number") { n = i; break } }
  1895. txt = txt.replace(/\{(\d+)\}/g, function (v, f) { v = a[+f + n]; return v == null ? "" : v });
  1896. return txt;
  1897. }
  1898. window.Html_xT = function (v) { return v }
  1899. }
  1900. if (window.Recorder) {
  1901. // reclog(Html_$T('BL9u::頁面已準備好,請先點擊打開錄音,然後點擊錄製'), 2);
  1902. alert('錄音已準備好,請先點擊右下角打開錄音,即可開始錄製')
  1903. } else {
  1904. reclog(Html_$T("YzPd::js文件加载失败,请刷新重试!"), "#f00;font-size:50px");
  1905. console.log('js文件加载失敗,请刷新重试!')
  1906. }
  1907. recording_button.addEventListener('click', async () => {
  1908. recording_block.classList.toggle('show');
  1909. recording_block.classList.toggle('hidden');
  1910. recStart();
  1911. });
  1912. function recStart() {//打开了录音后才能进行start、stop调用
  1913. rec.start();
  1914. };
  1915. stopButton.addEventListener('click', async () => {
  1916. // recorder.stop();
  1917. // uploadAudio(audioBlob);
  1918. recStop();
  1919. });
  1920. window.onload = function () {
  1921. recOpen();
  1922. };
  1923. // 錄音程式
  1924. // =======================
  1925. var rec, wave, recBlob;
  1926. /**调用open打开录音请求好录音权限 Call open to open the recording and request the recording permission**/
  1927. var recOpen = function () {//一般在显示出录音按钮或相关的录音界面时进行此方法调用,后面用户点击开始录音时就能畅通无阻了
  1928. rec = null;
  1929. wave = null;
  1930. recBlob = null;
  1931. var newRec = Recorder({
  1932. type: "mp3", sampleRate: 16000, bitRate: 16 //mp3格式,指定采样率hz、比特率kbps,其他参数使用默认配置;注意:是数字的参数必须提供数字,不要用字符串;需要使用的type类型,需提前把格式支持文件加载进来,比如使用wav格式需要提前加载wav.js编码引擎
  1933. , onProcess: function (buffers, powerLevel, bufferDuration, bufferSampleRate, newBufferIdx, asyncEnd) {
  1934. // // //录音实时回调,大约1秒调用12次本回调
  1935. document.querySelector(".recpowerx").style.width = powerLevel + "%";
  1936. document.querySelector(".recpowert").innerText = formatMs(bufferDuration, 1) + " / " + powerLevel;
  1937. // //可视化图形绘制
  1938. // wave.input(buffers[buffers.length - 1], powerLevel, bufferSampleRate);
  1939. }
  1940. });
  1941. newRec.open(function () {//打开麦克风授权获得相关资源
  1942. rec = newRec;
  1943. //此处创建这些音频可视化图形绘制浏览器支持妥妥的
  1944. // wave = Recorder.FrequencyHistogramView({ elem: ".recwave" });
  1945. reclog(Html_$T("GVCa::已打開錄音,可以點擊錄制開始錄音"), 2);
  1946. }, function (msg, isUserNotAllow) {//用户拒绝未授权或不支持
  1947. reclog((isUserNotAllow ? "UserNotAllow, " : "") + Html_$T("TOOV::打開錄音失敗:") + msg, 1);
  1948. });
  1949. };
  1950. /**关闭录音,释放资源 Close recording, release resources**/
  1951. function recClose() {
  1952. if (rec) {
  1953. rec.close();
  1954. reclog(Html_$T("jqOs::已关闭"));
  1955. } else {
  1956. reclog(Html_$T("VOOw::未打开录音"), 1);
  1957. };
  1958. };
  1959. /**开始录音 Start recording**/
  1960. function recStart() {//打开了录音后才能进行start、stop调用
  1961. if (rec && Recorder.IsOpen()) {
  1962. recBlob = null;
  1963. rec.start();
  1964. reclog(Html_$T("CGdy::已經開始錄音...") + " " + rec.set.type + " " + rec.set.sampleRate + " " + rec.set.bitRate + "kbps");
  1965. } else {
  1966. reclog(Html_$T("ajKR::未打开錄音"), 1);
  1967. };
  1968. };
  1969. /**暂停录音 Passing recording**/
  1970. function recPause() {
  1971. if (rec && Recorder.IsOpen()) {
  1972. rec.pause();
  1973. reclog(Html_$T("GvCy::已暂停"));
  1974. } else {
  1975. reclog(Html_$T("gCAR::未打開錄音"), 1);
  1976. };
  1977. };
  1978. /**恢复录音 Resume recording**/
  1979. function recResume() {
  1980. if (rec && Recorder.IsOpen()) {
  1981. rec.resume();
  1982. reclog(Html_$T("5q1K::继续录音中..."));
  1983. } else {
  1984. reclog(Html_$T("Ob6S::未打开录音"), 1);
  1985. };
  1986. };
  1987. /**结束录音,得到音频文件 Stop recording and get audio files**/
  1988. function recStop() {
  1989. if (!(rec && Recorder.IsOpen())) {
  1990. reclog(Html_$T("5JuL::未打開錄音"), 1);
  1991. return;
  1992. };
  1993. rec.stop(function (blob, duration) {
  1994. console.log(blob, (window.URL || webkitURL).createObjectURL(blob), Html_xT(Html_$T("gOix::時長:{1}ms", 0, duration)));
  1995. recBlob = blob;
  1996. var file = new File([blob], "recording.mp3", { type: "audio/mp3" });
  1997. // console.log(file);
  1998. handleAudioToText(file);
  1999. reclog(Html_$T("0LHf::已錄製mp3:{1}ms {2}字節,可以點擊播放、上傳、本地下载了", 0, formatMs(duration), blob.size), 2);
  2000. }, function (msg) {
  2001. reclog(Html_$T("kGZO::錄音失敗:") + msg, 1);
  2002. });
  2003. };
  2004. /**播放 Play**/
  2005. function recPlay() {
  2006. if (!recBlob) {
  2007. reclog(Html_$T("tIke::请先錄音,然后停止後再播放"), 1);
  2008. return;
  2009. };
  2010. var cls = ("a" + Math.random()).replace(".", "");
  2011. reclog(Html_$T('GlWb::播放中: ') + '<span class="' + cls + '"></span>');
  2012. var audio = document.createElement("audio");
  2013. audio.controls = true;
  2014. document.querySelector("." + cls).appendChild(audio);
  2015. //简单利用URL生成播放地址,注意不用了时需要revokeObjectURL,否则霸占内存
  2016. audio.src = (window.URL || webkitURL).createObjectURL(recBlob);
  2017. audio.play();
  2018. setTimeout(function () {
  2019. (window.URL || webkitURL).revokeObjectURL(audio.src);
  2020. }, 5000);
  2021. };
  2022. /**上传 Upload**/
  2023. function recUpload() {
  2024. var blob = recBlob;
  2025. if (!blob) {
  2026. reclog(Html_$T("DUTn::请先录音,然后停止后再上传"), 1);
  2027. return;
  2028. };
  2029. //本例子假设使用原始XMLHttpRequest请求方式,实际使用中自行调整为自己的请求方式
  2030. //录音结束时拿到了blob文件对象,可以用FileReader读取出内容,或者用FormData上传
  2031. var api = "http://127.0.0.1:9528";
  2032. var onreadystatechange = function (xhr, title) {
  2033. return function () {
  2034. if (xhr.readyState == 4) {
  2035. if (xhr.status == 200) {
  2036. reclog(title + Html_$T("G2MU::上传成功") + ' <span style="color:#999">response: ' + xhr.responseText + '</span>', 2);
  2037. } else {
  2038. reclog(title + Html_$T("TUdi::没有完成上传,演示上传地址无需关注上传结果,只要浏览器控制台内Network面板内看到的请求数据结构是预期的就ok了。"), "#d8c1a0");
  2039. console.error(Html_xT(title + Html_$T("HjDi::上传失败")), xhr.status, xhr.responseText);
  2040. };
  2041. };
  2042. };
  2043. };
  2044. reclog(Html_$T("QnSI::开始上传到{1},请稍候... (你可以先到源码 /assets/node-localServer 目录内执行 npm run start 来运行本地测试服务器)", 0, api));
  2045. /***方式一:将blob文件转成base64纯文本编码,使用普通application/x-www-form-urlencoded表单上传***/
  2046. var reader = new FileReader();
  2047. reader.onloadend = function () {
  2048. var postData = "";
  2049. postData += "mime=" + encodeURIComponent(blob.type);//告诉后端,这个录音是什么格式的,可能前后端都固定的mp3可以不用写
  2050. postData += "&upfile_b64=" + encodeURIComponent((/.+;\s*base64\s*,\s*(.+)$/i.exec(reader.result) || [])[1]) //录音文件内容,后端进行base64解码成二进制
  2051. //...其他表单参数
  2052. var xhr = new XMLHttpRequest();
  2053. xhr.open("POST", api + "/uploadBase64");
  2054. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  2055. xhr.onreadystatechange = onreadystatechange(xhr, Html_$T("gG1f::上传方式一【Base64】"));
  2056. xhr.send(postData);
  2057. };
  2058. reader.readAsDataURL(blob);
  2059. /***方式二:使用FormData用multipart/form-data表单上传文件***/
  2060. var form = new FormData();
  2061. form.append("upfile", blob, "recorder.mp3"); //和普通form表单并无二致,后端接收到upfile参数的文件,文件名为recorder.mp3
  2062. //...其他表单参数
  2063. var xhr = new XMLHttpRequest();
  2064. xhr.open("POST", api + "/upload");
  2065. xhr.onreadystatechange = onreadystatechange(xhr, Html_$T("vDzB::上传方式二【FormData】"));
  2066. xhr.send(form);
  2067. };
  2068. /**本地下载 Local download**/
  2069. function recLocalDown() {
  2070. if (!recBlob) {
  2071. reclog(Html_$T("M86h::请先录音,然后停止后再下载"), 1);
  2072. return;
  2073. };
  2074. var cls = ("a" + Math.random()).replace(".", "");
  2075. recdown64.lastCls = cls;
  2076. reclog(Html_$T('vJPl::点击 ') + '<span class="' + cls + '"></span>' + Html_$T('Whtc:: 下载,或复制文本')
  2077. + '<button onclick="recdown64(\'' + cls + '\')">' + Html_$T('XK4l::生成Base64文本') + '</button><span class="' + cls + '_b64"></span>');
  2078. var fileName = "recorder-" + Date.now() + ".mp3";
  2079. var downA = document.createElement("A");
  2080. downA.innerHTML = Html_$T("g8Fy::下载 ") + fileName;
  2081. downA.href = (window.URL || webkitURL).createObjectURL(recBlob);
  2082. downA.download = fileName;
  2083. document.querySelector("." + cls).appendChild(downA);
  2084. if (/mobile/i.test(navigator.userAgent)) {
  2085. alert(Html_xT(Html_$T("DIEK::因移动端绝大部分国产浏览器未适配Blob Url的下载,所以本demo代码在移动端未调用downA.click()。请尝试点击日志中显示的下载链接下载")));
  2086. } else {
  2087. downA.click();
  2088. }
  2089. //不用了时需要revokeObjectURL,否则霸占内存
  2090. //(window.URL||webkitURL).revokeObjectURL(downA.href);
  2091. };
  2092. function recdown64(cls) {
  2093. var el = document.querySelector("." + cls + "_b64");
  2094. if (recdown64.lastCls != cls) {
  2095. el.innerHTML = '<span style="color:red">' + Html_$T("eKKx::老的数据没有保存,只支持最新的一条") + '</span>';
  2096. return;
  2097. }
  2098. var reader = new FileReader();
  2099. reader.onloadend = function () {
  2100. el.innerHTML = '<textarea></textarea>';
  2101. el.querySelector("textarea").value = reader.result;
  2102. };
  2103. reader.readAsDataURL(recBlob);
  2104. };
  2105. var formatMs = function (ms, all) {
  2106. var ss = ms % 1000; ms = (ms - ss) / 1000;
  2107. var s = ms % 60; ms = (ms - s) / 60;
  2108. var m = ms % 60; ms = (ms - m) / 60;
  2109. var h = ms;
  2110. var t = (h ? h + ":" : "")
  2111. + (all || h + m ? ("0" + m).substr(-2) + ":" : "")
  2112. + (all || h + m + s ? ("0" + s).substr(-2) + "″" : "")
  2113. + ("00" + ss).substr(-3);
  2114. return t;
  2115. };
  2116. // =======================
  2117. let mediaRecorder;
  2118. let audioChunks = [];
  2119. var recordText = document.getElementById('record_text');
  2120. async function handleAudioToText(file) {
  2121. console.log('tts')
  2122. let audioLang = "cmn-Hant-TW"; // 音訊語言
  2123. // let lang = localStorage.getItem("lang");
  2124. let lang = "zh-tw";
  2125. console.log("lang", lang);
  2126. switch (lang) {
  2127. case "zh-tw":
  2128. audioLang = "cmn-Hant-TW";
  2129. break;
  2130. case "en-us":
  2131. audioLang = "en-US";
  2132. break;
  2133. case "ja-jp":
  2134. audioLang = "ja-JP";
  2135. break;
  2136. case "ko-kr":
  2137. audioLang = "ko-KR";
  2138. break;
  2139. default:
  2140. break;
  2141. }
  2142. let url = `https://cmm.ai:9001/gcp/speech-to-text?language_code=${audioLang}`;
  2143. const formData = new FormData();
  2144. formData.append("file", file);
  2145. try {
  2146. const response = await axios.post(url, formData);
  2147. console.log("response", response);
  2148. input_text_value = response.data;
  2149. recording_block.classList.remove('show');
  2150. recording_block.classList.add('hidden');
  2151. if (input_text_value.length === 0) {
  2152. alert('請再說一次')
  2153. return
  2154. }
  2155. console.log(input_text_value[0]);
  2156. inputField.value = input_text_value[0];
  2157. // Show the result for a short time before executing get_data
  2158. get_data(input_text_value);
  2159. // get_data(input_text_value);
  2160. } catch (error) {
  2161. console.log("error", error);
  2162. }
  2163. }
  2164. Object.defineProperty(obj, 'text', {
  2165. get() {
  2166. return this._text;
  2167. },
  2168. set(newValue) {
  2169. console.log(`字符串从 "${this._text}" 变为 "${newValue}"`);
  2170. this._text = newValue;
  2171. }
  2172. });