Arash Saidi
Arash Saidi

Reputation: 2238

Creating a heart rate monitor

I have been trying to create a heart rate monitor with chart.js, but I'm at odds as how to represent the graph.

I want to build something along the lines of this demonstration: link to demo.

The charts in chart.js are made like this:

var data = {
    labels: ["Heart Rate Monitor"],
    datasets: [
        {
            label: "heart rate",
            data: [65, 59, 80, 81, 56, 55, 40]
        }
    ]
};

var myLineChart = new Chart(ctx).Line(data, {});

And we can update the data like this:

myLineChart.datasets[0].points[0].value = 50;

Which would set the first value in the array: 65, to 50.

So I have made some attempts at creating an array with values that look like the demo, and move the first element to last, and every other element one down (-1) on the array and fill the chart with the values. This works, to a certain extent, but I am not able to change the speed of the values, which I need to do.

Suggestions?

Upvotes: 0

Views: 4222

Answers (2)

potatopeelings
potatopeelings

Reputation: 41075

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/

Upvotes: 2

honerlawd
honerlawd

Reputation: 1509

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.

Upvotes: 0

Related Questions