I have set up an example on jsfiddle. http://jsfiddle.net/tjjjohnson/XXFrg/2/#run推荐答案先前的解决方案会导致累积移动平均值.A previous solution results in a cumulative moving average. 我修改了/users/2255689/john-oconnor>约翰·奥康纳来提供 n 移动平均值,方法是将自定义插值函数传递给d3.svg.line():I modified the fiddle made by John O'Connor to provide an n-moving average by passing a custom interpolation function to d3.svg.line():movingAvg = function(n) { return function (points) { points = points.map(function(each, index, array) { var to = index + n - 1; var subSeq, sum; if (to < points.length) { subSeq = array.slice(index, to + 1); sum = subSeq.reduce(function(a,b) { return [a[0] + b[0], a[1] + b[1]]; }); return sum.map(function(each) { return each / n; }); } return undefined; }); points = points.filter(function(each) { return typeof each !== 'undefined' }) // Note that one could re-interpolate the points // to form a basis curve (I think...) return points.join("L"); }}var movingAverageLine = d3.svg.line() .x(function(d,i) { return x(i); }) .y(function(d,i) { return y(d); }) .interpolate(movingAvg(6)); 这篇关于在d3.js中绘制滚动/移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 09:35