我正在尝试将带有数月格式数据的csv转换为四分之一格式(平均四分之一,而不是总和......)
我需要的输出是一个嵌套数组,如下所示几个月,仅适用于季度。我已经使用d3.nest函数计算了每个季度的每个季度,但我似乎无法将其转换为季度。
这是带有数据的csv:
x,January,February,March,April,May,June,July
Overall,3635,3371,3474,4212,4815,5544,4464
A,1689,1489,1588,2231,2419,2889,2304
B,316,266,267,323,337,354,435
C,622,537,629,614,766,720,616
以下是嵌套数据的代码:
nest = d3.nest().key(function(d) { return d.x; })
.rollup(function(leaves) { var a = []; d3.entries(leaves[0]).forEach(function (k) { if (k.key != "x") a.push({x: k.key, y: k.value, month: k.key, quater: (Math.floor((new Date(Date.parse(k.key + "1, 2013")).getMonth()+1)/3+1))}); }); return a; });
data = nest.entries(data);
导致(规定适合......):
[
{
"key":"Overall",
"values":[
{
"x":"January",
"y":"3635",
"month":"January",
"quater":1
},
{
"x":"February",
"y":"3371",
"month":"February",
"quater":1
},
{
"x":"March",
"y":"3474",
"month":"March",
"quater":2
},
{
"x":"April",
"y":"4212",
"month":"April",
"quater":2
},
{
"x":"May",
"y":"4815",
"month":"May",
"quater":2
},
{
"x":"June",
"y":"5544",
"month":"June",
"quater":3
},
{
"x":"July",
"y":"4464",
"month":"July",
"quater":3
}
]
},
{
"key":"A",
"values":[
{
"x":"January",
"y":"1689",
"month":"January",
"quater":1
},
{
"x":"February",
"y":"1489",
"month":"February",
"quater":1
},
{
"x":"March",
"y":"1588",
"month":"March",
"quater":2
},
{
"x":"April",
"y":"2231",
"month":"April",
"quater":2
},
{
"x":"May",
"y":"2419",
"month":"May",
"quater":2
},
{
"x":"June",
"y":"2889",
"month":"June",
"quater":3
},
{
"x":"July",
"y":"2304",
"month":"July",
"quater":3
}
]
},
{
"key":"B",
"values":[
{
"x":"January",
"y":"316",
"month":"January",
"quater":1
},
{
"x":"February",
"y":"266",
"month":"February",
"quater":1
},
{
"x":"March",
"y":"267",
"month":"March",
"quater":2
},
{
"x":"April",
"y":"323",
"month":"April",
"quater":2
},
{
"x":"May",
"y":"337",
"month":"May",
"quater":2
},
{
"x":"June",
"y":"354",
"month":"June",
"quater":3
},
{
"x":"July",
"y":"435",
"month":"July",
"quater":3
}
]
},
{
"key":"C",
"values":[
{
"x":"January",
"y":"622",
"month":"January",
"quater":1
},
{
"x":"February",
"y":"537",
"month":"February",
"quater":1
},
{
"x":"March",
"y":"629",
"month":"March",
"quater":2
},
{
"x":"April",
"y":"614",
"month":"April",
"quater":2
},
{
"x":"May",
"y":"766",
"month":"May",
"quater":2
},
{
"x":"June",
"y":"720",
"month":"June",
"quater":3
},
{
"x":"July",
"y":"616",
"month":"July",
"quater":3
}
]
}
]
提前感谢帮助者!
答案 0 :(得分:3)
您是否使用d3.csv加载CSV?如果是这样,您可以在没有d3.nest()的情况下完成当前的数据转换。然后d3.nest()可用于按季度汇总。这是一个例子:
d3.csv('data.csv', function(row) {
var key = row.x;
var entries = d3.entries(row);
// remove the first (the x column)
entries.shift();
// map the rest of the columns
entries = entries.map(function(entry) {
return {
value: entry.value,
month: entry.key,
quarter: (Math.floor((new Date(Date.parse(entry.key + "1, 2013")).getMonth()+1)/3+1))
};
});
// nest the array of months by quarter, rollup to return the mean
var quarters = d3.nest()
.key(function(d) { return +d.quarter; })
.rollup(function(d) { return d3.mean(d, function(d){ return +d.value; })})
.entries(entries);
var months = d3.nest()
.key(function(d) { return d.month; })
.rollup(function(d) { return d3.sum(d, function(d) { return +d.value; })})
.entries(entries);
// return object for this row
return {
key: row.x,
months: months,
quarters: quarters
};
}, function(data) {
/* at this point data is an array of objects like:
{
key: 'Overall',
months: [{key: January, values: 123} ....],
quarters: [{key: 1, values: 4322}, ....]
}
*/
console.log(data);
});
<强>更新强> 这将返回一个数组,每个条目都有一个月和一个四分之一数组。在可视化代码中,您可以动态地从适当的月份或季度属性中提取。
此处的Plunker:http://plnkr.co/edit/sVt0T5LrTFJkMiVpkk8F?p=preview