-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
184 lines (159 loc) · 6.55 KB
/
index.html
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
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>📈 Live Stock Prices</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://kit.fontawesome.com/a076d05399.js"></script> <!-- FontAwesome for icons -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <!-- Chart.js for graphs -->
<style>
body {
background-color: #121212;
color: white;
font-family: 'Arial', sans-serif;
}
.container {
margin-top: 30px;
text-align: center;
}
.stock-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 15px;
}
.stock-card {
width: 160px;
height: 160px;
background-color: #1e1e1e;
border-radius: 10px;
text-align: center;
padding: 15px;
box-shadow: 0px 4px 10px rgba(255, 255, 255, 0.1);
transition: transform 0.2s ease-in-out;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
cursor: pointer;
}
.stock-card:hover {
transform: scale(1.05);
}
.stock-symbol {
font-size: 20px;
font-weight: bold;
}
.stock-price {
font-size: 22px;
font-weight: bold;
}
.price-up { color: #00c853; }
.price-down { color: #d50000; }
.timestamp {
font-size: 12px;
color: #bdbdbd;
}
</style>
<script>
let stockPrices = {};
let priceHistory = {}; // Store historical prices for charts
let chart; // Global chart object
function subscribeStock(symbol) {
if (document.getElementById(symbol)) return; // Avoid duplicates
const eventSource = new EventSource(`/stocks/subscribe/${symbol}`);
let stockContainer = document.getElementById("stockContainer");
let card = document.createElement("div");
card.className = "stock-card";
card.id = symbol;
card.onclick = function() { showGraph(symbol); }; // Attach click event
card.innerHTML = `
<div class="stock-symbol">${symbol}</div>
<div class="stock-price" id="price-${symbol}">Loading...</div>
<div class="timestamp" id="time-${symbol}">--</div>
`;
stockContainer.appendChild(card);
priceHistory[symbol] = []; // Initialize price history array
eventSource.onmessage = function(event) {
let stockData = JSON.parse(event.data);
let priceElement = document.getElementById(`price-${symbol}`);
let timeElement = document.getElementById(`time-${symbol}`);
let prevPrice = stockPrices[symbol] || stockData.price;
priceElement.className = stockData.price > prevPrice ? "stock-price price-up" : "stock-price price-down";
priceElement.innerHTML = stockData.price.toFixed(2);
timeElement.innerHTML = stockData.timestamp;
stockPrices[symbol] = stockData.price;
// Store price history
priceHistory[symbol].push({ time: stockData.timestamp, price: stockData.price });
// Update the chart if it's open
if (chart && document.getElementById("chartTitle").innerText === symbol) {
updateChart(symbol);
}
};
eventSource.onerror = function() {
console.error(`Error receiving stock data for ${symbol}`);
eventSource.close();
};
}
function showGraph(symbol) {
document.getElementById("chartModal").style.display = "block";
document.getElementById("chartTitle").innerText = symbol;
drawChart(symbol);
}
function drawChart(symbol) {
const ctx = document.getElementById("stockChart").getContext("2d");
if (chart) chart.destroy(); // Destroy old chart if exists
chart = new Chart(ctx, {
type: "line",
data: {
labels: priceHistory[symbol].map(data => data.time),
datasets: [{
label: `Price of ${symbol}`,
data: priceHistory[symbol].map(data => data.price),
borderColor: "#00c853",
backgroundColor: "rgba(0, 200, 83, 0.2)",
fill: true,
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { title: { display: true, text: "Time" } },
y: { title: { display: true, text: "Price" } }
}
}
});
}
function updateChart(symbol) {
if (!chart) return;
chart.data.labels = priceHistory[symbol].map(data => data.time);
chart.data.datasets[0].data = priceHistory[symbol].map(data => data.price);
chart.update();
}
function closeModal() {
document.getElementById("chartModal").style.display = "none";
}
</script>
</head>
<body>
<div class="container">
<h2>📈 Real-Time Stock Prices</h2>
<div class="d-flex justify-content-center">
<input type="text" id="stockSymbol" class="form-control w-25 me-2" placeholder="Enter Stock Symbol">
<button class="btn btn-success" onclick="subscribeStock(document.getElementById('stockSymbol').value)">
<i class="fas fa-bell"></i> Subscribe
</button>
</div>
<div class="stock-container mt-4" id="stockContainer"></div>
</div>
<!-- Modal for Stock Chart -->
<div id="chartModal" class="modal" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.8);">
<div class="modal-content" style="background: white; color: black; padding: 20px; width: 60%; margin: 10% auto; border-radius: 10px; text-align: center;">
<h3 id="chartTitle"></h3>
<canvas id="stockChart" style="max-height: 300px;"></canvas>
<br>
<button class="btn btn-danger" onclick="closeModal()">Close</button>
</div>
</div>
</body>
</html>