Post

d3js Use Time Scale

D3 js bar chart with 24 hour x axis

1
2
3
4
5
6
7
8
9
10
11
12
13
var today = new Date();
today.setHours(0, 0, 0, 0);
todayMillis = today.getTime();

data.forEach(d => {
    var parts = d.time.split(/:/);
    var timePeriodMillis = (parseInt(parts[0], 10) * 60 * 60 * 1000) +
                           (parseInt(parts[1], 10) * 60 * 1000) + 
                           (parseInt(parts[2], 10) * 1000);

    d.time = new Date();
    d.time.setTime(todayMillis + timePeriodMillis);
});
1
2
3
d3.scaleTime()
  .nice(d3.timeDay, 1)
  .range(0, 2400);
This post is licensed under CC BY 4.0 by the author.