-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.html
More file actions
281 lines (246 loc) · 10.1 KB
/
client2.html
File metadata and controls
281 lines (246 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<title>Top Buy/Sell Ratio Stocks</title>
<!-- Thêm Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Thêm Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
background-color: #f4f6f9;
margin: 0;
padding: 0;
}
h2 {
margin-top: 20px;
text-align: center;
color: #333;
}
.container {
margin-top: 20px;
}
.loading {
text-align: center;
color: #999;
font-style: italic;
margin-top: 20px;
}
.table-hover tbody tr:hover {
background-color: #f1f1f1;
}
.chart-container {
margin-top: 40px;
}
</style>
</head>
<body>
<div class="container">
<h2>Top Buy/Sell Ratio Stocks</h2>
<!-- Bộ chọn ngày -->
<div class="input-group mb-3" id="datePicker">
<span class="input-group-text">Chọn ngày:</span>
<input type="date" class="form-control" id="dateInput">
<button class="btn btn-primary" type="button" id="dateButton">Xem dữ liệu</button>
</div>
<div id="loading" class="loading">Đang tải dữ liệu...</div>
<!-- Thanh tìm kiếm -->
<div class="input-group mb-3" style="display:none;" id="searchBar">
<input type="text" class="form-control" placeholder="Tìm kiếm mã cổ phiếu" id="searchInput">
<button class="btn btn-outline-secondary" type="button" id="searchButton">Tìm kiếm</button>
</div>
<!-- Bảng dữ liệu -->
<table id="stockTable" class="table table-striped table-bordered table-hover" style="display:none;">
<thead class="table-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Mã CP</th>
<th scope="col">Khối lượng mua</th>
<th scope="col">Khối lượng bán</th>
<th scope="col">Tổng giá trị</th>
<th scope="col">Tỷ lệ Mua/Bán</th>
</tr>
</thead>
<tbody id="tableBody">
<!-- Dữ liệu sẽ được thêm vào đây bởi JavaScript -->
</tbody>
</table>
<!-- Biểu đồ -->
<div class="chart-container" style="display:none;">
<canvas id="stockChart"></canvas>
</div>
</div>
<!-- Thêm Bootstrap JS và Popper.js -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
let socket;
let stockData = [];
let stockChart; // Khai báo biến để lưu trữ biểu đồ
// Lấy ngày hiện tại ở định dạng YYYY-MM-DD
const today = new Date().toISOString().split('T')[0];
const dateInput = document.getElementById("dateInput");
dateInput.value = today; // Đặt giá trị mặc định cho input date
// Hàm kết nối WebSocket với ngày được chọn
function connectWebSocket(dateStr) {
// Nếu đã có kết nối WebSocket trước đó, đóng nó
if (socket && socket.readyState === WebSocket.OPEN) {
socket.close();
}
// Hiển thị thông báo loading và ẩn các phần tử khác
const loading = document.getElementById("loading");
const table = document.getElementById("stockTable");
const searchBar = document.getElementById("searchBar");
const chartContainer = document.querySelector('.chart-container');
loading.style.display = "block";
loading.innerText = "Đang tải dữ liệu...";
table.style.display = "none";
searchBar.style.display = "none";
chartContainer.style.display = "none";
// Kết nối tới WebSocket server với ngày được chọn
socket = new WebSocket(`ws://intraday_socket.microtrade.club/ws_top_buy_sell_ratio/${dateStr}`);
// Khi kết nối WebSocket được mở
socket.onopen = () => {
console.log("Đã kết nối tới WebSocket server cho ngày:", dateStr);
};
// Khi nhận được dữ liệu từ WebSocket server
socket.onmessage = (event) => {
const response = JSON.parse(event.data);
const data = response.data;
console.log("Nhận được dữ liệu:", data);
// Ẩn thông báo loading và hiển thị bảng, thanh tìm kiếm
loading.style.display = "none";
table.style.display = "table";
searchBar.style.display = "flex";
// Lưu trữ dữ liệu để sử dụng cho tìm kiếm và biểu đồ
stockData = data;
// Hiển thị dữ liệu
displayData(stockData);
updateChart(stockData);
};
// Xử lý khi kết nối WebSocket bị đóng
socket.onclose = (event) => {
console.log("Đã ngắt kết nối với WebSocket server", event);
if (event.code !== 1000) { // Nếu không phải đóng bình thường
const loading = document.getElementById("loading");
loading.style.display = "block";
loading.innerText = "Kết nối đã đóng. Vui lòng thử lại.";
}
};
// Xử lý lỗi kết nối WebSocket
socket.onerror = (error) => {
console.error("Lỗi WebSocket:", error);
const loading = document.getElementById("loading");
loading.style.display = "block";
loading.innerText = "Đã xảy ra lỗi. Vui lòng thử lại sau.";
};
}
// Gọi hàm kết nối WebSocket với ngày hiện tại khi tải trang
connectWebSocket(today);
// Xử lý khi người dùng nhấn nút "Xem dữ liệu"
const dateButton = document.getElementById("dateButton");
dateButton.addEventListener("click", () => {
const selectedDate = dateInput.value;
if (selectedDate) {
connectWebSocket(selectedDate);
}
});
// Hàm hiển thị dữ liệu trong bảng
function displayData(data) {
const tableBody = document.getElementById("tableBody");
tableBody.innerHTML = "";
data.forEach((item, index) => {
const row = document.createElement("tr");
// Liên kết tới trang chi tiết cho mỗi mã cổ phiếu
const link = `https://fireant.vn/top-symbols/content/symbols/${item._id}`;
row.innerHTML = `
<th scope="row">${index + 1}</th>
<td><a href="${link}" target="_blank">${item._id}</a></td>
<td>${Number(item.buy_vol).toLocaleString()}</td>
<td>${Number(item.sell_vol).toLocaleString()}</td>
<td>${Number(item.total_value).toLocaleString()}</td>
<td>${Number(item.rate_buy_sell).toFixed(2)}</td>
`;
tableBody.appendChild(row);
});
}
// Xử lý tìm kiếm
const searchInput = document.getElementById("searchInput");
const searchButton = document.getElementById("searchButton");
searchButton.addEventListener("click", () => {
const query = searchInput.value.trim().toUpperCase();
const filteredData = stockData.filter(item => item._id.includes(query));
displayData(filteredData);
updateChart(filteredData);
});
searchInput.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
searchButton.click();
}
});
// Hàm cập nhật biểu đồ
function updateChart(data) {
const chartContainer = document.querySelector('.chart-container');
chartContainer.style.display = "block";
const ctx = document.getElementById('stockChart').getContext('2d');
const labels = data.map(item => item._id);
const buyVolumes = data.map(item => item.buy_vol);
const sellVolumes = data.map(item => item.sell_vol);
if (stockChart) {
stockChart.destroy(); // Hủy biểu đồ cũ nếu tồn tại
}
stockChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: 'Khối lượng mua',
data: buyVolumes,
backgroundColor: 'rgba(75, 192, 192, 0.7)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
},
{
label: 'Khối lượng bán',
data: sellVolumes,
backgroundColor: 'rgba(255, 99, 132, 0.7)',
borderColor: 'rgba(255,99,132,1)',
borderWidth: 1
}
]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
ticks: {
callback: function(value) {
return value.toLocaleString();
}
}
}
},
plugins: {
tooltip: {
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += context.parsed.y.toLocaleString();
}
return label;
}
}
}
}
}
});
}
</script>
</body>
</html>