我正在尝试将包含月份格式数据的 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
}
]
}
]
提前感谢帮助者!