File size: 7,788 Bytes
450d83e | 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 | // Global variables
let ecgData = [];
let bpmHistory = [];
let historyRecords = [];
let isPaused = false;
let updateInterval;
let ecgChart;
// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
initializeChart();
startMonitoring();
setupEventListeners();
});
function initializeChart() {
const ctx = document.getElementById('ecgChart').getContext('2d');
ecgChart = new Chart(ctx, {
type: 'line',
data: {
labels: Array(100).fill(''),
datasets: [{
label: 'ECG',
data: Array(100).fill(0),
borderColor: '#EF4444',
borderWidth: 2,
tension: 0.1,
pointRadius: 0
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
min: -2,
max: 2,
display: false
},
x: {
display: false
}
},
animation: {
duration: 0
},
plugins: {
legend: {
display: false
}
}
}
});
}
function startMonitoring() {
// Initial data
generateInitialData();
// Start updating the chart every second
updateInterval = setInterval(updateECGData, 1000);
}
function generateInitialData() {
// Generate some initial random ECG-like data
for (let i = 0; i < 100; i++) {
ecgData.push(generateECGPoint(i));
}
updateChart();
}
function generateECGPoint(index) {
// Simulate ECG data with some randomness
const position = index % 100;
// Generate a basic ECG waveform with some randomness
if (position > 90) {
return 0.2 + Math.random() * 0.1;
} else if (position > 80) {
return -0.5 - Math.random() * 0.2;
} else if (position > 70) {
return 1.5 + Math.random() * 0.3;
} else if (position > 60) {
return -0.8 - Math.random() * 0.2;
} else if (position > 50) {
return 0.3 + Math.random() * 0.1;
} else {
// Baseline with some small noise
return (Math.random() - 0.5) * 0.2;
}
}
function updateECGData() {
if (isPaused) return;
// Remove first point and add new one
ecgData.shift();
ecgData.push(generateECGPoint(Math.floor(Math.random() * 100)));
// Update chart
updateChart();
// Calculate and display BPM
const bpm = 60 + Math.floor(Math.random() * 60);
updateBPM(bpm);
// Check for irregularity (10% chance)
const isIrregular = Math.random() < 0.1;
updateIrregularityStatus(isIrregular, bpm);
// Add to history
addToHistory(bpm, isIrregular);
// Update stats
updateStats();
}
function updateChart() {
ecgChart.data.datasets[0].data = ecgData;
ecgChart.update();
}
function updateBPM(bpm) {
document.getElementById('bpmDisplay').textContent = bpm;
document.getElementById('currentBpm').textContent = bpm;
bpmHistory.push(bpm);
// Limit history to last 100 readings
if (bpmHistory.length > 100) {
bpmHistory.shift();
}
}
function updateIrregularityStatus(isIrregular, bpm) {
const statusElement = document.getElementById('irregularityStatus');
const heartStatus = document.getElementById('heartStatus');
if (isIrregular) {
statusElement.innerHTML = `
<i data-feather="alert-circle" class="inline mr-2"></i>
<span>Irregular heartbeat detected (${bpm} BPM)</span>
`;
statusElement.className = 'text-center py-4 px-6 rounded-lg irregular mb-4';
heartStatus.innerHTML = `
<span class="h-4 w-4 rounded-full bg-yellow-500 animate-pulse mr-2"></span>
<span class="text-gray-600">Irregular</span>
`;
} else {
statusElement.innerHTML = `
<i data-feather="check-circle" class="inline mr-2"></i>
<span>Regular heartbeat (${bpm} BPM)</span>
`;
statusElement.className = 'text-center py-4 px-6 rounded-lg regular mb-4';
heartStatus.innerHTML = `
<span class="h-4 w-4 rounded-full bg-green-500 animate-pulse mr-2"></span>
<span class="text-gray-600">Regular</span>
`;
}
feather.replace();
}
function addToHistory(bpm, isIrregular) {
const now = new Date();
const timeString = now.toLocaleTimeString();
historyRecords.unshift({
time: timeString,
bpm: bpm,
isIrregular: isIrregular,
timestamp: now.getTime()
});
// Limit history to 50 records
if (historyRecords.length > 50) {
historyRecords.pop();
}
updateHistoryTable();
}
function updateHistoryTable() {
const tableBody = document.getElementById('historyTable');
tableBody.innerHTML = '';
historyRecords.forEach(record => {
const row = document.createElement('tr');
row.innerHTML = `
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${record.time}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium ${record.isIrregular ? 'text-yellow-600' : 'text-gray-900'}">${record.bpm}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${record.isIrregular ? 'bg-yellow-100 text-yellow-800' : 'bg-green-100 text-green-800'}">
${record.isIrregular ? 'Irregular' : 'Regular'}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<button class="text-blue-500 hover:text-blue-700">
<i data-feather="info" class="w-4 h-4"></i>
</button>
</td>
`;
tableBody.appendChild(row);
});
feather.replace();
}
function updateStats() {
if (bpmHistory.length === 0) return;
const sum = bpmHistory.reduce((a, b) => a + b, 0);
const avg = Math.round(sum / bpmHistory.length);
const min = Math.min(...bpmHistory);
const max = Math.max(...bpmHistory);
document.getElementById('averageBpm').textContent = avg;
document.getElementById('minBpm').textContent = min;
document.getElementById('maxBpm').textContent = max;
// Update irregular beats count
const irregularCount = historyRecords.filter(r => r.isIrregular).length;
document.getElementById('irregularBeats').textContent = irregularCount;
// Update last irregularity time
const lastIrregular = historyRecords.find(r => r.isIrregular);
if (lastIrregular) {
document.getElementById('lastIrregularity').textContent = lastIrregular.time;
}
}
function setupEventListeners() {
// Pause/Resume button
document.getElementById('pauseBtn').addEventListener('click', function() {
isPaused = !isPaused;
this.innerHTML = isPaused ?
'<i data-feather="play"></i> Resume' :
'<i data-feather="pause"></i> Pause';
feather.replace();
});
// Export button
document.getElementById('exportBtn').addEventListener('click', function() {
alert('Data exported successfully!');
});
// Clear button
document.getElementById('clearBtn').addEventListener('click', function() {
if (confirm('Are you sure you want to clear all history?')) {
historyRecords = [];
bpmHistory = [];
updateHistoryTable();
updateStats();
}
});
} |