Simple plot of blood pressure in a heart over a short period of time.
Built with blockbuilder.org
forked from lwthatcher's block: Blood Pressure Waveform
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis--match text{
fill: purple;
}
.line {
fill: none;
stroke: red;
stroke-width: 1.5px;
}
.bar {
fill: purple;
opacity: .5;
}
.bar:hover {
fill: red;
opacity: .7;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 50, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var y2 = d3.scaleLinear()
.range([height, 0]);
var line = d3.line()
.x(d => x(d.t))
.y(d => y(d.value));
var matched = d3.area()
.curve(d3.curveMonotoneX)
.x(d => x(d.t))
.y0(height)
.y1(d => y2(d.match));
var notch_idx = 87;
var template_size = 3;
d3.csv("data.dat", type, (err, data) => {
if (err) throw err;
let template = rangeAround(data, notch_idx, template_size);
console.log("template", template);
template = normalize(template);
check(template)
console.info("normalized template", template);
data = convolve(data, template);
// console.log('data', data);
x.domain(d3.extent(data, d => d.t));
y.domain(d3.extent(data, d => d.value));
y2.domain(d3.extent(data, d => Math.max(d.match, 0)));
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
svg.append("g")
.attr("class", "axis axis--match")
.attr("transform", "translate( " + width + ", 0 )")
.call(d3.axisRight(y2));
// Blood Pressure
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
// Match
// svg.append("path")
// .datum(data)
// .attr("class", "match")
// .attr("d", matched);
var bars = svg.append("g")
bars.selectAll(".bar")
.data(data)
.enter().filter(d => "match" in d).append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.t) -2.5; })
.attr("y", function(d) { return y2(d.match); })
.attr("width", 5)
.attr("height", (d) => {return Math.max(height - y2(d.match), 0);})
.on("mouseover", mouseover)
function mouseover(d) {
console.log("d", d);
}
});
function type(d, i) {
d.value = +d.value;
d.t = i;
return d;
}
function check(template) {
sum = 0;
template.forEach(t => {
sum += t.value;
})
console.info("sum", sum);
}
function rangeAround(data, idx, size) {
var side = Math.floor(size/2);
var result = [];
for (var i = idx-side; i <= idx+side; i++) {
result.push(data[i]);
}
return result;
}
function normalize(values) {
var result = [];
var side = Math.floor(values.length/2);
var ave = d3.mean(values, d => d.value);
console.log("average", ave);
var j = -side;
values.forEach(d => {
var val = d.value - ave
result.push({value: val, offset: j});
j++;
})
return result;
}
function convolve(data, template) {
var side = Math.floor(template.length/2);
for (var i = 0; i < data.length; i++) {
if (i + side < data.length && i - side >= 0)
{
var sum = 0;
template.forEach(t => {
var j = i + t.offset;
sum += (data[j].value * t.value);
})
data[i]["match"] = sum;
}
}
return data;
}
</script>
</body>
https://d3js.org/d3.v4.min.js