d3: Plot as a cumulative graph

僤鯓⒐⒋嵵緔 提交于 2019-12-24 05:07:35

问题


Does d3 have a built in method to plot a data set as a cumulative graph?

For example, if the y values are: [2, 4, 2, 2], I want them to actually be plotted as: [2, 6, 8, 10]. Does d3 have a way to do this or would I have to traverse the data set and do this manually?


回答1:


You can check https://github.com/mbostock/d3/wiki/Arrays for more info but I think you can use the reduce() function here.

i.e:

[0, 2, 4, 2, 2].reduce(function(previousValue, currentValue, currentIndex, array) {
  console.log(previousValue + currentValue);//2,6,8,10
  return previousValue + currentValue;
});


来源:https://stackoverflow.com/questions/36706870/d3-plot-as-a-cumulative-graph

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