Highstock - display number of week

前端 未结 3 658
盖世英雄少女心
盖世英雄少女心 2021-01-22 17:23

How can I show the week number for the date in Highstock (not Highcharts!)?

My SQL looks like this

select unix_timestamp(date)*1000 week         


        
3条回答
  •  孤街浪徒
    2021-01-22 18:04

    To keep weeks and years consistent, I tend to use ISO-8601 standard when dealing with week numbering.

    With Pawel answer and the js code from this blog, I set up the following solution.

    Highcharts.dateFormats = {
            V: function (timestamp) {    
                var target  = new Date(timestamp);  
                var dayNr   = (target.getDay() + 6) % 7;  
                target.setDate(target.getDate() - dayNr + 3);  
                var firstThursday = target.valueOf();  
                target.setMonth(0, 1);  
                if (target.getDay() != 4) {  
                    target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);  
                }  
                return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000  
            }  ,
            G: function(timestamp) {
                var target  = new Date(timestamp);  
                target.setDate(target.getDate() - ((target.getDay() + 6) % 7) + 3);  
                return target.getFullYear();  
            }
    };
    

    %V and %G respects the strftime format used in highstock.

提交回复
热议问题