d3js v4 - 刷精度,如何获得毫秒

时间:2018-01-19 13:31:23

标签: javascript date d3.js brush

如何以毫秒为单位获取所选日期?

我想要ISO 8601 UTC格式(%Y-%m-%dT%H:%M:%S。%LZ)

let e = d3.event.selection.map(x3.invert);
let s = d3.event.selection || x3.range();
console.log(e[0], e[1]); 
console.log(s[0], s[1]);

// Sun Jul 23 2017 13:32:33 GMT-0300 (-03) Fri Oct 13 2017 08:27:54 GMT-0300 (-03)
// 1037.59375 1040.59375

current format

我正在使用timeParse函数生成线条和点:

parseDate = d3.timeParse("%Y-%m-%dT%H:%M:%S.%LZ");

来自数据对象:

lines: [[[1467341999153,0,5],5,10,15,20,25,30,35,40,45,50,60,70,...
// The first point of each line is an array: 
// [unix epoch (milliseconds), Y Value, interval (milliseconds) between each point of the line]

从数据对象

格式化millis到ISOString的功能
let calcDate = function(baseDate, frequencyDate, dateMultiplier) {
     let _ms = baseDate+(frequencyDate*dateMultiplier);
     let _date = new Date(_ms);
     return _date.toISOString()
}


let lineFunction = d3.line()
    .defined(function(d) { return d; })
    .x(function(d,i) {
          if (Array.isArray(d)) {
               _dateMultiplier = 0;
               _frequencyDate = d[2];
               _baseDate = d[0];
               return x(parseDate(calcDate(_baseDate, _frequencyDate, _dateMultiplier)))
          } else {
               _dateMultiplier++;
               return x(parseDate(calcDate(_baseDate, _frequencyDate, _dateMultiplier)))
          };
    })

谢谢!

1 个答案:

答案 0 :(得分:1)

解决了!这很简单!只需使用.toISOString()

进行隐蔽

console.log(e [0] .toISOString(),e [1] .toISOString());

由于