0

我正在尝试将包含月份格式数据的 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
         }
      ]
   }
]

提前感谢帮助者!

4

1 回答 1

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

于 2013-10-26T01:42:05.583 回答