【发布时间】:2016-04-19 01:19:45
【问题描述】:
d3 是否具有将数据集绘制为累积图的内置方法?
例如,如果 y 值为:[2, 4, 2, 2],我希望它们实际绘制为:[2, 6, 8, 10]。 d3 是否有办法做到这一点,还是我必须遍历数据集并手动执行此操作?
【问题讨论】:
标签: d3.js graph cumulative-line-chart
d3 是否具有将数据集绘制为累积图的内置方法?
例如,如果 y 值为:[2, 4, 2, 2],我希望它们实际绘制为:[2, 6, 8, 10]。 d3 是否有办法做到这一点,还是我必须遍历数据集并手动执行此操作?
【问题讨论】:
标签: d3.js graph cumulative-line-chart
您可以查看https://github.com/mbostock/d3/wiki/Arrays 了解更多信息,但我认为您可以在此处使用 reduce() 函数。
即:
[0, 2, 4, 2, 2].reduce(function(previousValue, currentValue, currentIndex, array) {
console.log(previousValue + currentValue);//2,6,8,10
return previousValue + currentValue;
});
【讨论】: