【问题标题】:d3: Plot as a cumulative graphd3:绘制为累积图
【发布时间】:2016-04-19 01:19:45
【问题描述】:

d3 是否具有将数据集绘制为累积图的内置方法?

例如,如果 y 值为:[2, 4, 2, 2],我希望它们实际绘制为:[2, 6, 8, 10]。 d3 是否有办法做到这一点,还是我必须遍历数据集并手动执行此操作?

【问题讨论】:

    标签: d3.js graph cumulative-line-chart


    【解决方案1】:

    您可以查看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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2014-11-20
      • 2013-06-10
      • 2017-01-10
      相关资源
      最近更新 更多