Pregunta

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.

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?

¿Fue útil?

Solución

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)]);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top