d3恢复嵌套对象

时间:2018-07-25 09:59:45

标签: javascript json d3.js

在我的代码中,我自由使用了d3.nest()函数从数据集中删除重复的行,并附加了输出样本。

现在我在JSON数组中有唯一的条目,我想对此数据集进行一些计算,特别是找到每个日期的平均“周期时间”。在此示例中,理想情况下,输出将如下所示:

[
  {
    "key": "2012-03",
    "values": [
      {
        "mean": 16,
      }
    ]
  },
  {
    "key": "2012-06",
    "values": [
      {
        "mean": 10,
      }
    ]
  },
  {
    "key": "2012-07",
    "values": [
      {
        "mean": 8,
      }
    ]
  }
]

我已经尝试过在线跟踪一些示例,但似乎缺少明显的东西,有人可以帮忙吗?

var summaryTable = [
  {
    "key": "2012-03",
    "values": [
      {
        "key": "AAA-1",
        "value": {
          "cycletime": 14
        }
      },
      {
        "key": "AAA-2",
        "value": {
          "cycletime": 18
        }
      }
    ]
  },
  {
    "key": "2012-06",
    "values": [
      {
        "key": "AAA-3",
        "value": {
          "cycletime": 8
        }
      },
      {
        "key": "AAA-4",
        "value": {
          "cycletime": 12
        }
      }
    ]
  },
  {
    "key": "2012-07",
    "values": [
      {
        "key": "AAA-5",
        "value": {
          "cycletime": 15
        }
      },
      {
        "key": "AAA-5",
        "value": {
          "cycletime": 1
        }
      },
      {
        "key": "AAA-6",
        "value": {
          "cycletime": 8
        }
      }
    ]
  }
]

var d3Table = d3.nest()
    .key(function(d) { return d['key']; })
    .rollup(function(d) { 
        return {
                "medianCycleTime": d3.mean(d, d3.values(d['values']['value'])),
        };
    })
    .entries(summaryTable);

1 个答案:

答案 0 :(得分:2)

仔细检查了您的 if (Bars.events.dragE) { // Note: xs(0) refers to pixel value at 0 on the x-scale. Must be reset for generalizability // function returns width (in pixels) of bar. Used to ensure drag doesn't exceed plot area function returnw(d, i){ if (values_function(d, i) <= 0) { Bars.elements.barController.offset(position_function(d, i), xs(0) - xs(values_function(d, i)), values_function(d, i)); return xs(0) - xs(values_function(d, i)); } else { Bars.elements.barController.offset(position_function(d, i), xs(values_function(d, i)) - xs(0), values_function(d, i)) return Math.abs(xs(0)-xs(values_function(d, i))) } } var drag = d3.behavior.drag() .on('dragstart', draginit) .on('drag', dragmove) .on('dragend', dragfin); function draginit(d){ d3.event.sourceEvent.stopPropagation(); // replace fill color with variable for user input function Bars.events.dragE(d, i, d3.select(this).attr('style', 'fill: yellow; stroke: black; stroke-width: 3px;')) } function dragmove(d){ var svgwidth = Bars.elements.chartController.plotObject.attributes.width //margin of plotObject var svginterval = svgwidth/(Bars.elements.chartController.plotObject.attributes.xmax-Bars.elements.chartController.plotObject.attributes.xmin) //number of pixels between whole numbers in coordinate plot Bars.events.dragE(d, i, d3.select(this) .attr("x", function(d, i){ if (d3.event.x < 0) {return 0} else if (d3.event.x + returnw(d,i) > svgwidth){ return (svgwidth - returnw(d,i))} else {return xs(Math.floor(d3.event.x/svginterval)) } })); //this function ensures value will be a whole number var valuechange = d3.event.x < 0 ? 0 : (d3.event.x + returnw(d,i) > svgwidth ? svgwidth - returnw(d,i) : d3.event.x); console.log("Start value changed to: ", Math.floor(valuechange/svginterval)); } function dragfin(d){ Bars.events.dragE(d, i, d3.select(this).attr('style', 'stroke: black; stroke-width: 0px;') .attr('fill', Bars.attributes.fill)) } rects.attr("cursor", "ew-resize") .on('dragstart', function(d, i){ Bars.events.dragE(d, i, d3.select(this).call(drag)) }) .on('drag', function (d, i) { window.dragThis = this; Bars.events.dragE(d, i, d3.select(this).call(drag)) }) .on("dragend", function(d, i){ Bars.events.dragE(d, i, d3.select(this).call(drag)) }) } 数据后,我发现该函数可以在d3.nest中使用。

rollup的参数d是一个数组,其中包含所有具有该键的对象。您必须先对其进行索引,否则您将获得rollup类的values()方法。

Array