2016-10-27 18 views
5

In einer früheren Version von meinem Code benutzte ich wie das entsprechende locale Format einzustellen diesed3.time.format.multi in v4.x

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

und dann

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

ich endlich gespeichert diese Zecke Formateinstellungen, damit ich sie in meinen Charts verwenden

D3Preferences['localTimeTickFormat'] = tickFormat; 

nach dem Update v4.2.8 freizugeben d3.locale ist weg und ich kann nicht herausfinden, wie man das gleiche Ergebnis erzielt.

Kann mir jemand in die richtige Richtung zeigen? Die d3 Dokumentation half mir nicht

+0

Nur um das klarzustellen: Damit Ihr zweiter Code-Code v3 sein kann, muss er 'localeFormatter.time.format.multi' sein, da' timeFormat' bereits v4 ist. – altocumulus

+2

Was Sie eigentlich suchen, ist kein Ersatz für 'd3.locale', sondern' d3.time.format.multi', was jetzt etwas anders gehandhabt wird. Überprüfen Sie die Dokumentation des Moduls [d3-time-format] (https://github.com/d3/d3-time-format#d3-time-format) (dritter Absatz beginnt * "Sie können komplexere bedingte Zeitformate implementieren. .. "*) für eine Erklärung. – altocumulus

Antwort

0

simplely Sie es wie diese

tun können
d3.formatDefaultLocale(format); 

und dann

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

}

Es funktioniert für mich.

0

Mit .multi veraltet, Ihre tickFormat() Funktion hat nun Filterung Logik als auch zu handhaben, wie folgt aus:

// 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 (derjenige, von dem Sie wahrscheinlich localeFormatter.timeFormat.multi() abgeleitet), stellen Sie die bedingte Logik @altocumulus verwenden Erwähnungen oben.