Show average of dygraph points, which dynamically amends as the user zooms

安稳与你 提交于 2020-01-17 04:56:06

问题


I have seen this - Horizontal line to show average in dygraph - and it doesn't quite answer my question.

I have the following simple dygraph:

g2 = new Dygraph(
  document.getElementById("graphdiv2"),
  "../newDataFile.csv",
  { ... }
);

where newDataFile.csv has two columns.

What I would like to show is the average of the points plotted (as a line or just a number), and for this average to change as I zoom in to different parts of the graph. Any ideas?


回答1:


var idx=1; //change this value if you have several plots.
var data = g2.rawData_;
var sum = data.map(function(e){return e[idx]})
              .reduce(function(a,b){return a+b});
var average = sum/data.length;

If you want to keep only values displayed if the graph is zoomed :

var idx=1; //change this value if you have several plots.
var data = g2.rawData_.slice(g2.boundaryIds_[idx][0],g2.boundaryIds_[idx][1]+1);
var sum = data.map(function(e){return e[idx]})
              .reduce(function(a,b){return a+b});
var average = sum/data.length;


来源:https://stackoverflow.com/questions/27918591/show-average-of-dygraph-points-which-dynamically-amends-as-the-user-zooms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!