我试图从一些支出数据中获得月平均数据。我正在使用d3.js从csv导入和汇总数据,但在继续计算平均花费之前,我似乎无法过滤掉结果数组中的值。
我是Javascript的新手,并没有任何直觉感觉。
// get filtered data (df)
d3.csv("/data/cic_data.csv", function (csv) {
// roll up and group data
var data = d3.nest()
.key(function(d) { return d.date; })
.rollup(function(d) {
return d3.sum(d, function(g) {return g.spend; });
}).entries(csv);
var valueData = data.filter(function(d) {
return d.values;
})
var sum = 0;
for( var i = 0; i < data.length; i++ ){
sum += parseInt( data[i], 10 ); //don't forget to add the base
}
var avg = sum/data.length;
console.log(avg)
以下是我的csv示例:
entity,level1,level2,supplier,type,date,spend,year,sin
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREEN SHOES ARTS COMMUNITY INTEREST COMPANY,LARGE,2014-07-01,4001.50,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREENSPACE INFORMATION FOR GREATER LONDON CIC,OTHER,2014-07-01,2400.00,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREEN SHOES ARTS COMMUNITY INTEREST COMPANY,LARGE,2014-06-01,8995.00,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREEN SHOES ARTS COMMUNITY INTEREST COMPANY,LARGE,2014-05-01,640.00,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,PUBLIC HEALTH ACTION SUPPORT TEAM CIC,SMALL,2014-05-01,6480.00,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREEN SHOES ARTS COMMUNITY INTEREST COMPANY,LARGE,2014-04-01,7286.00,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREEN SHOES ARTS COMMUNITY INTEREST COMPANY,LARGE,2014-03-01,15659.50,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,THE SMALL BUSINESS CONSULTANCY LEICESTER CIC,OTHER,2014-03-01,6000.00,2014,12
BARKING DAGENHAM LONDON BOROUGH COUNCIL,Local Government,LONDON,GREEN SHOES ARTS COMMUNITY INTEREST COMPANY,LARGE,2014-02-01,4001.50,2014,12
答案 0 :(得分:3)
您只需要csv中所有值的平均值吗?
你的巢后:
var avg = d3.sum(data, function(d){
return d.values;
}) / data.length;
或之前:
var avg = d3.sum(csv, function(d){
return +d.spend;
}) / csv.length;
或者在你的巢中避免这么多迭代:
var sum = 0;
var data = d3.nest()
.key(function(d) {
return d.date;
})
.rollup(function(d) {
return d3.sum(d, function(g) {
sum += +g.spend;
return g.spend;
});
}).entries(csv);
var avg = sum / data.length;
答案 1 :(得分:0)
以下是对您的代码的一些评论:
请注意,在嵌套csv文件后,您将获得如下数组: [{key:“2014-07-01”,值:6401.5},{key:“2014-06-01”,值:8995},...]
因此
data[0] = {key: "2014-07-01", values: 6401.5}
和
data[1] = {key: "2014-06-01", values: 8995}
这意味着您的数据数组包含对象作为元素。要访问values属性,请执行
data[0].values
另见https://github.com/mbostock/d3/wiki/Arrays。 以下是包含一些中间输出的完整代码,以了解您的数据会发生什么。
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// get filtered data (df)
d3.csv("cic_data.csv", function (sv) {
// roll up and group data
console.log("Before nesting:");
console.log(csv);
var data = d3.nest()
.key(function(d) { return d.date; })
.rollup(function(d) {
return d3.sum(d, function(g) {return g.spend; });
}).entries(csv);
console.log("After nesting:");
console.log(data);
var sum = 0;
for( var i = 0; i < data.length; i++ ){
sum += parseInt( data[i].values, 10 ); //don't forget to add the base
}
var avg = sum/data.length;
console.log(avg);
})
</script>
</body>
</html>