heartbeatserver/views/plot.html
Ruben van de Ven f68dc8bd62 Fixes
2018-10-30 23:17:08 +01:00

147 lines
3.1 KiB
HTML

<!DOCTYPE html>
<meta charset="utf-8" />
<title>Quantified Other: Heart Edition</title>
<style media="screen">
body{
font-family: Helvetica, sans-serif;
}
#graph{
height: calc(100vh);
width:100vw;
position: absolute;
top:0;
left:0;
z-index: -1;
}
#downloads {
border: solid #ccc 1px;
padding: 10px;
position: absolute;
top: 180px;
right: 10px;
background: aliceblue;
font-weight: bold;
width: 200px;
}
#title {
background: aliceblue;
width: 200px;
height: 130px;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 20pt;
padding: 10px;
position: absolute;
top: 10px;
right: 10px;
border: solid 1px #ccc;
text-shadow: 1px 1px #ccc, -1px -1px #ccc,1px -1px #ccc, -1px 1px #ccc;
}
#value {
padding-left: 6px;
font-size: 60px;
display: block;
float: right;
width: 50%;
}
#downloads span {
font-weight: normal;
margin-left: 20px;
}
</style>
<script src="https://cdn.plot.ly/plotly-1.5.0.min.js"></script>
<script language="javascript" type="text/javascript">
var wsUri = window.location.toString().replace('http','ws') + "ws";
console.log(wsUri)
</script>
<h2 id="title">live bpm: <span id='value'>...</span></h2>
<div id='downloads'>
Data
<span><a href='/1h.csv'>hour</a></span>
<span><a href='/24h.csv'>day</a></span>
<span><a href='/week.csv'>week</a></span>
</div>
<div id="graph">
</div>
<script type="text/javascript">
var valueEl = document.getElementById('value');
var audio = new Audio('/assets/heartbeat.mp3');
Plotly.d3.csv("/1h.csv", function(err, rows){
console.log(rows);
function unpack(rows, key) {
return rows.map(function(row) { return row[key]; });
}
var trace1 = {
type: "scatter",
mode: "lines",
name: 'BPM',
x: unpack(rows, 'timestamp'),
y: unpack(rows, 'bpm'),
line: {color: '#17BECF'}
}
var data = [trace1];
var layout = {
title: 'Last hour',
};
var currentBpm = null;
Plotly.newPlot('graph', data, layout);
var wsUri = window.location.toString().replace('http','ws') + "ws";
var connectSocket = function() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) {
console.log("opened");
};
websocket.onclose = function(evt) {
console.log("closed", evt);
valueEl.innerHTML = '...';
setTimeout(connectSocket, 1000 * 10);
};
websocket.onmessage = function(evt) {
data = JSON.parse(evt.data);
var update = {
x: [[data['timestamp']]],
y: [[data['bpm']]]
}
console.log("update", update)
valueEl.innerHTML = data['bpm'];
currentBpm = data['bpm'];
Plotly.extendTraces('graph', update, [0])
};
websocket.onerror = function(evt) {
};
}
connectSocket();
let tick = function() {
var duration = 10000;
con
if(currentBpm != null) {
audio.play();
duration = 60000 / currentBpm;
}
setTimeout(tick, duration);
}
tick();
});
</script>