score:2

Accepted answer

Rather than plotting the height of the closest point, try plotting the average height of the points either side of your bar.

You can get even closer if you work out a linear best fit for your bar on the line between the points either side. Something like:

function getY(xVal,points) {

var higher = 0;
var lower=0;
for(var i=0;i<points.length;i++){
    if (points[i][0] > xVal) {
        higher=i;
        lower=i-1;
        break;
    }
}

return points[lower][1] + ((points[higher][1]-points[lower][1])*(xVal-points[lower][0])/(points[higher][0]-points[lower][0]));
};

http://jsfiddle.net/5h471o3d/


Related Query

More Query from same tag