How to format date and time in Dygraphs legend according to user locale

て烟熏妆下的殇ゞ 提交于 2020-01-23 12:28:44

问题


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:

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/32148482/how-to-format-date-and-time-in-dygraphs-legend-according-to-user-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!