Chartjs-Creating a heart rate monitor

2👍

You could use 2 series and just swap in the points. You might need to interpolate between points to smoothen out the animation.

I’ve used linear interpolation, but for a smoother curve you could probably use something else, but I believe if you have actual data, linear interpolation would work fine.

function getActualData() {
    var actualData = []
    for (var m = 0; m < 20; m++)
        actualData.push(45 + parseInt(Math.random() * 35))
    return actualData;
}


var ANIMATIONSTEPS = 200;

var myLineChart;
var labels;
var animationStep;
setInterval(function () {
    if (myLineChart === undefined) {
        var actualData = getActualData();

        // if we have only a few data points interpolate to fill out enough points to make the animation smooth
        var interpolationSteps = Math.ceil(ANIMATIONSTEPS / actualData.length);
        labels = []
        var data = []
        var blankData = []
        for (var i = 0; i < (actualData.length - 1); i++) {
            labels.push('')
            data.push(actualData[i])
            blankData.push(null)

            // push interpolation
            var difference = actualData[i + 1] - actualData[i];
            var interpolationStep = 1 / interpolationSteps;
            for (var j = 1; j < interpolationSteps; j++) {
                labels.push('')
                data.push(actualData[i] + difference * Chart.helpers.easingEffects["linear"](j * interpolationStep));
                blankData.push(null)
            }
        }
        labels.push('')
        data.push(actualData[i])
        blankData.push(null)

        var data = {
            labels: labels,
            datasets: [
                {
                    strokeColor: "rgba(243, 118, 27, 1)",
                    data: blankData
                },
                {
                    strokeColor: "transparent",
                    data: data
                }
            ]
        };

        var ctx = document.getElementById("myChart").getContext("2d");
        myLineChart = new Chart(ctx).Line(data, {
            animation: false,
            datasetFill: false,
            pointDot: false,
            datasetStrokeWidth: 5,
            showTooltips: false,
            scaleOverride: true,
            scaleSteps: 12,
            scaleStepWidth: 5,
            scaleStartValue: 30,
            scaleShowVerticalLines: false,
            scaleShowLabels: false,
        });

        animationStep = 0;
    }

    // the actual animation
    myLineChart.datasets[0].points[animationStep].value = myLineChart.datasets[1].points[animationStep].value
    myLineChart.update();
    animationStep++;

    // start new cycle
    if (animationStep >= labels.length) {
        myLineChart.destroy();
        myLineChart = undefined;
    }
}, 10)

Fiddle – http://jsfiddle.net/t795k4j3/

0👍

An EKG is a graph of the electrical activity in the heart over a period of time. It is not your heart rate (however an EKG can show your heart rate because it shows an increase in electrical activity at certain points over time giving a average heart rate).

So I don’t believe it is even possible for you to use the heart rate in order to generate a graph similar to an EKG.

Leave a comment