123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823 |
- console.log('text-to-chart');
- var accessToken = localStorage.getItem("access_token");
- console.log(accessToken);
- if (accessToken === null) {
- alert('請先登入')
- window.location.href = "./login.html";
- }
- var usernameEmail = localStorage.getItem("username");
- var unit_table_value
- var obj = {
- _text: ''
- };
- // let username = usernameEmail.split('@')[0];
- Chart.register(ChartDataLabels);
- var userNameSpan = document.getElementById("userName");
- var lineYfontSize = document.getElementById("lineYfontSize");
- userNameSpan.textContent = usernameEmail;
- var inputField = document.getElementById("keyword_data");
- var inputField_compare = document.getElementById("keyword_data_compare");
- var compare_box = document.getElementById("compare");
- var sendButton = document.getElementById("send_data");
- var send_data_compare = document.getElementById("data_compare");
- var canvasPng = document.getElementById("textToChart");
- var img_box = document.getElementById("img_box");
- var sidebar = document.getElementById('style_setting');
- var input_text_value;
- var chartType = 'line';
- var unit = '';
- var myChart = null;
- var chartColor = '#912B2B';
- var chartColorBar = '#'
- var displayXaxes = true;
- var ctx;
- var chartlabels;
- var chartdata;
- var XfontSizeValue = 18;
- var YfontSizeValue = 18;
- var TitlefontSizeValue = 32;
- var pieFontSize = 14;
- var PieMaxWidth = 100;
- var chart_bgimg_url = "url(./img/bg06.webp)";
- var vocab = [];
- var chartDiv = document.getElementById('chartdiv');
- var dataFirstValue;
- var dataLastValue;
- let cancelToken;
- let source;
- $('#chartdiv').hide();
- var clickExample = document.getElementById('example_box')
- document.addEventListener('DOMContentLoaded', function () {
- axios.get("https://cmm.ai:8080/vocab")
- .then(response => {
- // apiHideLoading();
- console.log(response);
- vocab = response.data;
- })
- .catch(error => {
- console.error('發生錯誤:', error);
- });
- });
- inputField.addEventListener('click', function () {
- // 確保輸入框處於焦點狀態
- inputField.focus();
- // 在控制台中顯示焦點狀態
- console.log('Input field focused');
- });
- document.addEventListener('DOMContentLoaded', function () {
- // const input = document.getElementById('stockInput');
- const suggestions = document.getElementById('suggestions');
- inputField.addEventListener('input', function () {
- const query = this.value.trim();
- suggestions.innerHTML = ''; // 清空之前的建議
- console.log('query', query);
- if (query) {
- // const filteredVocab = vocab.filter(item => item.toLowerCase().includes(query));
- const queryTerms = query.split(/\s+/); // 分割輸入的內容
- const lastQueryTerm = queryTerms[queryTerms.length - 1];
- const filteredVocab = vocab.filter(item => {
- // 對每個分割後的單詞進行匹配,只要其中有一個匹配成功就返回 true
- return item.toLowerCase().includes(lastQueryTerm.toLowerCase());
- });
- if (filteredVocab.length > 0) {
- suggestions.style.border = '1px solid #ccc';
- suggestions.style.display = 'block'; // 顯示建議容器
- } else {
- suggestions.style.border = 'none';
- suggestions.style.display = 'none'; // 隱藏建議容器
- }
- console.log(queryTerms[queryTerms.length - 1])
- filteredVocab.forEach(item => {
- const div = document.createElement('div');
- div.classList.add('autocomplete-suggestion');
- div.textContent = item;
- // div.addEventListener('click', function () {
- // inputField.value = item;
- // suggestions.innerHTML = ''; // 清空建議
- // suggestions.style.border = 'none';
- // suggestions.style.display = 'none'; // 隱藏建議容器
- // });
- // console.log('queryTerms', queryTerms);
- // console.log('filteredVocab', filteredVocab);
- div.addEventListener('click', function () {
- queryTerms.forEach((term, index) => {
- const regex = new RegExp(term, 'gi');
- const match = regex.exec(inputField.value);
- // console.log(queryTerms[queryTerms.length - 1]);
- // console.log(filteredVocab, queryTerms[queryTerms.length - 1])
- if (index === queryTerms.length - 1) {
- if (match) {
- var start = match.index;
- var end = start + match[0].length;
- console.log(start, end)
- const textBefore = inputField.value.substring(0, start);
- const textAfter = inputField.value.substring(end, inputField.value.length);
- console.log('start', start, 'end', end, 'textBefore', textBefore, 'textAfter', textAfter);
- // 捕獲被選中的部分並替換它
- const selectedText = inputField.value.substring(start, end);
- console.log('selectedText', selectedText);
- const newText = textBefore + item + textAfter.replace(selectedText, '');
- console.log('newText', newText);
- inputField.value = newText;
- // inputField.value = textBefore + item.replace(selectedText, '') + textAfter;
- suggestions.innerHTML = ''; // 清空建議
- suggestions.style.border = 'none';
- }
- }
- });
- // const start = inputField.selectionStart;
- // const end = inputField.selectionEnd;
- });
- suggestions.appendChild(div);
- });
- } else {
- suggestions.style.border = 'none';
- suggestions.style.display = 'none'; // 隱藏建議容器
- }
- });
- });
- document.addEventListener('DOMContentLoaded', function () {
- // const input = document.getElementById('stockInput');
- const suggestions_compare = document.getElementById('suggestions_compare');
- inputField_compare.addEventListener('input', function () {
- const query = this.value.trim();
- suggestions_compare.innerHTML = ''; // 清空之前的建議
- console.log('query', query);
- if (query) {
- // const filteredVocab = vocab.filter(item => item.toLowerCase().includes(query));
- const queryTerms = query.split(/\s+/); // 分割輸入的內容
- const lastQueryTerm = queryTerms[queryTerms.length - 1];
- const filteredVocab = vocab.filter(item => {
- // 對每個分割後的單詞進行匹配,只要其中有一個匹配成功就返回 true
- return item.toLowerCase().includes(lastQueryTerm.toLowerCase());
- });
- if (filteredVocab.length > 0) {
- suggestions_compare.style.border = '1px solid #ccc';
- suggestions_compare.style.display = 'block'; // 顯示建議容器
- } else {
- suggestions_compare.style.border = 'none';
- suggestions_compare.style.display = 'none'; // 隱藏建議容器
- }
- console.log(queryTerms[queryTerms.length - 1])
- filteredVocab.forEach(item => {
- const div = document.createElement('div');
- div.classList.add('autocomplete-suggestion');
- div.textContent = item;
- // div.addEventListener('click', function () {
- // inputField.value = item;
- // suggestions.innerHTML = ''; // 清空建議
- // suggestions.style.border = 'none';
- // suggestions.style.display = 'none'; // 隱藏建議容器
- // });
- // console.log('queryTerms', queryTerms);
- // console.log('filteredVocab', filteredVocab);
- div.addEventListener('click', function () {
- queryTerms.forEach((term, index) => {
- const regex = new RegExp(term, 'gi');
- const match = regex.exec(inputField_compare.value);
- // console.log(queryTerms[queryTerms.length - 1]);
- // console.log(filteredVocab, queryTerms[queryTerms.length - 1])
- if (index === queryTerms.length - 1) {
- if (match) {
- var start = match.index;
- var end = start + match[0].length;
- console.log(start, end)
- const textBefore = inputField_compare.value.substring(0, start);
- const textAfter = inputField_compare.value.substring(end, inputField_compare.value.length);
- console.log('start', start, 'end', end, 'textBefore', textBefore, 'textAfter', textAfter);
- // 捕獲被選中的部分並替換它
- const selectedText = inputField_compare.value.substring(start, end);
- console.log('selectedText', selectedText);
- const newText = textBefore + item + textAfter.replace(selectedText, '');
- console.log('newText', newText);
- inputField_compare.value = newText;
- // inputField.value = textBefore + item.replace(selectedText, '') + textAfter;
- suggestions_compare.innerHTML = ''; // 清空建議
- suggestions_compare.style.border = 'none';
- }
- }
- });
- // const start = inputField.selectionStart;
- // const end = inputField.selectionEnd;
- });
- suggestions_compare.appendChild(div);
- });
- } else {
- suggestions_compare.style.border = 'none';
- suggestions_compare.style.display = 'none'; // 隱藏建議容器
- }
- });
- });
- // 監聽輸入框的鍵盤事件
- // document.getElementById("keyword_data").addEventListener("keyup", function (event) {
- // // 判斷是否按下 Enter 鍵 (key code: 13)
- // if (event.keyCode === 13) {
- // // 觸發送出按鈕的點擊事件
- // document.getElementById("send_data").click();
- // }
- // });
- // 定義按鈕點擊事件處理函數
- function sendButtonClickHandler() {
- // inputField.value = "";
- console.log(inputField.value);
- var input_text_value = inputField.value;
- compare_box.style.display = "none";
- $('#chartdiv').hide();
- sidebar.classList.remove('show');
- sidebar.classList.add('hidden');
- // 在這裡添加你希望在按下按鈕時執行的其他代碼
- get_data(input_text_value);
- }
- sendButton.addEventListener("click", function () {
- sendButtonClickHandler();
- inputField.value = ''
- chartTypeBtn.style.display = "none";
- });
- let labels2 = [];
- let data2 = [];
- // 漲幅or單價
- const checkbox1 = document.getElementById('checkbox1');
- const checkbox2 = document.getElementById('checkbox2');
- var compare_type_input = document.querySelector('.checkbox-group')
- var compare_type = "";
- checkbox1.addEventListener('change', () => {
- if (checkbox1.checked) {
- checkbox2.checked = false;
- compare_type = '漲幅'
- console.log(compare_type);
- }
- });
- checkbox2.addEventListener('change', () => {
- if (checkbox2.checked) {
- checkbox1.checked = false;
- compare_type = '單價'
- console.log(compare_type);
- }
- });
- send_data_compare.addEventListener("click", function () {
- console.log('比較', inputField_compare.value);
- const stockName = inputField_compare.value;
- axios
- .get(`https://cmm.ai:8080/get_data_from_date?start_date=${dataFirstValue}&end_date=${dataLastValue}&stock_name=${inputField_compare.value}`)
- .then((response) => {
- console.log(response);
- var compare_data = response.data.data;
- labels2 = []; // Clear previous labels
- data2 = []; // Clear previous data
- if (compare_type === "") {
- alert('請輸入比較類型');
- return
- }
- // console.log('比較送出後 ', chartType)
- if (response.data.data === "無法產生圖表") {
- alert('無法產生圖表')
- return
- }
- if (chartType === 'line') {
- const colors = [
- 'rgba(130, 163, 63, 1)',
- 'rgba(47, 72, 123, 1)',
- '#df8c49',
- '#72598f',
- '#489fb6',
- '#99b0d5'
- ];
- let colorIndex = 0;
- for (var i = 0; i < compare_data.length; i++) {
- var item = compare_data[i];
- // 遍历当前项的属性
- for (var key in item) {
- // 如果属性名不在 labels 数组中,并且属性值不是对象,则将属性名添加到 labels 数组中
- if (typeof item[key] === 'string') {
- labels2.push(item[key]);
- }
- // 如果属性值是数值类型,则将其添加到 data 数组中
- if (typeof item[key] === 'number') {
- data2.push(item[key]);
- }
- }
- }
- function getRandomColor() {
- const randomIndex = Math.floor(Math.random() * colors.length);
- const selectedColor = colors[randomIndex];
- colors.splice(randomIndex, 1); // 从数组中移除已选颜色
- return selectedColor;
- }
- const normalize = (prices) => {
- const initialPrice = prices[0];
- // return prices.map(price => (price - initialPrice) / initialPrice * 100);
- return prices.map(price => {
- // Calculate the normalized price
- const normalized = (price - initialPrice) / initialPrice * 100;
- // Round to 2 decimal places and convert back to a number
- return parseFloat(normalized.toFixed(2));
- });
- };
- var stock2Normalized;
- var stock1Normalized;
- if (compare_type === '漲幅') {
- console.log('漲幅')
- stock2Normalized = normalize(data2);
- stock1Normalized = normalize(data);
- unitInput.value = '%';
- unit_value.textContent = '%'
- } else {
- console.log('單價')
- unitInput.value = unitInput.value;
- unit_value.textContent = unitInput.value;
- stock2Normalized = data2;
- stock1Normalized = data;
- }
- const color = getRandomColor();
- const newDataset = {
- label: stockName,
- backgroundColor: colors,
- pointRadius: 0,
- pointHoverRadius: 0,
- borderColor: colors,
- data: stock2Normalized,
- fill: false
- };
- colorIndex = (colorIndex + 1) % colors.length;
- myChart.data.datasets.push(newDataset);
- myChart.data.datasets[0].data = stock1Normalized;
- console.log('多筆資料', myChart.data.datasets);
- // console.log('比較', myChart.options.plugins.legend);
- myChart.options.plugins.legend.display = true;
- // myChart.defaults.global.legend = {
- // display: true,
- // position: 'top', // 默认图例位置为顶部
- // labels: {
- // fontColor: 'rgb(255, 99, 132)', // 默认图例标签颜色
- // fontSize: 14 // 默认图例标签字体大小
- // }
- // };
- myChart.update();
- inputField_compare.value = '';
- compare_type_input.style.display = 'none'
- } else if (chartType === 'bar') {
- }
- })
- .catch((error) =>
- console.log(error)
- );
- });
- // sendButton.addEventListener("click", function () {
- // console.log(inputField.value);
- // input_text_value = inputField.value;
- // // if (myChart) {
- // // console.log('已存在')
- // // data = [];
- // // labels = [];
- // // myChart.removePlugin(Chart.pluginService.getPlugin('afterDraw'));
- // // myChart.destroy();
- // // }
- // get_data(input_text_value);
- // });
- var lastKeyPressTime = 0;
- var enterCount = 0;
- var ENTER_THRESHOLD = 500; // 設定連續按下 Enter 的時間閾值(毫秒)
- inputField.addEventListener("keyup", function (event) {
- // 判斷是否按下 Enter 鍵 (key code: 13)
- if (event.key === "Enter") {
- var currentTime = new Date().getTime();
- // 計算與上一次按鍵按下時間的差值
- var timeDiff = currentTime - lastKeyPressTime;
- lastKeyPressTime = currentTime;
- // 如果兩次 Enter 鍵按下的時間差小於閾值,則增加計數器
- if (timeDiff <= ENTER_THRESHOLD) {
- enterCount++;
- // 如果計數器為2,則觸發 API 請求並重置計數器
- if (enterCount === 2) {
- sendButtonClickHandler();
- enterCount = 0;
- }
- } else {
- // 如果時間差大於閾值,則重置計數器
- enterCount = 0;
- }
- } else {
- // 如果按下的不是 Enter 鍵,則重置計數器
- enterCount = 0;
- }
- });
- function number_format(number, decimals, dec_point, thousands_sep) {
- // * example: number_format(1234.56, 2, ',', ' ');
- // * return: '1 234,56'
- number = (number + '').replace(',', '').replace(' ', '');
- var n = !isFinite(+number) ? 0 : +number,
- prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
- sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
- dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
- s = '',
- toFixedFix = function (n, prec) {
- var k = Math.pow(10, prec);
- return '' + Math.round(n * k) / k;
- };
- // Fix for IE parseFloat(0.55).toFixed(0) = 0;
- s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
- if (s[0].length > 3) {
- s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
- }
- if ((s[1] || '').length < prec) {
- s[1] = s[1] || '';
- s[1] += new Array(prec - s[1].length + 1).join('0');
- }
- return s.join(dec);
- }
- var labels = [];
- var data = [];
- function apiLoading() {
- document.getElementById('chartDataLoading').style.display = 'block';
- }
- function apiHideLoading() {
- document.getElementById('chartDataLoading').style.display = 'none';
- }
- const DISPLAY = true;
- const BORDER = true;
- const CHART_AREA = true;
- const TICKS = true;
- var labels = [];
- var data = [];
- var dataArray;
- function generateChart(dataArray) {
- if (dataArray.length > 10) {
- lineYfontSize.style.display = 'none'
- } else {
- lineYfontSize.style.display = 'block'
- }
- // 创建一个空数组来存储 labels 和 data
- console.log(dataArray)
- // 遍历数据数组
- for (var i = 0; i < dataArray.length; i++) {
- var item = dataArray[i];
- // 遍历当前项的属性
- for (var key in item) {
- // 如果属性名不在 labels 数组中,并且属性值不是对象,则将属性名添加到 labels 数组中
- if (typeof item[key] === 'string') {
- labels.push(item[key]);
- } else {
- if (key === 'x') {
- labels.push(String(item[key]));
- }
- }
- if (key === 'y') {
- data.push(item[key]);
- }
- // 如果属性值是数值类型,则将其添加到 data 数组中
- // if (typeof item[key] === 'number') {
- // data.push(item[key]);
- // }
- }
- }
- console.log(data);
- dataFirstValue = labels[0];
- dataLastValue = labels[labels.length - 1]
- console.log('dataFirstValue', dataFirstValue);
- console.log('dataLastValue', dataLastValue);
- if (myChart) {
- console.log('已存在')
- myChart.destroy();
- }
- if (chartType === "doughnut") {
- // createChartDoughnut(chartType, data, labels);
- // createChartDoughnut2(data, labels)
- } else {
- createChart(chartType, data, labels);
- }
- // console.log(data);
- console.log(labels);
- }
- var amChartInstance;
- function extractAndGenerateChart(dataArray) {
- for (var i = 0; i < dataArray.length; i++) {
- var item = dataArray[i];
- // 遍历当前项的属性
- for (var key in item) {
- // 如果属性名不在 labels 数组中,并且属性值不是对象,则将属性名添加到 labels 数组中
- if (typeof item[key] === 'string') {
- labels.push(item[key]);
- }
- // 如果属性值是数值类型,则将其添加到 data 数组中
- if (typeof item[key] === 'number') {
- data.push(item[key]);
- }
- }
- }
- // console.log('key', key);
- generateBarChart(key, dataArray);
- }
- // 圓餅圖
- function generatePieChart(dataArray, pieFontSize, PieMaxWidth) {
- compare_box.style.display = "none";
- $('#chartdiv').show();
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- // chartDiv.style.width = "100%"
- chartWhitelement.style.background = "rgba(255,255,255,0.5)"
- // Themes begin
- am4core.useTheme(am4themes_animated);
- // Themes end
- // Create chart instance
- var chart = am4core.create("chartdiv", am4charts.PieChart);
- chart.data = dataArray;
- chart.innerRadius = am4core.percent(50);
- // Add and configure Series
- var pieSeries = chart.series.push(new am4charts.PieSeries());
- pieSeries.dataFields.value = "y";
- pieSeries.dataFields.category = "x";
- pieSeries.slices.template.stroke = am4core.color("#fff");
- pieSeries.slices.template.strokeWidth = 2;
- pieSeries.slices.template.strokeOpacity = 1;
- // Configure labels
- pieSeries.labels.template.wrap = true;
- console.log('PieMaxWidth', PieMaxWidth);
- pieSeries.labels.template.maxWidth = PieMaxWidth;
- pieSeries.labels.template.truncate = false;
- pieSeries.labels.template.fontSize = 14;
- if (typeof PieMaxWidth === 'string') {
- console.log('The variable is a string.');
- } else {
- console.log('The variable is a number.');
- }
- // Configure label text to wrap and show percentages
- // pieSeries.labels.template.adapter.add("textOutput", function (text, target) {
- // if (target.dataItem && target.dataItem.values.value.percent) {
- // return "[font-size: 14px]" + target.dataItem.category + ": " + target.dataItem.values.value.percent.toFixed(1) + "%";
- // }
- // return text;
- // });
- pieSeries.labels.template.adapter.add("textOutput", function (text, target) {
- if (target.dataItem && target.dataItem.values.value.percent) {
- return "[font-size: " + pieFontSize + "px]" + target.dataItem.category + ": " + target.dataItem.values.value.percent.toFixed(1) + "%";
- }
- return text;
- });
- pieSeries.colors.list = [
- am4core.color('rgb(171, 51, 49)'),
- am4core.color('rgb(34, 83, 149)'),
- am4core.color('rgb(79, 148, 65)'),
- am4core.color('rgb(217, 195, 105)'),
- am4core.color('rgb(142, 124, 180)'),
- am4core.color('rgb(211, 183, 144)'),
- am4core.color('rgb(83, 84, 84)'),
- am4core.color('rgb(229, 147, 152)')
- ];
- // Ensure labels have background
- // pieSeries.labels.template.background.fillOpacity = 1;
- console.log(pieSeries.labels.template)
- // Set label background color to match corresponding slice color
- pieSeries.labels.template.adapter.add("background.fill", function (fill, target) {
- return target.dataItem.slice.fill;
- });
- // This creates initial animation
- pieSeries.hiddenState.properties.opacity = 1;
- pieSeries.hiddenState.properties.endAngle = -90;
- pieSeries.hiddenState.properties.startAngle = -90;
- // console.log('調位置')
- downloadButton.style.display = "inline-block";
- chartTypeBtn.style.display = "inline-block";
- }
- // var colorEven='#288D97';
- // var colorOdd='#427D7E'
- function adjustColorBrightness(color, amount) {
- let usePound = false;
- if (color[0] == "#") {
- color = color.slice(1);
- usePound = true;
- }
- let num = parseInt(color, 16);
- let r = (num >> 16) + amount;
- let g = ((num >> 8) & 0x00FF) + amount;
- let b = (num & 0x0000FF) + amount;
- if (r > 255) r = 255;
- else if (r < 0) r = 0;
- if (g > 255) g = 255;
- else if (g < 0) g = 0;
- if (b > 255) b = 255;
- else if (b < 0) b = 0;
- return (usePound ? "#" : "") + (r << 16 | g << 8 | b).toString(16).padStart(6, '0');
- }
- // 柱狀圖
- function generateBarChart(key, dataArray) {
- console.log('generateBarChart', dataArray)
- chartTypeBtn.style.display = "inline-block";
- compare_box.style.display = "none";
- $('#chartdiv').show();
- // var colorEven = document.getElementById('colorEven').value;
- // var colorOdd = document.getElementById('colorOdd').value;
- var baseColor = document.getElementById('borderColorInputBar').value;
- var categoryAxisFzValue = document.getElementById('XfontSizeBar').value;
- var valueAxisFzValue = document.getElementById('YfontSizeBar').value;
- var colorEven = am4core.color(baseColor);
- var colorOdd = adjustColorBrightness(baseColor, -20);
- chartDiv.style.fontFamily = "Arial, sans-serif";
- chartDiv.style.fontWeight = "900";
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- chartWhitelement.style.background = "rgba(255,255,255,0.5)"
- console.log('labelDependent', labelDependent);
- // Themes begin
- am4core.useTheme(am4themes_animated);
- // Themes end
- // Create chart instance
- var chart = am4core.create("chartdiv", am4charts.XYChart3D);
- chart.data = dataArray;
- chart.angle = 30; // 圖表角度
- chart.depth = 25; // 圖表深度
- let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
- categoryAxis.dataFields.category = "x";
- categoryAxis.renderer.labels.template.rotation = 0;
- categoryAxis.renderer.labels.template.hideOversized = false;
- categoryAxis.renderer.minGridDistance = 20; // 間距
- // categoryAxis.renderer.labels.template.horizontalCenter = "left";
- // categoryAxis.renderer.labels.template.verticalCenter = "middle";
- // categoryAxis.tooltip.label.rotation = 90;
- // categoryAxis.tooltip.label.horizontalCenter = "right";
- // categoryAxis.tooltip.label.verticalCenter = "middle";
- // label 換行
- let label = categoryAxis.renderer.labels.template;
- label.wrap = true;
- if (dataArray.length > 5) {
- console.log('資料長度5')
- label.maxWidth = 60;
- } else if (dataArray.length < 7) {
- label.maxWidth = 70;
- } else {
- label.maxWidth = 100;
- console.log('資料長度<5')
- }
- // x軸字體大小
- categoryAxis.renderer.labels.template.fontSize = categoryAxisFzValue;
- categoryAxis.renderer.cellEndLocation = 0.5; // 減少值可以增加間距
- let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
- valueAxis.title.text = labelDependent; // Y軸
- valueAxis.title.fontWeight = "bold";
- // y軸字體大小
- valueAxis.renderer.labels.template.fontSize = valueAxisFzValue;
- // Create series
- var series = chart.series.push(new am4charts.ColumnSeries3D());
- series.dataFields.valueY = 'y';
- series.dataFields.categoryX = "x";
- series.name = "y";
- series.tooltipText = "{categoryX}: [bold]{valueY}[/]";
- series.columns.template.fillOpacity = .8;
- let columnTemplate = series.columns.template;
- columnTemplate.strokeWidth = 2;
- columnTemplate.strokeOpacity = 1;
- columnTemplate.stroke = am4core.color("#FFFFFF");
- // columnTemplate.adapter.add("fill", function (fill, target) {
- // return chart.colors.getIndex(target.dataItem.index);
- // })
- // 設置基數和偶數顏色
- columnTemplate.adapter.add("fill", function (fill, target) {
- return target.dataItem.index % 2 === 0 ? am4core.color(colorEven) : am4core.color(colorOdd);
- });
- columnTemplate.adapter.add("stroke", function (stroke, target) {
- return chart.colors.getIndex(target.dataItem.index);
- })
- // // 加上白色線條
- // series.columns.template.events.on("validated", function (event) {
- // let column = event.target;
- // let whiteLine = column.createChild(am4core.Line);
- // whiteLine.stroke = am4core.color("#ffffff");
- // whiteLine.strokeWidth = 1.3; // 線條寬度
- // whiteLine.strokeOpacity = 0.5;// 透明度
- // whiteLine.x1 = column.pixelWidth / 1;
- // whiteLine.y1 = 0;
- // whiteLine.x2 = column.pixelWidth / 1;
- // whiteLine.y2 = column.pixelHeight;
- // });
- chart.cursor = new am4charts.XYCursor();
- chart.cursor.lineX.strokeOpacity = 0;
- chart.cursor.lineY.strokeOpacity = 0;
- categoryAxis.renderer.grid.template.strokeOpacity = 0; // 隱藏 X 軸
- // valueAxis.renderer.grid.template.strokeOpacity = 0; // 隱藏 Y 軸
- // valueAxis.renderer.line.strokeOpacity = 0;
- // LineSeries 底部線條
- var lineSeries = chart.series.push(new am4charts.LineSeries());
- lineSeries.dataFields.valueY = "bottom";
- lineSeries.dataFields.categoryX = "0";
- lineSeries.stroke = am4core.color("#FFFFFF")
- lineSeries.strokeWidth = 2;
- lineSeries.strokeOpacity = 1
- var bottomData = [];
- for (var i = 0; i < dataArray.length; i++) {
- bottomData.push({ "x": dataArray[i].x, "bottom": 0 });
- }
- lineSeries.data = bottomData;
- downloadButton.style.display = "inline-block";
- chartTypeBtn.style.display = "inline-block";
- chartTypeBtn.style.opacity = "1";
- // 底部線條
- // let rectElements = document.querySelectorAll("rect");
- // // let widthValue = 0;
- // let fifthRectElement = rectElements[3];
- // // fifthRectElement.style.position = "relative";
- // setTimeout(() => {
- // let width = fifthRectElement.getAttribute("width");
- // // widthValue = parseFloat(width);
- // console.log('rectElements', rectElements);
- // console.log('fifthRectElement', fifthRectElement);
- // console.log('width', width);
- // let line = document.createElement("div");
- // line.style.position = "absolute";
- // line.style.bottom = "162px";
- // line.style.right = "40px";
- // line.style.width = `${parseFloat(width)}px`;
- // line.style.height = "2px";
- // line.style.backgroundColor = "#000000";
- // document.getElementById("chartdiv").appendChild(line);
- // }, 10)
- // end am4core.ready()
- }
- function handleClick(element) {
- const Clickvalue = element.dataset.value;
- console.log(Clickvalue); // 123
- get_data(Clickvalue);
- }
- // 圖表類型
- var chartTypeBtn = document.getElementById("chartType");
- chartTypeBtn.addEventListener('change', function () {
- const selectedValue = this.value;
- console.log('選擇的圖表類型:', selectedValue);
- obj.text = selectedValue;
- // 在此处添加处理选定图表类型的代码
- if (selectedValue === 'line') {
- // 处理折线图
- console.log('處理折線圖', dataArray);
- $('#chartdiv').hide();
- chartType = 'line';
- if (myChart) {
- data = [];
- labels = [];
- myChart.destroy();
- }
- generateChart(dataArray);
- } else if (selectedValue === 'bar') {
- chartType = 'bar';
- $('#textToChart').hide();
- // 处理柱状图
- console.log('處理柱狀圖', dataArray);
- extractAndGenerateChart(dataArray);
- } else if (selectedValue === 'doughnut') {
- chartType = 'doughnut';
- console.log('處理圓餅圖', dataArray);
- unit_value.textContent = "";
- $('#textToChart').hide();
- generatePieChart(dataArray, pieFontSize, PieMaxWidth)
- // 处理圆饼图
- }
- });
- var downloadButton = document.getElementById("downloadButton");
- // downloadButton.style.display = 'none';
- // 添加點擊事件監聽器
- downloadButton.addEventListener('click', function () {
- setTimeout(function () {
- // html2canvas(document.getElementById('data_chart_box')).then(function (canvas) {
- // // // 獲取圖片資料 URL
- // // var imageData = canvasPng.toDataURL("image/png");
- // // // 創建下載連結
- // // var link = document.createElement('a');
- // // link.download = 'chart.png'; // 下載的檔案名稱
- // // link.href = imageData; // 圖片資料 URL
- // // // 模擬點擊下載連結
- // // link.click();
- // // 創建一個<a>元素
- // const link = document.createElement('a');
- // // 將 Canvas 轉換為 Data URL
- // link.href = canvas.toDataURL('image/png');
- // // 設置下載文件名
- // link.download = 'chart.png';
- // // 模擬點擊下載
- // link.click();
- // });
- html2canvas(document.getElementById('data_chart_box'), {
- backgroundColor: null, // 確保背景透明
- scale: 2,
- useCORS: true,
- scrollX: 0,
- scrollY: 0
- }).then(function (canvas) {
- // 創建一個 <a> 元素
- const link = document.createElement('a');
- // 將 Canvas 轉換為 Data URL
- link.href = canvas.toDataURL('image/png');
- // 設置下載文件名
- link.download = 'chart.png';
- // 模擬點擊下載
- link.click();
- });
- }, 1000); // 等待 1 秒钟
- });
- var pieChartColors = [
- 'rgb(171, 51, 49)',
- 'rgb(34, 83, 149)',
- 'rgb(79, 148, 65)',
- 'rgb(217, 195, 105)',
- 'rgb(142, 124, 180)',
- 'rgb(211, 183, 144)',
- 'rgb(83, 84, 84)',
- 'rgb(229, 147, 152)'
- ];
- function createChartDoughnut2(data, labels) {
- const chartData = {
- type: 'outlabeledPie',
- data: {
- labels: labels,
- datasets: [
- {
- data: data,
- borderWidth: borderWidthValue,
- fill: false,
- backgroundColor: pieChartColors,
- },
- ],
- },
- options: {
- plugins: {
- legend: false,
- outlabels: {
- text: '%l %p',
- color: 'white',
- stretch: 35,
- font: {
- resizable: true,
- family: 'Arial',
- size: 48,
- weight: 'bold',
- style: 'italic',
- },
- },
- },
- },
- };
- const devicePixelRatio = 4;
- var chartUrl = `https://quickchart.io/chart?c=${encodeURIComponent(JSON.stringify(chartData))}&devicePixelRatio=${devicePixelRatio}&f=png&fontsize=20`;
- // 获取目标 div 元素
- const targetDiv = document.getElementById('data_chartJs_box');
- // 创建一个 img 元素来显示图表
- const chartImg = document.createElement('img');
- chartImg.src = chartUrl;
- chartImg.classList.add('outlabeled_img');
- // 清空目标 div 元素
- targetDiv.innerHTML = '';
- // 将图表 img 元素插入到目标 div 中
- targetDiv.appendChild(chartImg);
- console.log(chart_bgimg_url);
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- chartWhitelement.style.background = "rgba(255,255,255,0.5)"
- }
- // 版型二源餅圖
- // // 创建 Chart.js 图表
- function createChartDoughnut(chartType, data, labels) {
- $('#textToChart').show();
- canvasPng.style.maxWidth = '450px';
- canvasPng.style.maxHeight = '450px';
- var windowWidth = window.innerWidth || document.documentElement.clientWidth;
- var canvasWidth = Math.min(500, windowWidth); // 限制最大宽度为 500px
- canvasPng.style.width = canvasWidth + 'px'; // 设置 canvas 宽度
- canvasPng.style.marginLeft = 'auto'; // 左外边距自动计算
- canvasPng.style.marginRight = 'auto'; // 右外边距自动计算
- var ctx = document.getElementById("textToChart").getContext('2d');
- myChart = new Chart(ctx, {
- type: chartType,
- data: {
- labels: labels,
- datasets: [{
- strokeColor: "rgba(220,220,220,1)",
- StrokeWidth: 5,
- data: data,
- borderWidth: borderWidthValue,
- pointRadius: 0,
- pointBorderColor: '#fff',
- pointBorderWidth: 3,
- pointHoverRadius: 0,
- fill: false,
- backgroundColor: pieChartColors,
- // barPercentage: 0.5,
- // categoryPercentage: 0.5
- }]
- },
- options: {
- // responsive: true, // 啟用響應式
- maintainAspectRatio: true,
- responsive: true,
- aspectRatio: 1,
- plugins: {
- legend: {
- display: false,
- },
- datalabels: {
- color: '#fff', // 数据标签的颜色
- anchor: 'end', // 数据标签的位置
- align: 'start', // 数据标签的对齐方式
- font: {
- size: 14, // 数据标签的字体大小
- },
- }
- },
- scales: {
- x: {
- display: false, // 显示 x 轴
- grid: {
- display: false,
- lineWidth: 5,
- color: '#584B3D'
- },
- ticks: {
- textStrokeColor: '#fff',
- textStrokeWidth: 5,
- font: {
- display: false,
- family: 'Arial', // 设置字体
- size: XfontSizeValue, // 设置字体大小
- weight: 'bold', // 设置字体粗细
- fontColor: 'rgba(255,255,255,0.8)', // X-axis font color
- shadowColor: 'rgba(0, 0, 0, 0.5)', // Shadow color
- shadowBlur: 10, // Shadow blur level
- shadowOffsetX: 5, // Horizontal shadow offset
- shadowOffsetY: 5
- },
- },
- },
- y: {
- display: false,
- beginAtZero: false,
- grid: {
- display: false,
- lineWidth: 5,
- color: '#584B3D'
- },
- ticks: {
- textStrokeColor: '#fff',
- textStrokeWidth: 5,
- font: {
- family: 'Arial', // 設置字體
- size: XfontSizeValue, // 設置字體大小
- weight: 'bold', // 設置字體粗細
- color: 'rgba(255,255,255,0.8)', // Y-axis 字體顏色
- shadowColor: 'rgba(0, 0, 0, 0.5)', // 陰影顏色
- shadowBlur: 10, // 陰影模糊級別
- shadowOffsetX: 5, // 水平陰影偏移
- shadowOffsetY: 5 // 垂直陰影偏移
- },
- }
- }
- },
- layout: {
- padding: {
- left: 50, // 调整图表左边距
- right: 50, // 调整图表右边距
- top: 10,
- bottom: 10,
- }
- },
- },
- });
- console.log(chart_bgimg_url);
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- chartWhitelement.style.background = "rgba(255,255,255,0.5)"
- }
- var chartJsBox = document.getElementById('data_chartJs_box');
- function createTable(dataArray, unit, Label_dependent_variable, Label_independent_variable,) {
- $('#chartdiv').hide();
- $('#textToChart').hide();
- chartTitle.style.padding = "15px 0px 30px 30px";
- bgImgelement.style.paddingTop = "10px";
- var table = document.createElement('table');
- table.classList.add('table', 'table_template', 'dynamic-table'); // 添加 Bootstrap 的 table 样式,如果你在使用 Bootstrap
- // 创建表头
- var thead = table.createTHead();
- var headerRow = thead.insertRow();
- // Object.keys(dataArray[0]).forEach(function (key) {
- // var header = headerRow.insertCell();
- // header.textContent = key;
- // });
- var yearHeader = headerRow.insertCell();
- yearHeader.textContent = Label_independent_variable;
- var avgPriceHeader = headerRow.insertCell();
- avgPriceHeader.textContent = Label_dependent_variable;
- // 创建表格主体
- var tbody = table.createTBody();
- dataArray.forEach(function (item) {
- console.log(item)
- var row = tbody.insertRow();
- // var cell1 = row.insertCell();
- // cell1.textContent = item.year;
- // cell1.classList.add('year-class'); // 为年份单元格添加类
- // var cell2 = row.insertCell();
- Object.keys(item).forEach(function (key, index) {
- var cell = row.insertCell();
- if (index === 0) { // If it's the first cell, add 'year-class'
- cell.textContent = item[key];
- cell.classList.add('year-class');
- } else { // Otherwise, add 'average-class'
- if (key === 'Average_Close') {
- // cell.textContent = item[key].toFixed(2) + unit;
- cell.innerHTML = item[key].toFixed(2) + '<span class="unit_table">' + unit + '</span>';
- } else {
- // cell.textContent = item[key];
- // cell.textContent = item[key].toFixed(2) + unit;
- cell.innerHTML = item[key].toFixed(2) + '<span class="unit_table">' + unit + '</span>';
- }
- cell.classList.add('average-class');
- }
- });
- // console.log(item.avg_price.toFixed(2))
- // cell.textContent = item[key].toFixed(2) + unit;
- // cell2.classList.add('average-class'); // 为年份单元格添加类
- });
- // 获取包含表格的 div 元素
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- img_box.style.bottom = '60px';
- img_box.style.right = '5px';
- document.getElementById('img_box_url').style.width = "180px";
- // chartWhitelement.style.background = "rgba(255,255,255,0.5)"
- // 将表格添加到指定的 div 元素中
- chartJsBox.appendChild(table);
- unit_table_value = document.querySelectorAll('.unit_table');
- downloadButton.style.display = "inline-block";
- chartTypeBtn.style.display = "inline-block";
- chartTypeBtn.style.opacity = "0";
- }
- function getDatalabelsConfig(dataLength) {
- if (dataLength < 10) {
- return {
- formatter: function (value, context) {
- return value + ' unit'; // Add unit if data length is less than 10
- },
- textStrokeColor: '#fff',
- textStrokeWidth: 5,
- color: '#584B3D',
- font: {
- size: YfontSizeValue,
- },
- anchor: 'end',
- align: 'end',
- };
- } else {
- return null; // No datalabels if data length is 10 or more
- }
- }
- var pointRadiusValue = 0;
- var pointHoverRadiusValue = 0;
- // 版型一折線圖
- // // 创建 Chart.js 图表
- function createChart(chartType, data, labels) {
- $('#textToChart').show();
- canvasPng.style.maxWidth = '';
- canvasPng.style.maxHeight = '';
- console.log('繪製圖表', data, labels);
- if (data.length < 15) {
- pointRadiusValue = 5;
- pointHoverRadiusValue = 8;
- togglePoints.checked = true;
- }
- const datalabelsConfig = data.length <= 15 ? {
- formatter: function (value, context) {
- // if (context.dataIndex === 0) {
- // return '';
- // }
- // if (typeof value === 'number' && value.toString().includes('.')) {
- // value = value.toFixed(4);
- // }
- // return value;
- return value;
- },
- textStrokeColor: '#fff',
- textStrokeWidth: 5,
- color: '#584B3D',
- font: {
- size: YfontSizeValue,
- },
- anchor: 'end',
- align: 'top'
- } : null;
- ctx = document.getElementById("textToChart");
- myChart = new Chart(ctx, {
- type: chartType,
- data: {
- labels: labels,
- datasets: [{
- label: stocknumber,
- strokeColor: "rgba(220,220,220,1)",
- StrokeWidth: 5,
- data: data,
- borderWidth: borderWidthValue,
- pointRadius: pointRadiusValue,
- pointBorderColor: '#fff',
- pointBorderWidth: 3,
- pointHoverRadius: pointHoverRadiusValue,
- fill: false,
- backgroundColor: chartColor,
- borderColor: chartColor,
- // barPercentage: 0.5,
- // categoryPercentage: 0.5
- }]
- },
- options: {
- // responsive: true, // 啟用響應式
- maintainAspectRatio: false,
- plugins: {
- chart3d: {
- enabled: true, // 啟用 3D 插件
- alpha: 25, // 圖表繞 x 軸的旋轉角度
- beta: 25, // 圖表繞 y 軸的旋轉角度
- depth: 15 // 圖表的深度
- },
- datalabels: datalabelsConfig,
- backgroundColor: 'transparent',
- // datalabels: {
- // formatter: function (value, context) {
- // // 添加单位
- // return value;
- // },
- // textStrokeColor: '#fff',
- // textStrokeWidth: 5,
- // color: '#584B3D',
- // font: {
- // size: YfontSizeValue,
- // },
- // anchor: 'end',
- // align: 'end',
- // },
- // datalabels: null,
- // datalabels: data.length <= 10 ? {
- // formatter: function (value, context) {
- // // 添加单位
- // return value + ' ' + unit;
- // },
- // textStrokeColor: '#fff',
- // textStrokeWidth: 5,
- // color: '#584B3D',
- // font: {
- // size: YfontSizeValue,
- // },
- // anchor: 'end',
- // align: 'end',
- // } : null,
- customLabels: null,
- legend: {
- display: false,
- },
- },
- scales: {
- x: {
- display: displayXaxes, // 显示 x 轴
- grid: {
- display: false,
- lineWidth: 5,
- color: '#584B3D'
- },
- ticks: {
- textStrokeColor: '#fff',
- textStrokeWidth: 5,
- font: {
- display: false,
- family: 'Arial', // 设置字体
- size: XfontSizeValue, // 设置字体大小
- weight: 'bold', // 设置字体粗细
- fontColor: 'rgba(255,255,255,0.8)', // X-axis font color
- shadowColor: 'rgba(0, 0, 0, 0.5)', // Shadow color
- shadowBlur: 10, // Shadow blur level
- shadowOffsetX: 5, // Horizontal shadow offset
- shadowOffsetY: 5
- },
- },
- },
- y: {
- display: true,
- beginAtZero: false,
- grid: {
- grid: {
- display: true, // 显示 y 轴网格线
- lineWidth: 1, // 网格线宽度
- color: '#C0C5CC' // 网格线颜色
- },
- },
- ticks: {
- textStrokeColor: '#fff',
- textStrokeWidth: 5,
- font: {
- family: 'Arial', // 設置字體
- size: XfontSizeValue, // 設置字體大小
- weight: 'bold', // 設置字體粗細
- color: 'rgba(255,255,255,0.8)', // Y-axis 字體顏色
- shadowColor: 'rgba(0, 0, 0, 0.5)', // 陰影顏色
- shadowBlur: 10, // 陰影模糊級別
- shadowOffsetX: 5, // 水平陰影偏移
- shadowOffsetY: 5 // 垂直陰影偏移
- },
- }
- }
- },
- layout: {
- padding: {
- left: 50, // 调整图表左边距
- right: 50, // 调整图表右边距
- top: 40,
- bottom: 10,
- }
- },
- },
- });
- console.log('第一次', myChart.options.legend)
- console.log(chart_bgimg_url);
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- chartWhitelement.style.background = "rgba(255,255,255,0.5)";
- downloadButton.style.display = "inline-block";
- chartTypeBtn.style.display = "inline-block";
- chartTypeBtn.style.opacity = "1";
- compare_box.style.display = "flex";
- }
- // createChart(chartType);
- // createChart(chartType)
- const ChartOptions = document.querySelectorAll('input[name="ChartOptions"]');
- ChartOptions.forEach(button => {
- button.addEventListener('click', function () {
- // console.log(this.value);
- chartType = this.value;
- console.log(chartType);
- if (myChart !== null) {
- myChart.destroy();
- data = [];
- labels = [];
- }
- if (chartType === 'line') {
- $('#chartdiv').hide();
- generateChart(dataArray);
- } else {
- $('#textToChart').hide();
- extractAndGenerateChart(dataArray);
- }
- // createChart(chartType, data, labels);
- });
- });
- // 表格字體
- var fontSizeInput = document.getElementById('table_fontSize');
- // 添加事件监听器
- fontSizeInput.addEventListener('input', function () {
- // 获取输入框的值
- var fontSize = fontSizeInput.value;
- // 获取动态创建的表格元素
- var dynamicTable = document.querySelector('.dynamic-table');
- // 如果表格存在,则调整其字体大小
- if (dynamicTable) {
- dynamicTable.style.fontSize = fontSize + 'px';
- }
- });
- // 圓餅圖字體
- var pieFontSizeInput = document.getElementById('pie_fontSize');
- // 添加事件监听器
- pieFontSizeInput.addEventListener('input', function () {
- // 获取输入框的值
- pieFontSize = pieFontSizeInput.value;
- console.log('圓餅圖字體', pieFontSize);
- if (pieFontSize) {
- generatePieChart(dataArray, pieFontSize, PieMaxWidth);
- } else {
- alert("Please enter a valid font size.");
- }
- // 如果表格存在,则调整其字体大小
- // if (dynamicTable) {
- // dynamicTable.style.fontSize = fontSize + 'px';
- // }
- });
- document.getElementById('PieMaxWidthRange').addEventListener('input', function () {
- PieMaxWidth = Number(document.getElementById('PieMaxWidthRange').value);
- console.log('圓餅圖', PieMaxWidth)
- generatePieChart(dataArray, pieFontSize, PieMaxWidth);
- });
- // 是否顯示point
- var togglePoints = document.getElementById('togglePoints');
- togglePoints.addEventListener('change', function () {
- console.log('checked', "change")
- if (this.checked) {
- myChart.data.datasets[0].pointRadius = 5;
- myChart.data.datasets[0].pointHoverRadius = 8;
- } else {
- myChart.data.datasets[0].pointRadius = 0;
- myChart.data.datasets[0].pointHoverRadius = 0;
- }
- myChart.update();
- });
- // 是否顯示單位
- var toggleUnit = document.getElementById('toggleUnit');
- toggleUnit.addEventListener('change', function () {
- if (this.checked) {
- unit_value_box.style.display = 'block';
- } else {
- unit_value_box.style.display = 'none';
- }
- });
- // 單位
- // =========================
- var unitInput = document.getElementById('unit_data');
- var unit_value = document.querySelector('.unit');
- var unit_value_box = document.querySelector('#unit_box');
- // console.log('單位', unitInput)
- unitInput.addEventListener('input', function () {
- // unitInput.value = unit_value.textContent;
- if (chartType === 'table') {
- // unit_table_value.textContent = this.value;
- unit_table_value.forEach(span => {
- span.textContent = this.value;
- });
- } else {
- unit_value.textContent = this.value;
- }
- });
- // ==========================
- // y軸字體大小-折線圖
- // ==========================
- var YfontSize = document.getElementById('YfontSize');
- YfontSize.addEventListener('input', function () {
- YfontSizeValue = YfontSize.value;
- if (myChart) {
- myChart.destroy();
- }
- createChart(chartType, data, labels);
- });
- // ==========================
- // x軸字體大小-折線圖
- // ==========================
- var XfontSize = document.getElementById('XfontSize');
- XfontSize.addEventListener('input', function () {
- XfontSizeValue = XfontSize.value;
- console.log(XfontSizeValue)
- if (myChart) {
- myChart.destroy();
- }
- createChart(chartType, data, labels);
- });
- // ==========================
- // x軸字體大小-柱狀圖
- // ==========================
- var XfontSizeBar = document.getElementById('XfontSizeBar');
- XfontSizeBar.addEventListener('input', function () {
- extractAndGenerateChart(dataArray);
- });
- // ==========================
- // y軸字體大小-柱狀圖
- // ==========================
- var YfontSizeBar = document.getElementById('YfontSizeBar');
- YfontSizeBar.addEventListener('input', function () {
- extractAndGenerateChart(dataArray);
- });
- // ==========================
- // 線條顏色-柱狀圖
- // =========================
- const colorInputBar = document.getElementById('borderColorInputBar');
- // 添加事件监听器,当颜色选择发生变化时触发
- colorInputBar.addEventListener('input', function () {
- extractAndGenerateChart(dataArray);
- });
- // =========================
- // 線條粗細
- // =========================
- var rangeInput = document.getElementById('borderWidthRange');
- var rangeInputSpan = document.getElementById('borderWidthRangeValue');
- var borderWidthValue;
- rangeInputSpan.textContent = rangeInput.value;
- // 添加事件监听器,当滑动条的值发生改变时更新显示的值
- rangeInput.addEventListener('input', function () {
- // 获取滑动条的当前值
- borderWidthValue = rangeInput.value;
- console.log(borderWidthValue);
- rangeInputSpan.textContent = borderWidthValue;
- if (myChart) {
- myChart.destroy();
- }
- createChart(chartType, data, labels);
- // myChart.data.datasets[0].borderWidth = borderWidthValue;
- });
- // =========================
- // 線條顏色-折線圖
- // =========================
- const colorInput = document.getElementById('borderColorInput');
- // 添加事件监听器,当颜色选择发生变化时触发
- colorInput.addEventListener('input', function () {
- // 获取当前选择的颜色值
- chartColor = colorInput.value;
- if (myChart) {
- myChart.destroy();
- }
- console.log(chartColor);
- createChart(chartType, data, labels);
- });
- // =========================
- // 模板樣式
- // =========================
- // JavaScript
- document.addEventListener("DOMContentLoaded", function () {
- // 獲取所有帶有 templateImg class 的圖像元素
- var templateImgs = document.querySelectorAll('.templateImg');
- // 為每個圖像元素添加點擊事件監聽器
- templateImgs.forEach(function (img) {
- img.addEventListener('click', function () {
- // 在點擊時印出圖像元素的 value 屬性值
- console.log(this.getAttribute('value'));
- });
- });
- });
- // =========================
- var bgImgelement = document.getElementById('data_chart_box');
- var chartWhitelement = document.getElementById('data_chartJs_box');
- // 背景樣式
- // =========================
- // JavaScript
- document.addEventListener("DOMContentLoaded", function () {
- // 獲取所有帶有 templateImg class 的圖像元素
- var bgImgs = document.querySelectorAll('.bgImg');
- // 為每個圖像元素添加點擊事件監聽器
- bgImgs.forEach(function (img) {
- img.addEventListener('click', function () {
- // 在點擊時印出圖像元素的 value 屬性值
- console.log(this.getAttribute('value'));
- var bgImgUrl = this.getAttribute('value');
- chart_bgimg_url = 'url(' + bgImgUrl + ')';
- if (bgImgUrl === './img/bg07.webp') {
- chartTitle.style.color = "#ffff";
- chartTitle.style.padding = '0px'
- }
- else {
- chartTitle.style.color = "#1c2d6d";
- }
- if (bgImgUrl === "") {
- chartWhitelement.style.background = "";
- console.log('無背景');
- bgImgelement.style.backgroundImage = "";
- chartWhitelement.style.background = 'rgba(255, 255, 255, 0) !important;'
- } else {
- bgImgelement.style.backgroundImage = chart_bgimg_url;
- chartWhitelement.style.background = "rgba(255, 255, 255, 0.5)"
- }
- // bgImgelement.style.backgroundSize = 'cover'; // 調整背景圖片大小
- // bgImgelement.style.backgroundPosition = 'center'; // 調整背景圖片位置
- // bgImgelement.style.backgroundRepeat = 'no-repeat'; // 調整背景圖片重複
- });
- });
- });
- // =========================
- const radioButtons = document.querySelectorAll('input[name="inlineRadioOptions"]');
- var chartTitle = document.querySelector('.chart_title');
- radioButtons.forEach(button => {
- button.addEventListener('click', function () {
- if (this.value === '0') {
- chartTitle.style.textAlign = 'left';
- } else if (this.value === '1') {
- chartTitle.style.textAlign = 'center';
- } else if (this.value === '2') {
- chartTitle.style.textAlign = 'right';
- }
- });
- });
- // 圖表標題
- var keywordInput = document.getElementById('title_data');
- // 初始时将输入框的值设置为标题的文本内容
- keywordInput.value = chartTitle.textContent;
- // 监听输入框的输入事件,实现内容同步更新
- keywordInput.addEventListener('input', function () {
- chartTitle.textContent = this.value;
- });
- // 標題字體大小
- // ==========================
- var TitlefontSize = document.getElementById('TitlefontSize');
- TitlefontSize.addEventListener('input', function () {
- var TitlefontSizeValue = TitlefontSize.value;
- chartTitle.style.fontSize = TitlefontSizeValue + 'px';;
- });
- // 標題字體顏色
- var stocknumber;
- // ==========================
- let labelDependent = ""; // 圖表左側 Label
- function get_data(input_text_value) {
- clickExample.style.display = 'none'
- // 重新创建取消令牌和源
- cancelToken = axios.CancelToken;
- source = cancelToken.source();
- // inputField.value = "";
- apiLoading();
- var existingTable = chartJsBox.querySelector('table');
- downloadButton.style.display = "none";
- if (chartDiv.childElementCount != 0) {
- data = [];
- labels = [];
- console.log('已存在')
- chartTitle.textContent = "";
- unit_value.textContent = "";
- bgImgelement.style.backgroundImage = "";
- chartWhitelement.style.background = "";
- document.getElementById('img_box_url').src = "";
- }
- if (existingTable) {
- unit_value.textContent = "";
- chartJsBox.removeChild(existingTable);
- chartTitle.textContent = "";
- bgImgelement.style.backgroundImage = "";
- chartWhitelement.style.background = "";
- document.getElementById('img_box_url').src = "";
- }
- if (myChart) {
- unit_value_box.style.display = 'none';
- unit_value.textContent = "";
- data = [];
- labels = [];
- chartTitle.textContent = "";
- bgImgelement.style.backgroundImage = "";
- chartWhitelement.style.background = "";
- myChart.destroy();
- document.getElementById('img_box_url').src = "";
- }
- const apiUrl = `https://cmm.ai:8080/answer_with_token?token=${accessToken}&question=${input_text_value}`;
- axios.get(apiUrl, {
- cancelToken: source.token // 将取消令牌传递给 Axios 请求
- })
- .then(response => {
- apiHideLoading();
- console.log(response);
- if (response.data.data == "未達相似度搜尋標準") {
- alert('無法生成圖表')
- return
- } else {
- unit_value_box.style.display = "block";
- var chart_info = response.data.chart_info.Title;
- chartType = response.data.chart_info.Chart_type;
- var finance_img_url = response.data.imageUrl_info.imageUrl;
- if (response.data.imageUrl_info != null) {
- console.log(finance_img_url);
- document.getElementById('img_box_url').src = finance_img_url;
- }
- keywordInput.value = chart_info;
- chartTitle.textContent = chart_info;
- unit = response.data.chart_info.Unit_of_dependent_variable;
- var Label_dependent_variable = response.data.chart_info.Label_dependent_variable;
- var Label_independent_variable = response.data.chart_info.Label_independent_variable;
- stocknumber = response.data.chart_info.args;
- chartTypeBtn.value = chartType;
- unit_data.value = response.data.chart_info.Unit_of_dependent_variable;
- if (response.data.chart_info.Unit_of_dependent_variable == "") {
- unit_value.textContent = ""
- } else {
- unit_value.textContent = response.data.chart_info.Unit_of_dependent_variable;
- }
- obj.text = chartType;
- dataArray = response.data.data;
- if (chartType === "table") {
- unit_value.textContent = "";
- unit_value_box.style.display = "none"
- createTable(dataArray, unit, Label_dependent_variable, Label_independent_variable);
- // document.getElementById('img_box_url').src = "";
- } else if (chartType === "bar") {
- unit_value_box.style.display = 'block';
- console.log('圖表類型bar')
- $('#textToChart').hide();
- extractAndGenerateChart(dataArray);
- } else if (chartType === "doughnut") {
- unit_value.textContent = "";
- $('#textToChart').hide();
- generatePieChart(dataArray, pieFontSize, PieMaxWidth)
- } else {
- unit_value_box.style.display = "block"
- $('#chartdiv').hide();
- generateChart(dataArray);
- }
- // }
- // 在這裡處理成功獲取 JSON 的情況
- // console.log(response);
- // switch (chartType) {
- // case "line":
- // document.getElementById("inlineRadio4").checked = true; // 折线图
- // break;
- // case "bar":
- // document.getElementById("inlineRadio5").checked = true; // 柱状图
- // labelDependent = response.data.chart_info.Label_dependent_variable;
- // break;
- // case "pie":
- // document.getElementById("inlineRadio6").checked = true; // 圆饼图
- // break;
- // case "table":
- // document.getElementById("inlineRadio7").checked = true; // 表格
- // break;
- // default:
- // // 默认情况
- }
- // generateChart(dataArray);
- })
- .catch(error => {
- // 错误处理逻辑,包括取消请求的情况
- apiHideLoading();
- if (axios.isCancel(error)) {
- console.log('请求被取消:', error.message);
- } else {
- console.error('发生错误:', error);
- }
- });
- }
- // 取消请求的函数
- function cancelRequest() {
- if (source) {
- source.cancel('取消了 API 请求');
- apiHideLoading(); // 隐藏加载状态
- }
- }
- document.getElementById('cancel_send_data').addEventListener('click', cancelRequest);
- $(document).ready(function () {
- $('#dataTable').DataTable();
- });
- function resizeChart() {
- console.log(myChart)
- if (myChart != null) {
- myChart.resize();
- }
- }
- document.addEventListener('visibilitychange', function () {
- console.log('Visibility changed:', document.visibilityState);
- if (document.visibilityState === 'visible') {
- resizeChart();
- }
- });
- // 监听窗口resize事件
- window.addEventListener('resize', resizeChart);
- var setting_button = document.getElementById('chart_seeting_button');
- var closeButton = document.getElementById('style_setting_close');
- var closeButton_table = document.getElementById('style_setting_close_table');
- // var borderSetting = document.getElementById('borderSetting');
- var lineChartSetting = document.getElementById('lineChartSetting');
- var barChartSetting = document.getElementById('barChartSetting');
- var table_setting = document.getElementById('table_setting');
- var chart_category = document.getElementById('chart_category');
- var pie_setting = document.getElementById('pie_setting');
- // 获取 chartDiv 元素
- setting_button.addEventListener('click', () => {
- console.log('setting_button')
- if (myChart === null && !chartJsBox.querySelector('table') && chartDiv.childElementCount === 0) {
- alert('請先生成圖表');
- return
- }
- if (chartType === 'table') {
- console.log('表格');
- sidebar.classList.toggle('show');
- sidebar.classList.toggle('hidden');
- barChartSetting.style.display = 'none';
- lineChartSetting.style.display = 'none';
- table_setting.style.display = 'block';
- chart_category.style.display = 'none';
- pie_setting.style.display = 'none';
- } else if (chartType === "bar") {
- console.log('柱狀圖')
- sidebar.classList.toggle('show');
- sidebar.classList.toggle('hidden');
- barChartSetting.style.display = 'block';
- lineChartSetting.style.display = 'none';
- table_setting.style.display = 'none';
- pie_setting.style.display = 'none';
- } else if (chartType === "doughnut") {
- console.log('快捷功能圓餅圖')
- sidebar.classList.toggle('show');
- sidebar.classList.toggle('hidden');
- barChartSetting.style.display = 'none';
- lineChartSetting.style.display = 'none';
- pie_setting.style.display = 'block';
- table_setting.style.display = 'none';
- chart_category.style.display = 'none';
- document.getElementById('unit_input').style.display = 'none';
- } else {
- sidebar.classList.toggle('show');
- sidebar.classList.toggle('hidden');
- table_setting.style.display = 'none';
- barChartSetting.style.display = 'none';
- lineChartSetting.style.display = 'block';
- pie_setting.style.display = 'none';
- }
- });
- closeButton.addEventListener('click', () => {
- sidebar.classList.remove('show');
- sidebar.classList.add('hidden');
- });
- const finance_options = [
- { name: "原油油桶", imageUrl: "./img/finance/01.webp" },
- { name: "黃金", imageUrl: "./img/finance/02.webp" },
- { name: "房屋", imageUrl: "./img/finance/03.webp" },
- { name: "車", imageUrl: "./img/finance/04.webp" },
- { name: "股市", imageUrl: "./img/finance/05.webp" },
- { name: "晶圓", imageUrl: "./img/finance/06.webp" },
- { name: "TSMC 台積電", imageUrl: "./img/finance/07.webp" },
- { name: "台幣", imageUrl: "./img/finance/08.webp" },
- { name: "美金", imageUrl: "./img/finance/09.webp" },
- { name: "購物車", imageUrl: "./img/finance/10.webp" },
- { name: "美中貿易", imageUrl: "./img/finance/11.webp" },
- { name: "世界地圖", imageUrl: "./img/finance/12.webp" },
- { name: "小麥", imageUrl: "./img/finance/13.webp" },
- { name: "玉米", imageUrl: "./img/finance/14.webp" },
- { name: "智慧產線", imageUrl: "./img/finance/15.webp" }
- ];
- const selectOptions = document.getElementById('finance_options');
- const selectOptions_table = document.getElementById('finance_options_table');
- finance_options.forEach((option, index) => {
- const optionElement = document.createElement('option');
- optionElement.value = index + 1; // 值从 1 开始
- optionElement.textContent = option.name;
- optionElement.setAttribute('data-image-url', option.imageUrl); // 存储图片 URL
- selectOptions.appendChild(optionElement);
- });
- selectOptions.addEventListener('change', function () {
- const selectedOption = selectOptions.options[selectOptions.selectedIndex];
- const imageUrl = selectedOption.getAttribute('data-image-url');
- document.getElementById('img_box_url').src = imageUrl;
- });
- // finance_options.forEach((option, index) => {
- // const optionElement = document.createElement('option');
- // optionElement.value = index + 1; // 值从 1 开始
- // optionElement.textContent = option.name;
- // optionElement.setAttribute('data-image-url', option.imageUrl); // 存储图片 URL
- // selectOptions_table.appendChild(optionElement);
- // });
- // selectOptions_table.addEventListener('change', function () {
- // const selectedOption = selectOptions_table.options[selectOptions_table.selectedIndex];
- // const imageUrl = selectedOption.getAttribute('data-image-url');
- // document.getElementById('img_box_url').src = imageUrl;
- // });
- document.querySelectorAll('input[name="ChartOptions"]').forEach(function (radio) {
- radio.addEventListener('change', function () {
- // 檢查哪個單選按鈕被選中
- if (this.value === 'line') {
- // 如果選擇了折線圖,執行相應的操作
- console.log("選擇了折線圖");
- // 在這裡執行顯示折線圖的相關代碼
- } else if (this.value === 'doughnut') {
- // 如果選擇了柱狀圖,執行相應的操作
- console.log("選擇了柱狀圖");
- // 在這裡執行顯示柱狀圖的相關代碼
- }
- });
- });
- function tokencheck() {
- if (!localStorage.getItem("access_token")) {
- window.location.href = "./login.html";
- } else {
- console.log("存在");
- }
- }
- tokencheck();
- function logout() {
- localStorage.removeItem("access_token");
- }
- $(document).on("click", "#logout", function (event) {
- alert("登出成功");
- logout();
- location.reload();
- });
- // speech_to_text
- var closeRecord = document.getElementById('recording_block_close');
- var recording_block = document.getElementById('recording_block');
- var PageLM = "2024-05-07 18:33";
- var recording_button = document.getElementById('recording_button');
- var stopButton = document.getElementById('stop');
- var audio = document.getElementById('audio');
- var recorder, audioBlob;
- closeRecord.addEventListener('click', () => {
- recPause();
- console.log('record-click')
- recording_block.classList.remove('show');
- recording_block.classList.add('hidden');
- });
- function reclog(s, color) {
- var now = new Date();
- var t = ("0" + now.getHours()).substr(-2)
- + ":" + ("0" + now.getMinutes()).substr(-2)
- + ":" + ("0" + now.getSeconds()).substr(-2);
- var div = document.createElement("div");
- var elem = document.querySelector(".reclog");
- elem.insertBefore(div, elem.firstChild);
- div.innerHTML = '<div style="color:' + (!color ? "" : color == 1 ? "red" : color == 2 ? "#0b1" : color) + '">[' + t + ']' + s + '</div>';
- };
- window.onerror = function (message, url, lineNo, columnNo, error) {
- //https://www.cnblogs.com/xianyulaodi/p/6201829.html
- reclog('<span style="color:red">【Uncaught Error】' + message + '<pre>' + "at:" + lineNo + ":" + columnNo + " url:" + url + "\n" + (error && error.stack || Html_$T("kBaF::不能获得错误堆栈")) + '</pre></span>');
- };
- if (!window.Html_$T) {//没有提供本页面用的国际化多语言支持时 返回中文文本
- window.Html_$T = function () {
- var a = arguments, txt = a[0].replace(/^.+?::/, ""), n = 0;
- for (var i = 0; i < a.length; i++) { if (typeof a[i] == "number") { n = i; break } }
- txt = txt.replace(/\{(\d+)\}/g, function (v, f) { v = a[+f + n]; return v == null ? "" : v });
- return txt;
- }
- window.Html_xT = function (v) { return v }
- }
- if (window.Recorder) {
- // reclog(Html_$T('BL9u::頁面已準備好,請先點擊打開錄音,然後點擊錄製'), 2);
- alert('錄音已準備好,請先點擊右下角打開錄音,即可開始錄製')
- } else {
- reclog(Html_$T("YzPd::js文件加载失败,请刷新重试!"), "#f00;font-size:50px");
- console.log('js文件加载失敗,请刷新重试!')
- }
- recording_button.addEventListener('click', async () => {
- recording_block.classList.toggle('show');
- recording_block.classList.toggle('hidden');
- recStart();
- });
- function recStart() {//打开了录音后才能进行start、stop调用
- rec.start();
- };
- stopButton.addEventListener('click', async () => {
- // recorder.stop();
- // uploadAudio(audioBlob);
- recStop();
- });
- window.onload = function () {
- recOpen();
- };
- // 錄音程式
- // =======================
- var rec, wave, recBlob;
- /**调用open打开录音请求好录音权限 Call open to open the recording and request the recording permission**/
- var recOpen = function () {//一般在显示出录音按钮或相关的录音界面时进行此方法调用,后面用户点击开始录音时就能畅通无阻了
- rec = null;
- wave = null;
- recBlob = null;
- var newRec = Recorder({
- type: "mp3", sampleRate: 16000, bitRate: 16 //mp3格式,指定采样率hz、比特率kbps,其他参数使用默认配置;注意:是数字的参数必须提供数字,不要用字符串;需要使用的type类型,需提前把格式支持文件加载进来,比如使用wav格式需要提前加载wav.js编码引擎
- , onProcess: function (buffers, powerLevel, bufferDuration, bufferSampleRate, newBufferIdx, asyncEnd) {
- // // //录音实时回调,大约1秒调用12次本回调
- document.querySelector(".recpowerx").style.width = powerLevel + "%";
- document.querySelector(".recpowert").innerText = formatMs(bufferDuration, 1) + " / " + powerLevel;
- // //可视化图形绘制
- // wave.input(buffers[buffers.length - 1], powerLevel, bufferSampleRate);
- }
- });
- newRec.open(function () {//打开麦克风授权获得相关资源
- rec = newRec;
- //此处创建这些音频可视化图形绘制浏览器支持妥妥的
- // wave = Recorder.FrequencyHistogramView({ elem: ".recwave" });
- reclog(Html_$T("GVCa::已打開錄音,可以點擊錄制開始錄音"), 2);
- }, function (msg, isUserNotAllow) {//用户拒绝未授权或不支持
- reclog((isUserNotAllow ? "UserNotAllow, " : "") + Html_$T("TOOV::打開錄音失敗:") + msg, 1);
- });
- };
- /**关闭录音,释放资源 Close recording, release resources**/
- function recClose() {
- if (rec) {
- rec.close();
- reclog(Html_$T("jqOs::已关闭"));
- } else {
- reclog(Html_$T("VOOw::未打开录音"), 1);
- };
- };
- /**开始录音 Start recording**/
- function recStart() {//打开了录音后才能进行start、stop调用
- if (rec && Recorder.IsOpen()) {
- recBlob = null;
- rec.start();
- reclog(Html_$T("CGdy::已經開始錄音...") + " " + rec.set.type + " " + rec.set.sampleRate + " " + rec.set.bitRate + "kbps");
- } else {
- reclog(Html_$T("ajKR::未打开錄音"), 1);
- };
- };
- /**暂停录音 Passing recording**/
- function recPause() {
- if (rec && Recorder.IsOpen()) {
- rec.pause();
- reclog(Html_$T("GvCy::已暂停"));
- } else {
- reclog(Html_$T("gCAR::未打開錄音"), 1);
- };
- };
- /**恢复录音 Resume recording**/
- function recResume() {
- if (rec && Recorder.IsOpen()) {
- rec.resume();
- reclog(Html_$T("5q1K::继续录音中..."));
- } else {
- reclog(Html_$T("Ob6S::未打开录音"), 1);
- };
- };
- /**结束录音,得到音频文件 Stop recording and get audio files**/
- function recStop() {
- if (!(rec && Recorder.IsOpen())) {
- reclog(Html_$T("5JuL::未打開錄音"), 1);
- return;
- };
- rec.stop(function (blob, duration) {
- console.log(blob, (window.URL || webkitURL).createObjectURL(blob), Html_xT(Html_$T("gOix::時長:{1}ms", 0, duration)));
- recBlob = blob;
- var file = new File([blob], "recording.mp3", { type: "audio/mp3" });
- // console.log(file);
- handleAudioToText(file);
- reclog(Html_$T("0LHf::已錄製mp3:{1}ms {2}字節,可以點擊播放、上傳、本地下载了", 0, formatMs(duration), blob.size), 2);
- }, function (msg) {
- reclog(Html_$T("kGZO::錄音失敗:") + msg, 1);
- });
- };
- /**播放 Play**/
- function recPlay() {
- if (!recBlob) {
- reclog(Html_$T("tIke::请先錄音,然后停止後再播放"), 1);
- return;
- };
- var cls = ("a" + Math.random()).replace(".", "");
- reclog(Html_$T('GlWb::播放中: ') + '<span class="' + cls + '"></span>');
- var audio = document.createElement("audio");
- audio.controls = true;
- document.querySelector("." + cls).appendChild(audio);
- //简单利用URL生成播放地址,注意不用了时需要revokeObjectURL,否则霸占内存
- audio.src = (window.URL || webkitURL).createObjectURL(recBlob);
- audio.play();
- setTimeout(function () {
- (window.URL || webkitURL).revokeObjectURL(audio.src);
- }, 5000);
- };
- /**上传 Upload**/
- function recUpload() {
- var blob = recBlob;
- if (!blob) {
- reclog(Html_$T("DUTn::请先录音,然后停止后再上传"), 1);
- return;
- };
- //本例子假设使用原始XMLHttpRequest请求方式,实际使用中自行调整为自己的请求方式
- //录音结束时拿到了blob文件对象,可以用FileReader读取出内容,或者用FormData上传
- var api = "http://127.0.0.1:9528";
- var onreadystatechange = function (xhr, title) {
- return function () {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- reclog(title + Html_$T("G2MU::上传成功") + ' <span style="color:#999">response: ' + xhr.responseText + '</span>', 2);
- } else {
- reclog(title + Html_$T("TUdi::没有完成上传,演示上传地址无需关注上传结果,只要浏览器控制台内Network面板内看到的请求数据结构是预期的就ok了。"), "#d8c1a0");
- console.error(Html_xT(title + Html_$T("HjDi::上传失败")), xhr.status, xhr.responseText);
- };
- };
- };
- };
- reclog(Html_$T("QnSI::开始上传到{1},请稍候... (你可以先到源码 /assets/node-localServer 目录内执行 npm run start 来运行本地测试服务器)", 0, api));
- /***方式一:将blob文件转成base64纯文本编码,使用普通application/x-www-form-urlencoded表单上传***/
- var reader = new FileReader();
- reader.onloadend = function () {
- var postData = "";
- postData += "mime=" + encodeURIComponent(blob.type);//告诉后端,这个录音是什么格式的,可能前后端都固定的mp3可以不用写
- postData += "&upfile_b64=" + encodeURIComponent((/.+;\s*base64\s*,\s*(.+)$/i.exec(reader.result) || [])[1]) //录音文件内容,后端进行base64解码成二进制
- //...其他表单参数
- var xhr = new XMLHttpRequest();
- xhr.open("POST", api + "/uploadBase64");
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.onreadystatechange = onreadystatechange(xhr, Html_$T("gG1f::上传方式一【Base64】"));
- xhr.send(postData);
- };
- reader.readAsDataURL(blob);
- /***方式二:使用FormData用multipart/form-data表单上传文件***/
- var form = new FormData();
- form.append("upfile", blob, "recorder.mp3"); //和普通form表单并无二致,后端接收到upfile参数的文件,文件名为recorder.mp3
- //...其他表单参数
- var xhr = new XMLHttpRequest();
- xhr.open("POST", api + "/upload");
- xhr.onreadystatechange = onreadystatechange(xhr, Html_$T("vDzB::上传方式二【FormData】"));
- xhr.send(form);
- };
- /**本地下载 Local download**/
- function recLocalDown() {
- if (!recBlob) {
- reclog(Html_$T("M86h::请先录音,然后停止后再下载"), 1);
- return;
- };
- var cls = ("a" + Math.random()).replace(".", "");
- recdown64.lastCls = cls;
- reclog(Html_$T('vJPl::点击 ') + '<span class="' + cls + '"></span>' + Html_$T('Whtc:: 下载,或复制文本')
- + '<button onclick="recdown64(\'' + cls + '\')">' + Html_$T('XK4l::生成Base64文本') + '</button><span class="' + cls + '_b64"></span>');
- var fileName = "recorder-" + Date.now() + ".mp3";
- var downA = document.createElement("A");
- downA.innerHTML = Html_$T("g8Fy::下载 ") + fileName;
- downA.href = (window.URL || webkitURL).createObjectURL(recBlob);
- downA.download = fileName;
- document.querySelector("." + cls).appendChild(downA);
- if (/mobile/i.test(navigator.userAgent)) {
- alert(Html_xT(Html_$T("DIEK::因移动端绝大部分国产浏览器未适配Blob Url的下载,所以本demo代码在移动端未调用downA.click()。请尝试点击日志中显示的下载链接下载")));
- } else {
- downA.click();
- }
- //不用了时需要revokeObjectURL,否则霸占内存
- //(window.URL||webkitURL).revokeObjectURL(downA.href);
- };
- function recdown64(cls) {
- var el = document.querySelector("." + cls + "_b64");
- if (recdown64.lastCls != cls) {
- el.innerHTML = '<span style="color:red">' + Html_$T("eKKx::老的数据没有保存,只支持最新的一条") + '</span>';
- return;
- }
- var reader = new FileReader();
- reader.onloadend = function () {
- el.innerHTML = '<textarea></textarea>';
- el.querySelector("textarea").value = reader.result;
- };
- reader.readAsDataURL(recBlob);
- };
- var formatMs = function (ms, all) {
- var ss = ms % 1000; ms = (ms - ss) / 1000;
- var s = ms % 60; ms = (ms - s) / 60;
- var m = ms % 60; ms = (ms - m) / 60;
- var h = ms;
- var t = (h ? h + ":" : "")
- + (all || h + m ? ("0" + m).substr(-2) + ":" : "")
- + (all || h + m + s ? ("0" + s).substr(-2) + "″" : "")
- + ("00" + ss).substr(-3);
- return t;
- };
- // =======================
- let mediaRecorder;
- let audioChunks = [];
- var recordText = document.getElementById('record_text');
- async function handleAudioToText(file) {
- console.log('tts')
- let audioLang = "cmn-Hant-TW"; // 音訊語言
- // let lang = localStorage.getItem("lang");
- let lang = "zh-tw";
- console.log("lang", lang);
- switch (lang) {
- case "zh-tw":
- audioLang = "cmn-Hant-TW";
- break;
- case "en-us":
- audioLang = "en-US";
- break;
- case "ja-jp":
- audioLang = "ja-JP";
- break;
- case "ko-kr":
- audioLang = "ko-KR";
- break;
- default:
- break;
- }
- let url = `https://cmm.ai:9001/gcp/speech-to-text?language_code=${audioLang}`;
- const formData = new FormData();
- formData.append("file", file);
- try {
- const response = await axios.post(url, formData);
- console.log("response", response);
- input_text_value = response.data;
- recording_block.classList.remove('show');
- recording_block.classList.add('hidden');
- if (input_text_value.length === 0) {
- alert('請再說一次')
- return
- }
- console.log(input_text_value[0]);
- inputField.value = input_text_value[0];
- // Show the result for a short time before executing get_data
- get_data(input_text_value);
- // get_data(input_text_value);
- } catch (error) {
- console.log("error", error);
- }
- }
- Object.defineProperty(obj, 'text', {
- get() {
- return this._text;
- },
- set(newValue) {
- console.log(`字符串从 "${this._text}" 变为 "${newValue}"`);
- this._text = newValue;
- }
- });
|