d3中的自定义多尺度时间格式 [英] Custom multi-scale time formats in d3

查看:96
本文介绍了d3中的自定义多尺度时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

d3.time.scale 有一个整洁的时间格式化程序,使用多尺度时间格式。是否有API或其他方式(尚未)指定多尺度时间格式



我知道可以创建一个新的 d3.time.format ,但不会是多



感谢。

解决方案

多尺度时间格式未公开为 d3.time所返回的标准API之外的公共API .scale tickFormat 方法。 / p>

也就是说,实现本身是相当简单,所以你可以创建自己的多尺度时间格式没有很多工作。多尺度时间格式仅仅是时间格式的有序数组,每个其中具有相关的测试功能。返回true的第一个测试函数确定使用哪种时间格式。



d3.time.scale使用的时间格式具有以下格式(取自 time / scale.js ),反向指定order:

  [。%L,function(d){return d.getMilliseconds(); }],
[:%S,function(d){return d.getSeconds(); }],
[%I:%M,function(d){return d.getMinutes(); }],
[%I%p,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,d3_true]

时间具有非零毫秒字段,则使用。%L格式;否则,如果它具有非零秒字段,则使用:%S;等等。


d3.time.scale has a neat time formatter which uses multi-scale time formats. Is there an API or other way (yet) to specify a multi-scale time formats?

I know it is possible to create a new d3.time.format, but that won't be multi-scale.

Thanks.

解决方案

The multi-scale time format isn’t exposed as a public API outside of the standard one returned by a d3.time.scale’s tickFormat method.

That said, the implementation itself is fairly simple, so you could create your own multi-scale time format without a lot of work. The multi-scale time format is simply an ordered array of time formats, each of which has an associated test function. The first test function that returns true determines which time format is used.

The time format used by d3.time.scale has the following formats (taken from time/scale.js), specified in reverse order:

  [".%L", function(d) { return d.getMilliseconds(); }],
  [":%S", function(d) { return d.getSeconds(); }],
  ["%I:%M", function(d) { return d.getMinutes(); }],
  ["%I %p", 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", d3_true]

So, for example, if the time has a non-zero milliseconds field, then the ".%L" format is used; otherwise, if it has a non-zero seconds field, then ":%S" is used; and so on.

这篇关于d3中的自定义多尺度时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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