dc.js - how to get the average of a column in data set

冷暖自知 提交于 2019-12-05 19:24:43

You need to use the group.reduce(add, remove, initial) method, like:

var col1DimTotal = col1Dim.group().reduce(reduceAdd, reduceRemove, reduceInitial);

function reduceAdd(p, v) {
  ++p.count;
  p.total += v.value;
  return p;
}

function reduceRemove(p, v) {
  --p.count;
  p.total -= v.value;
  return p;
}

function reduceInitial() {
  return {count: 0, total: 0};
}

Because you're using dc.js, you'll need to use chart.valueAccessor method to use the average in your charts, like:

chart.valueAccessor(function(p) { return p.value.count > 0 ? p.value.total / p.value.count : 0; });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!