简体   繁体   English

D3多折线图-多个数组,解析时间戳

[英]D3 Multi Line Chart- Multiple Arrays, parsing Timestamps

So I'm using d3, v4. 所以我正在使用d3,v4。 Trying to graph multiple lines individually, by date (x) and value (y). 尝试按日期(x)和值(y)分别绘制多条线。 The problem is I cannot parse the date after doing it once for the first line. 问题是在第一行执行一次日期后,我无法解析日期。 What can I do differently (for the first function in particular) to make it work for both? 我可以做些什么(特别是对于第一个功能)以使其对两者都起作用?

Here is the code pen for the entire project (where I've included multiple arrays of data, but am only in the process of graphing the first two. Sorry for the messiness. The lines below can be found starting line 314) Important pieces: 这是整个项目的代码笔(我已经包含了多个数据数组,但仅处于绘制前两个数据的过程中。很抱歉造成混乱。下面的几行可以从第314行开始)重要部分:

function type(data) {
   data[0].forEach(function(d) {
   d.inspected_at =  parseTime(d.inspected_at);
   console.log(d)
   return d;
   })
};

// define the line
var line1 = d3.line()
    .x(function(d) { return x(d.inspected_at); })
    .y(function(d) { return y(d.flow_data);});
var line2 = d3.line()
    .x(function(d) { return x(d.inspected_at); })
    .y(function(d) { return y(d.flow_data);});

// Add the line path(s)
//line CB-01
svg.append('g')
  .attr('clip-path', 'url(#clipper)') 
  .selectAll('path#line').data([data[0]])
  .enter().append("path") 
  .attr("class", "line")
  .attr("id", "downstreamLine")
  .attr("d", line1)
  .attr("stroke", "blue");  
  //line CB-02
svg.append('g')
  .attr('clip-path', 'url(#clipper)') 
  .selectAll('path#line').data([data[1]])
  .enter().append("path") 
  .attr("class", "line")
  // .attr("id", "downstreamLine")
  .attr("d", line2)
  .attr("stroke", "green");

Here is what my data looks like: 这是我的数据:

var data = [{"id":"CB-01","inspected_at":"2018-04-08T12:44:30.000Z","flow_data":"4"},
{"id":"CB-01","inspected_at":"2018-04-08T12:44:30.000Z","flow_data":"5"},
{"id":"CB-01","inspected_at":"2018-04-08T12:54:36.000Z","flow_data":"4"},
{"id":"CB-01","inspected_at":"2018-04-08T12:54:36.000Z","flow_data":"7"},
{"id":"CB-01","inspected_at":"2018-04-08T12:54:36.000Z","flow_data":"2"},
{"id":"CB-01","inspected_at":"2018-04-08T13:04:42.000Z","flow_data":"3"},
{"id":"CB-01","inspected_at":"2018-04-08T13:04:42.000Z","flow_data":"9"}],


[{"id":"CB-02","inspected_at":"2018-04-08T12:44:30.000Z","flow_data":"3"},
{"id":"CB-02","inspected_at":"2018-04-08T12:44:30.000Z","flow_data":"2"},
{"id":"CB-02","inspected_at":"2018-04-08T12:54:36.000Z","flow_data":"3"},
{"id":"CB-02","inspected_at":"2018-04-08T12:54:36.000Z","flow_data":"5"},
{"id":"CB-02","inspected_at":"2018-04-08T12:54:36.000Z","flow_data":"6"},
{"id":"CB-02","inspected_at":"2018-04-08T13:04:42.000Z","flow_data":"8"},
{"id":"CB-02","inspected_at":"2018-04-08T13:04:42.000Z","flow_data":"7"}]

The first array being one line, the second array is the next. 第一个数组是一行,第二个数组是下一行。

Thanks in advance! 提前致谢! Let me know if I can provide any more info. 让我知道是否可以提供更多信息。

This should work for any amount of lines, iterate every line array and do the same process for each point in that line array: 这应该适用于任何数量的线,迭代每个线阵列,并对该线阵列中的每个点执行相同的过程:

function type(data) {
  data.forEach(arr => arr.forEach(d => d.inspected_at = parseTime(d.inspected_at)))
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM