如何根据用户区域设置在Dygraphs图例中格式化日期和时间 [英] How to format date and time in Dygraphs legend according to user locale

查看:155
本文介绍了如何根据用户区域设置在Dygraphs图例中格式化日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形,显示一段时间内设备的功率输出,我想格式化图例,以便它以当前用户所在的位置样式显示时间戳(例如,在美国,它将显示MM/DD/YY h:m:s am/pm,而在欧盟则显示DD/MM/YYYY HH:MM:SS,依此类推).

I have a graph that displays a device's power output over time and I'd like to format the legend so that it displays the timestamps in the style of where the current user is (for example, in the US it would show MM/DD/YY h:m:s am/pm, and in EU would show DD/MM/YYYY HH:MM:SS, etc.).

当前,它的默认设置如下:

Currently, it's set at default, as follows:

我为x轴传递了一个JS Date()对象,我发现您可以修改x轴标签,但是当鼠标悬停在图表上时,我找不到有关更改图例格式的任何信息.

I pass in a JS Date() object for the x-axis and I've found that you can modify the x-axis labels, but I can't find any info on changing the format of the legend when hovering over the graph.

推荐答案

您已经发现 axisLabelFormatter 选项,可让您控制x轴上标签的格式.

You've discovered the axisLabelFormatter option, which lets you control formatting of labels on the x-axis.

要控制图例中标签的格式,您需要使用 valueFormatter

To control formatting of labels in the legend, you need to use valueFormatter

g = new Dygraph(div, data,
    axes: {
        x: {
            axisLabelFormatter: function (d, gran) {
                return d.toLocaleDateString();
            },
            valueFormatter: function (ms) {
                return new Date(ms).toLocaleDateString();
            }
        }
    }
});

有关完整示例,请参见此小提琴.自时代以来,一个格式化程序函数使用日期,而另一个使用毫秒则是奇怪的吗?是的.不幸的是,这是我们所坚持的不一致之处.

See this fiddle for a fully-worked example. Is it odd that one formatter function takes a Date whereas the other takes millis since epoch? Yes. Unfortunately, it's an inconsistency we're stuck with.

这篇关于如何根据用户区域设置在Dygraphs图例中格式化日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆