D3.js v4.x中的d3.time.format.multi

D3.js v4.x中的d3.time.format.multi,d3.js,D3.js,在我的代码的前一个版本中,我使用如下设置适当的区域设置格式 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", "L

在我的代码的前一个版本中,我使用如下设置适当的区域设置格式

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"]
}
d3.formatDefaultLocale(format);
然后

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文档对我没有帮助

简单地说,你可以这样做

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"]
}
d3.formatDefaultLocale(format);
然后

var tickFormat=function(date){
    if(date.getMinutes()) return d3.timeFormat('%H:%M')(date);
    if(date.getHours()) return d3.timeFormat('%H:%M')(date);
    if(date.getDay()&&date.getDate()!=1) return d3.timeFormat('%a %d')(date);
    if(date.getDate()!=1) return d3.timeFormat('%b %d')(date);
    if(date.getMonth()) return d3.timeFormat('%B')(date);
    return d3.timeFormat('%Y')(date);
}


它适合我。

对于
.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);
}
//使用locale.format()建立所需的格式选项:
var formatDay=d3.timeFormat(“%a%d”),
formatWeek=d3.timeFormat(“%b%d”),
formatMonth=d3.timeFormat(“%B”),
formatYear=d3.timeFormat(“%Y”);
//定义过滤条件
函数格式(日期){
返回(d3.时间月(日期)<日期?(d3.时间周(日期)<日期?格式日:格式周)
:d3.时间年(日期)<日期?格式月
:年份(日期);
}

(您可能从中派生的
localeFormatter.timeFormat.multi()
),设置为使用上面提到的条件逻辑@altocumulus

简单地说:要使第二个代码片段成为v3代码,它必须是
localeformter.time.format.multi
,因为
timeFormat
已经是v4了。实际上,您要寻找的不是取代
d3.locale
,而是
d3.time.format.multi
,现在处理的方式略有不同。查看模块的文档(第三段开头的“您可以实现更复杂的条件时间格式…”),以获取解释。