Sunday, 15 July 2012

javascript - How do I produce an interpolation function given n data points? -



javascript - How do I produce an interpolation function given n data points? -

i faced problem of trying interpolate values between points on series plot i.e. info looks next (please assume random x,y coordinates)

[[x0,y0], [1,1] ,[2,2], [2,3],.....[x,y]]

and interpolator, give 1.5 , interpolator function should homecoming 1.5 case. in other cases info random, should find best fit given set of points , homecoming y value given x value

is possible using d3 interpolate*** functions?

thanks

although d3 interpolators, easier utilize muli-part linear scale.

usually, linear scales have two-value domain , two-value range, , other values calculated straight line between start , end points of domain , range. however, can set both domain , range array of many values (so long both arrays same length), , scale deed series of straight-line relationships each section of domain.

in other words, if utilize array of x-values scale's domain, , array of y-values range, can input x value , scale homecoming linear interpolation between adjacent y values. values outside points, extrapolate initial or final linear relationship:

class="snippet-code-js lang-js prettyprint-override">var points = [ [0,10], [1,32], [2,14], [3,15] ]; var multiline = d3.scale.linear() .domain( points.map(function(p){return p[0];}) ) .range ( points.map(function(p){return p[1];}) ); document.body.innerhtml = "line @ 0.0: " + multiline(0) + "<br/>" + "line @ 1.0: " + multiline(1) + "<br/>" + "line @ 1.5: " + multiline(1.5) + "<br/>" + "line @ 2.3: " + multiline(2.3) + "<br/>" + "line @ 3.0: " + multiline(3) + "<br/>" + "line @ 4.0: " + multiline(4) ; class="snippet-code-html lang-html prettyprint-override"><script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

note you'll need create sure points sorted x-value in order work expected.

javascript svg d3.js

No comments:

Post a Comment