text-to-chart.js 86 KB

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