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

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

问题描述

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

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?

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

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

谢谢.

推荐答案

除了由 d3.time.scaletickFormat 方法.

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.

也就是说,实现本身 相当简单,因此您无需大量工作即可创建自己的多尺度时间格式.多尺度时间格式只是时间格式的有序数组,每个其中有相关的测试功能.第一个返回 true 的测试函数确定使用哪种时间格式.

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.

d3.time.scale 使用的时间格式有以下几种格式(取自 time/scale.js),以相反的顺序指定:

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]

因此,例如,如果时间具有非零毫秒字段,则使用.%L"格式;否则,如果它有一个非零秒字段,则使用 ":%S";等等.

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天全站免登陆