undefined
undefined

Reputation: 6844

JS d3 scale time doesn't produce the correct ticks

I want to generate ticks based on a start and end time. I'm using the scaleTime function from d3, passing the domain and the range.

const scale = scaleTime().domain([1625733909077, 1625733911965]).range([0, 1317]);

When I log scale.ticks() I'm getting this output: enter image description here

This seems to be correct because it takes something like 2.5 seconds.

But when I try to extract the ms value from each:

console.log(ticks.map((v) => v.getMilliseconds()).sort());

I'm getting this output:

[0, 0, 200, 200, 200, 400, 400, 400, 600, 600, 600, 800, 800, 800]

Which is not what I expect. The result I expect in this case is:

[0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600]

What am I doing wrong?

Upvotes: 0

Views: 110

Answers (1)

SmokeyShakers
SmokeyShakers

Reputation: 3412

I think it's working as intended. getMilliseconds won't return something 1000 or over. Those are seconds. The function does not convert seconds to milliseconds.

Based on your comments, it looks like you want an array of the number of milliseconds from your scale's domain minimum.

ticks.map(x =>  x - ticks[0])

Upvotes: 1

Related Questions