在我的代码的先前版本中,我曾经像这样设置适当的语言环境格式
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"]
}
Run Code Online (Sandbox Code Playgroud)
然后
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; }]
]);
Run Code Online (Sandbox Code Playgroud)
我终于存储了这些刻度格式设置,所以我可以在我的图表中使用它们
D3Preferences['localTimeTickFormat'] = tickFormat;
Run Code Online (Sandbox Code Playgroud)
更新到发布版本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);
}
Run Code Online (Sandbox Code Playgroud)
这是Mike的原始bl.ock(你可能从中得到的那个localeFormatter.timeFormat.multi()
)的更新版本,设置为使用上面提到的条件逻辑@altocumulus.
小智 0
简单地你可以这样做
d3.formatDefaultLocale(format);
Run Code Online (Sandbox Code Playgroud)
进而
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);
Run Code Online (Sandbox Code Playgroud)
}
这个对我有用。