-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (158 loc) · 6.18 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stock Price Visualization</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
body {
background: #ecf9ec; /* Fading from a light grey to white */
color: #333;
font-family: Arial, sans-serif;
}
h1, h2 {
padding-left: 20px;
color: #555;
}
table, th, td {
border: 1px solid #ddd;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
.highlight-red {
background-color: #ff9999; /* Light red */
color: rgb(60, 60, 60);
}
.highlight-green {
background-color: #99ff99; /* Light green */
color: rgb(60, 60, 60);
}
canvas {
background-color: #ffffff; /* White background for the chart */
border: 1px solid #ddd; /* Slight border for definition */
box-shadow: 0 2px 4px rgba(0,0,0,0.1); /* Subtle shadow for depth */
}
</style>
</head>
<body>
<h1> Real-Time Amazon Stock Price Visualization 🚀📈</h1>
<div style="display: flex; justify-content: space-between;">
<div style="flex: 1; padding: 20px;">
<canvas id="priceChart" width="600" height="300"></canvas>
</div>
<div style="flex: 1; padding: 20px;">
<h2>Summary</h2>
<p><strong id="lastRefreshTime">As of </strong></p>
<p><strong>Current Price:</strong> <span id="currentPrice"></span></p>
<h2>Latest Prices</h2>
<table id="priceTable">
<tr>
<th>Timestamp</th>
<th>Price</th>
</tr>
<!-- Rows will be added here dynamically -->
</table>
</div>
</div>
<script>
var ctx = document.getElementById('priceChart').getContext('2d');
var priceChart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Stock Price',
data: [],
borderColor: '#4a90e2', /* Subtle blue */
backgroundColor: 'rgba(74, 144, 226, 0.1)', /* Light blue fill */
tension: 0.1
}, {
label: 'Moving Average',
data: [],
borderColor: '#ff6f61', /* Soft red */
borderDash: [5, 5]
}]
},
options: {
scales: {
y: {
beginAtZero: false
}
},
animation: {
duration: 0 // Disable all animations
},
plugins: {
legend: {
labels: {
color: '#333' // ensures the text is easily readable in light mode
}
}
}
}
});
function fetchData() {
$.ajax({
url: '/data',
type: 'GET',
success: function(res) {
priceChart.data.labels = res.timestamp;
priceChart.data.datasets[0].data = res.price_data;
priceChart.data.datasets[1].data = calculateMovingAverage(res.price_data, 5);
priceChart.update('none');
document.getElementById('currentPrice').textContent = res.price_data[res.price_data.length - 1];
updateLastRefreshTime();
updatePriceTable(res.timestamp, res.price_data, res.flags);
}
});
}
function calculateMovingAverage(data, windowSize) {
let avgData = [];
for (let i = 0; i < data.length; i++) {
let windowEnd = Math.min(data.length, i + windowSize);
let sum = 0;
for (let j = i; j < windowEnd; j++) {
sum += data[j];
}
avgData.push(sum / (windowEnd - i));
}
return avgData;
}
function updatePriceTable(timestamps, prices, flags) {
var table = document.getElementById('priceTable');
var existingRows = table.getElementsByTagName('tr');
var maxRows = 20; // Maximum number of rows allowed in the table
// Add new rows with the latest data
timestamps.forEach(function(timestamp, i) {
// Insert new row at the top (after header)
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.textContent = timestamp;
cell2.textContent = prices[i];
if (flags[i] === 1) {
row.className = 'highlight-green';
} else if (flags[i] === -1) {
row.className = 'highlight-red';
}
// Ensure table does not exceed maximum row count
if (table.rows.length > maxRows + 1) { // +1 to account for the header row
table.deleteRow(-1); // Remove the oldest row at the bottom
}
});
}
function updateLastRefreshTime() {
var currentTime = new Date();
var formattedTime = currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
document.getElementById('lastRefreshTime').innerText = "As of " + formattedTime;
}
fetchData(); // initial data fetch
setInterval(fetchData, 5000); // fetch data every 5 seconds
</script>
</body>
</html>