格式和解析时间D3V4

时间:2018-02-26 23:23:58

标签: d3.js

尝试使用d3v4再次进步。

基于我的研究,我认为我的问题的解决方案来自我之前遇到的问题的答案:“......然后这不起作用的原因是nest.key必须是一个字符串,所以它将你的时间解析回一个字符串,然后不能用你的x音阶。“尽管有线索,但我无法完成这项工作。结果:

错误:属性d:预期数字,“MNaN,262.5LNaN,37 ......”。

我的目标是画一条线来计算按日期分组的事件数。当我解析creationDate时,我能够实现这个结果。尽管如此,我希望结果按天计算,而不是按小时计算。为此,我使用var formatTime = d3.timeFormat(“%Y-%m-%d”)格式化日期;但后来我得到了错误。

我已经看到很多基于D3v3的问题的答案,我试图改编它...... :(

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 2px;
}

</style>
<body>

<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// parse the date / time
var formatTime = d3.timeFormat("%Y-%m-%d");
var parseTime = d3.timeParse("%Y-%m-%dT%H:%M:%S.%L");

// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);


// define the 3rd line
var valueline3 = d3.line()
        .x(function(d) { return x(d.key); })
        .y(function(d) { return y(d.length); });


// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform",
          "translate(" + margin.left + "," + margin.top + ")");

// Get the data
d3.json("data5.json", function(error, data) {
  if (error) throw error;

  var groupByDate = d3.nest(data)
    .key(function(d) { return formatTime(parseTime(d.creationDate)); })
    .sortKeys(d3.ascending)
    .rollup(function(v) {
      return {
              count:    v.length,
};
})
    .entries(data);
        console.log(groupByDate);

        // format the data
        groupByDate.forEach(function(d) {
          d.creationDate = d.key;
            d.length = +d.value.count;
            d.value.count = +d.length;
        });


  // Scale the range of the data
  x.domain(d3.extent(groupByDate, function(d) { return d.key; }));
  y.domain([0, d3.max(groupByDate, function(d) {    return Math.max(d.length); })]);

// Add the valueline3 path.
      svg.append("path")
          .datum(groupByDate)
          .attr("class", "line")
          .style("stroke", "green")
          .attr("d", valueline3);

  // Add the X Axis
  svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  // Add the Y Axis
  svg.append("g")
      .call(d3.axisLeft(y));

});

</script>
</body>

我在同一目录中的JSON数据:

[
  {
    "id": 115,
    "creationDate": "2017-12-08T16:31:09.924"
  },
  {
    "id": 119,
    "creationDate": "2017-12-08T16:31:09.924"
  },
  {
    "id": 120,
    "creationDate": "2017-12-08T16:31:09.924"
  },
  {
    "id": 134,
    "creationDate": "2017-12-08T16:31:09.924"
  },
  {
    "id": 135,
    "creationDate": "2017-12-08T16:31:09.924"
  },
  {
    "id": 139,
    "creationDate": "2018-02-05T17:17:04.473"
  },
  {
    "id": 139,
    "creationDate": "2018-02-05T17:17:04.473"
  },
  {
    "id": 140,
    "creationDate": "2018-02-04T17:17:08.707"
  }
]

0 个答案:

没有答案