简体   繁体   English

数组索引的D3时间刻度

[英]D3 time scale from array index

I am attempting to map an array index to a time range in d3.js as I do not have individual dates for the array - I just know the values are between 1900 and 2000. 我正在尝试将数组索引映射到d3.js中的时间范围,因为我没有数组的单独日期-我只知道值在1900到2000之间。

var data = [45,678,490...]; // length 820

var x = d3.time.scale().range([0, width]).domain([new Date(1900, 0, 0), new Date(2000, 0, 0)]);

// line function
var line = d3.svg.line()
    .x(function(d,i) { 
    // here map i to date between domain min / max    
        return x(i); 
    })
    .y(function(d) { 
        return y(d); 
    });

Is there a smarter way of doing this than manually calculating a date for each array value? 有比手动计算每个数组值的日期更聪明的方法吗? Perhaps calculating the step and incrementing? 也许计算步长并递增?

Use another scale to map from the index value to date: 使用其他比例从索引值映射到日期:

var arrayScale = d3.time.scale()
                    .domain([0, data.length])
                    .range([new Date(1900, 0, 0), new Date(2000, 0, 0)]);

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

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