Files
2026-02-20 17:50:40 +00:00

248 lines
7.6 KiB
C

#ifndef WEB_GRAPH_H
#define WEB_GRAPH_H
const char HTML_GRAPH[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CAN FD Graph - ESP32 Logger</title>
<script src="https://unpkg.com/uplot@1.6.24/dist/uPlot.iife.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/uplot@1.6.24/dist/uPlot.min.css">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #1a1a2e;
color: #eee;
line-height: 1.6;
}
.header {
background: #16213e;
padding: 1rem;
text-align: center;
border-bottom: 2px solid #e94560;
}
.header h1 { color: #e94560; font-size: 1.5rem; }
.nav {
background: #0f3460;
padding: 0.5rem;
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 0.5rem;
}
.nav a {
color: #fff;
text-decoration: none;
padding: 0.5rem 1rem;
border-radius: 4px;
transition: background 0.3s;
}
.nav a:hover, .nav a.active { background: #e94560; }
.container {
max-width: 1400px;
margin: 0 auto;
padding: 1rem;
}
.graph-container {
background: #16213e;
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
}
.controls {
background: #16213e;
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
display: flex;
gap: 1rem;
flex-wrap: wrap;
align-items: center;
}
.signal-list {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.signal-item {
display: flex;
align-items: center;
gap: 0.5rem;
}
.signal-color {
width: 16px;
height: 16px;
border-radius: 50%;
}
.btn {
background: #e94560;
color: #fff;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
}
.btn:hover { background: #ff6b6b; }
#chart { margin-top: 1rem; }
.uplot {
background: #0f3460;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="header">
<h1>Real-time CAN Signal Graph</h1>
</div>
<nav class="nav">
<a href="/">Dashboard</a>
<a href="/graph" class="active">Graph</a>
<a href="/files">Files</a>
<a href="/can">CAN Transmit</a>
<a href="/settings">Settings</a>
</nav>
<div class="container">
<div class="controls">
<button class="btn" onclick="togglePause()" id="btnPause">Pause</button>
<button class="btn" onclick="clearGraph()">Clear</button>
<div class="signal-list" id="signalList"></div>
</div>
<div class="graph-container">
<div id="chart"></div>
</div>
</div>
<script>
let ws = null;
let uplot = null;
let paused = false;
let maxPoints = 500;
const colors = [
'#e94560', '#00d9ff', '#f39c12', '#2ecc71',
'#9b59b6', '#1abc9c', '#e74c3c', '#3498db',
'#f1c40f', '#95a5a6'
];
let signals = [];
let data = [[], [], [], [], [], [], [], [], [], [], []];
function initChart() {
const opts = {
width: document.getElementById('chart').offsetWidth,
height: 500,
title: "CAN Signals",
axes: [
{ label: "Time" },
{ label: "Value" }
],
scales: {
x: { time: true },
y: { auto: true }
},
series: [
{ label: "Time" }
]
};
for (let i = 0; i < 10; i++) {
opts.series.push({
label: `Signal ${i + 1}`,
stroke: colors[i],
width: 2
});
}
uplot = new uPlot(opts, data, document.getElementById('chart'));
}
function connectWebSocket() {
const host = window.location.hostname;
const wsPort = 81;
ws = new WebSocket('ws://' + host + ':' + wsPort);
ws.onopen = () => console.log('WebSocket connected');
ws.onclose = () => setTimeout(connectWebSocket, 3000);
ws.onmessage = (e) => {
const msg = JSON.parse(e.data);
if (msg.type === 'signal') {
msg.signals.forEach((sig, i) => {
addPoint(sig.signal_id, sig.value, msg.timestamp);
});
}
};
}
function updateGraph(msg) {
const timestamp = msg.timestamp / 1000;
// Add time to first series
data[0].push(timestamp);
// Add values for each signal
for (let i = 0; i < 10; i++) {
const signal = msg.signals[i];
if (signal) {
data[i + 1].push(signal.value);
updateSignalList(i, signal.id, signal.value);
} else {
data[i + 1].push(null);
}
}
// Keep only maxPoints
if (data[0].length > maxPoints) {
for (let i = 0; i < data.length; i++) {
data[i].shift();
}
}
uplot.setData(data);
}
function updateSignalList(index, id, value) {
const list = document.getElementById('signalList');
let item = document.getElementById('signal-' + index);
if (!item) {
item = document.createElement('div');
item.className = 'signal-item';
item.id = 'signal-' + index;
item.innerHTML = `
<div class="signal-color" style="background: ${colors[index]}"></div>
<span id="signal-name-${index}">${id}</span>:
<span id="signal-val-${index}">${value.toFixed(2)}</span>
`;
list.appendChild(item);
} else {
document.getElementById('signal-val-' + index).textContent = value.toFixed(2);
}
}
function togglePause() {
paused = !paused;
document.getElementById('btnPause').textContent = paused ? 'Resume' : 'Pause';
}
function clearGraph() {
data = [[], [], [], [], [], [], [], [], [], [], []];
uplot.setData(data);
document.getElementById('signalList').innerHTML = '';
}
window.addEventListener('resize', () => {
uplot.setSize({
width: document.getElementById('chart').offsetWidth,
height: 500
});
});
initChart();
connectWebSocket();
</script>
</body>
</html>
)rawliteral";
#endif // WEB_GRAPH_H