d3.time.format.multi in v4.x

纵饮孤独 提交于 2019-12-23 09:37:22

问题


In a previous version of my code I used to set the appropriate locale format like this

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"]
}

and then

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; }]
]);

I finally stored these tick format settings so I can use them in my charts

D3Preferences['localTimeTickFormat'] = tickFormat;

After updating to release v4.2.8 d3.locale is gone and I cannot figure out how to achieve the same result.

Can someone point me in the right direction? The d3 documentation did not help me


回答1:


With .multi deprecated, your tickFormat() function now has to handle filtering logic as well, like this:

// 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 (the one from which you likely derived localeFormatter.timeFormat.multi()), set to use the conditional logic @altocumulus mentions above.




回答2:


Simplely you can do it like this

d3.formatDefaultLocale(format);

and then

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);

}

It works for me.



来源:https://stackoverflow.com/questions/40286963/d3-time-format-multi-in-v4-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!