Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js "Error: Invalid value for <path> attribute" for moving average

I am trying to create a moving average over the rest of the chart. I am trying to do something similar to this.
However, for my data points I continuously get the error:

"Error: Invalid value for attribute d="M1.2121212121212122,NaNL1.4141414141414141,NaNC1.6161616161616161,NaN,2.0202020202020203,NaN,2.4242424..."

I believe it is because my data isn't formatted correctly, but I don't know how to modify the function to work with my data. My data currently looks like:

var data = [ {"x": 1, "y": 113},
{"x": 6, "y": 38},
{"x": 11, "y": 108},
{"x": 16, "y": 245},
{"x": 21, "y": 155},
{"x": 26, "y": 234},
...

Thanks

like image 314
surfaspen Avatar asked Feb 11 '23 06:02

surfaspen


2 Answers

Just map your [{x1,y1}, {x2,y2}, ... {xN,yN}] pairs to a [y1, y2, ... yN] series, which is what that moving average function expects:

movingAverageLine(data.map(function(d) { return d.y; }))
like image 183
meetamit Avatar answered Feb 12 '23 20:02

meetamit


Here's a working fiddle: http://jsfiddle.net/yz87b53d/3/

You should accept meetamit's answer since that essentially was the issue.

movingAverageLine(data.map(function(d) { return d.y; }))
like image 24
ekuusela Avatar answered Feb 12 '23 19:02

ekuusela