d3:绘制为累积图 [英] d3: Plot as a cumulative graph
本文介绍了d3:绘制为累积图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
d3 是否有内置方法将数据集绘制为累积图?
Does d3 have a built in method to plot a data set as a cumulative graph?
例如,如果y值是: [2,4,2,2]
,我想让它们实际上绘制为: [2,6,8,10]
。 d3有一个方法可以做到这一点,或者我必须遍历数据集并手动执行吗?
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?
推荐答案
https://github.com/mbostock/d3/wiki/Arrays 了解详情,但我想你可以在这里使用reduce()函数。
You can check https://github.com/mbostock/d3/wiki/Arrays for more info but I think you can use the reduce() function here.
ie:
[0, 2, 4, 2, 2].reduce(function(previousValue, currentValue, currentIndex, array) {
console.log(previousValue + currentValue);//2,6,8,10
return previousValue + currentValue;
});
这篇关于d3:绘制为累积图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文