在我的代码的先前版本中,我曾经像这样设置适当的语言环境格式

format = {
  "decimal": ".",
  "thousands": "",
  "grouping": [3],
  "currency": ["€", ""],
  "dateTime": "%a %b %e %X %Y",
  "date": "%d-%m-%Y",
  "time": "%H:%M:%S",
  "periods": ["AM", "PM"],
  "days": ["Domenica", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato"],
  "shortDays": ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa"],
  "months": ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"],
  "shortMonths": ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"]
}


然后

var localeFormatter = d3.locale(format);

// set time tick format
var tickFormat = localeFormatter.timeFormat.multi([
  ["%H:%M", function (d) { return d.getMinutes(); }],
  ["%H:%M", 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", function () { return true; }]
]);


我终于存储了这些刻度格式设置,以便可以在图表中使用它们

D3Preferences['localTimeTickFormat'] = tickFormat;


更新到版本v4.2.8后,d3.locale消失了,我无法弄清楚如何获得相同的结果。

有人可以指出我正确的方向吗? d3文档对我没有帮助

最佳答案

在不建议使用.multi的情况下,您的tickFormat()函数现在也必须处理过滤逻辑,如下所示:

// Establish the desired formatting options using locale.format():
var formatDay = d3.timeFormat("%a %d"),
    formatWeek = d3.timeFormat("%b %d"),
    formatMonth = d3.timeFormat("%B"),
    formatYear = d3.timeFormat("%Y");

// Define filter conditions
function tickFormat(date) {
  return (d3.timeMonth(date) < date ? (d3.timeWeek(date) < date ? formatDay : formatWeek)
    : d3.timeYear(date) < date ? formatMonth
    : formatYear)(date);
}


Here's an updated version of Mike's original bl.ock(您可能从中导出localeFormatter.timeFormat.multi()的那个),设置为使用上面提到的条件逻辑@altocumulus。

07-28 01:49
查看更多