Javascript highcharts 日期时间 x 轴自定义格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7762477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:30:44  来源:igfitidea点击:

highcharts datetime x-axis custom formatting

javascriptruby-on-railshighcharts

提问by butterywombat

I would like to have just the first letter of each weekday as the x-axis, ie M T W T F S S repeated. Currently you can set dateTimeLabelFormats, which I've set to use %a, which is the dateFormat for short weekday (Mon Tues Wed etc). How can I just use the first letter?

我只想将每个工作日的第一个字母作为 x 轴,即重复 MTWTFSS。目前您可以设置dateTimeLabelFormats,我已经设置为使用%a,这是短工作日(星期一星期二星期三等)的日期格式。我怎样才能只使用第一个字母?

Here's my code (I'm using Lazy Highcharts in rails)

这是我的代码(我在 Rails 中使用 Lazy Highcharts)

 f.xAxis({type: 'datetime', tickInterval: 24*3600*1000, dateTimeLabelFormats: {
        day: '%a',
        week: '%a'}
        })

Thanks.

谢谢。

回答by Bhesh Gurung

In the label -> formatter for xAxis, use dateFormat function to get the month and then use the substring function to get the first letter and return that letter as follows -

在标签 -> xAxis 的格式化程序中,使用 dateFormat 函数获取月份,然后使用 substring 函数获取第一个字母并返回该字母,如下所示 -

xAxis: {        
    type: 'datetime',
    labels: {
        formatter: function() {
            var monthStr = Highcharts.dateFormat('%b', this.value);
            var firstLetter = monthStr.substring(0, 1);
            return firstLetter;
        }
    }
},

See it on jsfiddle

在 jsfiddle 上查看