how to format time on xAxis use d3.js

后端 未结 5 1725
攒了一身酷
攒了一身酷 2020-12-04 15:38

According the demo on http://bl.ocks.org/mbostock/3883245

I don\'t know how format time on xAxis

this is my code : js:


    var data = [{
                 


        
5条回答
  •  庸人自扰
    2020-12-04 16:14

    The accepted answer is indeed correct, but in my case, I needed the flexibility for the formats to adjust to different scales (think zooming), but also to ensure the 24hr clock is used. The key is to define a multi-resolution time format. See the Documentation page for details.

    My code:

    var axisTimeFormat = d3.time.format.multi([
        [".%L", function(d) { return d.getMilliseconds(); }],
        [":%S", function(d) { return d.getSeconds(); }],
        ["%H:%M", function(d) { return d.getMinutes(); }],
        ["%H:%M", function(d) { return d.getHours(); }],
        ["%a %d", function(d) { return d.getDay() && d.getDate() != 1; }],
        ["%b %d", function(d) { return d.getDate() != 1; }],
        ["%B", function(d) { return d.getMonth(); }],
        ["%Y", function() { return true; }]
     ]);
    
    var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .tickFormat(axisTimeFormat);
    

提交回复
热议问题