mdj1412
commit first
c109682
raw
history blame
7.13 kB
// jQuery
// $(document).ready(function() { });
$(function() {
chartInit();
});
function handleReturn(output) {
return output;
}
function chartInit() {
// Ticker ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ
// ํ•ด๋‹น class์˜ text ๊ฐ€์ ธ์˜ค๊ธฐ
let ticker = document.querySelector('.tickerName').textContent;
// console.log(ticker.indexOf(' '));
idx = ticker.indexOf(' ');
ticker = ticker.substring(0, idx);
// Javascript -> Flask (Python) -> Javascript
chart_data = sendAjax_async("/chart", {"ticker": ticker}, "json", handleReturn);
console.log(chart_data);
console.log(Object.keys(chart_data.Close));
// x์ถ•๊ณผ data ์„ค์ •
// data: [{'x': date, 'o': open, 'h': high, 'l': low, 'c': close}, { }, { }, ... ]
data = [];
key_list = Object.keys(chart_data.Close);
for (var i=key_list.length-15; i<key_list.length; i++) {
key = key_list[i];
const [year, month, day] = key.split("-");
const x = new Date(parseInt(year), parseInt(month), parseInt(day), 9, 0, 0, 0).getTime();
data.push({'x': x, 'o': chart_data.Open[key].toFixed(2), 'h': chart_data.High[key].toFixed(2), 'l': chart_data.Low[key].toFixed(2), 'c': chart_data.Close[key].toFixed(2)})
}
console.log("data : ", data);
// Javascript chart.js candlestick
let mychart = document.getElementById('myChart');
new Chart(mychart, {
type: 'candlestick',
data: {
datasets: [{
label: 'CHRT - '.concat(ticker),
data: data
}]
}
});
//////////////////////////////////////////////////////////////////
// Javascript๋ฅผ ์ด์šฉํ•ด HTML์— ๋™์ ์œผ๋กœ ํƒœ๊ทธ ์ถ”๊ฐ€
// a ํƒœ๊ทธ onclick ์ ์šฉ
const goTicker = document.querySelector('.goticker');
let goTickerURL = '/'.concat(ticker)
goTicker.setAttribute('href', goTickerURL);
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// table title ํ‘œ์‹œ
// ํ•ด๋‹น class์˜ text ์ง‘์–ด๋„ฃ๊ธฐ
const table_title = document.querySelector('.table-title');
$('.table .table-title').text(ticker.concat(' News'));
// Javascript -> Flask (Python) -> Javascript
news = sendAjax_async(url="/news", data={"ticker": ticker}, dataType="json", handle=handleReturn);
news_table = document.querySelector('.table .news-table');
// console.log(news_table.innerHTML);
// console.log(news);
// console.log(Object.keys(news)); // key ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
// console.log(typeof Object.keys(news));
/*
[ ๋‚ ์งœ ์ •๋ ฌ ]
"news"์—์„œ index์— ๋Œ€ํ•ด์„œ ์ •๋ ฌ์„ ํ•˜๊ณ  reduce() ํ•จ์ˆ˜๋ฅผ ์ ์šฉ.
reduce() : ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ reducer ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜
*/
sorted_news = {}
sorted_news = Object.keys(news).sort(function (a, b) {
if (a < b) { return 1; }
else if (a > b) { return -1; }
else { return 0; }
}).reduce((sorted_news, key) => {
sorted_news[key] = news[key];
return sorted_news;
}, {});
// console.log(sorted_news);
var key_list = Object.keys(chart_data.Open);
var open_list = Object.values(chart_data.Open);
var close_list = Object.values(chart_data.Close);
for (var i=0; i<key_list.length; i++) {
const [year, month, day] = key_list[i].split("-");
key_list[i] = year + '.' + month + '.' + day;
}
console.log(key_list);
// List ์•ˆ์˜ value๋ฅผ ๋ฝ‘์„ ๋•Œ, (Python) => for item in list:
Object.keys(sorted_news).forEach(key => {
var idx = key_list.indexOf(String(key));
if (idx != -1) { var diff = ((open_list[idx]-close_list[idx-1])/(open_list[idx]) * 100.0).toFixed(2); }
else { var diff = '.'; }
if (diff == '.') {
var diff_html = '<th class="news diff">' + diff + '</th>';
}
else if (diff > 0) {
var diff_html = '<th class="news diff up">+' + diff + ' %</th>';
}
else {
var diff_html = '<th class="news diff down">' + diff + ' %</th>';
}
var html = '<tr align="center" bgcolor="white"><th>+</th><th>' + key + '</th>' + diff_html + '<td style="text-align: left;">';
for (var i = 0; i < sorted_news[key].length; i++) {
var title = sorted_news[key][i].substring(0, sorted_news[key][i].length-4);
var sendTitle = title; // Javascript -> Python ๋ณด๋‚ด๊ธฐ ์œ„ํ•œ title
// title์—์„œ & ํ‘œ์‹œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ.
// Title ์—์„œ '&'๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋Š”๋ฐ ๋”ฐ๋กœ ๊ตฌ๋ณ„ํ•ด์•ผ ๋œ๋‹ค.
// andSymbolInTitle ์—์„œ ๊ฐ€์ ธ์˜จ '&' ์œ„์น˜ index๋ฅผ title๊ณผ ํ•ฉ์ณ์ค€๋‹ค.
andSymbolInTitle = [];
let idx = 0;
// title = "asdf&asdf&AS&DF&&";
// sendTitle = title;
while (true) {
idx = sendTitle.indexOf('&', idx);
if (idx == -1) { break; }
sendTitle = sendTitle.substring(0, idx) + sendTitle.substring(idx+1, sendTitle.length);
// console.log(sendTitle);
andSymbolInTitle.push(idx + andSymbolInTitle.length);
}
var link = String('"/info?ticker='.concat(ticker, '&date=', key, '&title=', sendTitle, '&andSymbolInTitle=', andSymbolInTitle, '"'));
// console.log(link);
html = html + '<a href=' + link + '>' + title + '</a><br>';
}
html = html + '</td>';
news_table.innerHTML = news_table.innerHTML + html;
});
}
/**
*
* @param {string} url from javascript to flask(python) with route
* @param {dictionary} data from javascript to flask(python) with data
* @param {function} handle ํฐ ์˜๋ฏธ ์—†์Œ
*/
function sendAjax(url, data, handle) {
/*
jQuery.getJSON(url, [, data], [, success])
Load JSON-encoded data from the server using a GET HTTP request.
*/
$.getJSON(url, data,
function(response) {
handle(response.result);
});
}
/**
*
* @param {string} url from javascript to flask(python) with route
* @param {dictionary} data from javascript to flask(python) with data
* @param {string} dataType The type of data that you're expecting back from the server. (ex. "json")
* @param {function} handle ํฐ ์˜๋ฏธ ์—†์Œ
* @returns from flask(python) to javascript with data
*/
function sendAjax_async(url, data, dataType, handle) {
/*
jQuery.ajax(url, [, settings])
jQuery.getJSON => Asynchronous (๋น„๋™๊ธฐ์‹)
Synchronous => ๋™๊ธฐ์‹ : ์ฝ”๋“œ ์ˆœ์„œ๋Œ€๋กœ ์ง„ํ–‰
*/
var search_var;
console.log("Internal : sendAjax async");
$.ajax(url=url, settings={data: data, dataType: dataType, async: false,
success: function(response) {
console.log("Success : ", typeof response);
search_var = handle(response.result); // handle, ํฐ ์˜๋ฏธ ์—†์Œ
}
});
return search_var
}