如何在D3js的月/日进行本地化?

Neo*_*eon 20 d3.js

我正在寻找一种在D3上进行本地化的方法

我找到了价值观

d3_time_days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
d3_time_dayAbbreviations = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], 
d3_time_months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], 
d3_time_monthAbbreviations = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
Run Code Online (Sandbox Code Playgroud)

D3里面,但因为他们是D3的本地/私人(显然)无法访问它们.

任何提示都会很棒,谢谢:)


更新1:

似乎只有一个重新编译可以做到魔术 - 但不能被诅咒 - 所以最终硬编码缩小版本中的编辑 - 对我感到羞耻......


更新2:

将尝试研究如何让D3接受像fx moment.js这样的"即时"本地化设置:

moment.lang('da', {
    months : "Januar_Februar_Marts_April_Maj_Juni_Juli_August_September_Oktober_November_December".split("_"),
    monthsShort : "Jan_Feb_Mar_Apr_Maj_Jun_Jul_Aug_Sep_Okt_Nov_Dec".split("_"),
    weekdays : "Søndag_Mandag_Tirsdag_Onsdag_Torsdag_Fredag_Lørdag".split("_"),
    weekdaysShort : "Søn_Man_Tir_Ons_Tor_Fre_Lør".split("_"),
    weekdaysMin : "Sø_Ma_Ti_On_To_Fr_Lø".split("_"),
    ordinal : '%d.',
    week : {
        dow : 1, // Monday is the first day of the week.
        doy : 4  // The week that contains Jan 4th is the first week of the year.
    }
});
Run Code Online (Sandbox Code Playgroud)

Adr*_*zar 30

我只是遇到了同样的问题,我找到了修复它的方法而无需重新编译d3.

https://github.com/mbostock/d3/wiki/Localization

文档提到了函数d3.locale,这个函数构建了用于格式化数字和日期的函数.因此,如果您使用自己的本地化规则来调用它,那么就在那里.

var myFormatters = d3.locale({
  "decimal": ".",
  "thousands": ",",
  "grouping": [3],
  "currency": ["$", ""],
  "dateTime": "%a %b %e %X %Y",
  "date": "%m/%d/%Y",
  "time": "%H:%M:%S",
  "periods": ["AM", "PM"],
  "days": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  "shortDays": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  "months": ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
  "shortMonths": ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"]
});
Run Code Online (Sandbox Code Playgroud)

下一步是实际告诉d3使用这个新的格式化功能.因此,使用新的时间格式器执行此操作:

d3.time.format = myFormatters.timeFormat;
Run Code Online (Sandbox Code Playgroud)


fri*_*zvd 21

我以为我会加入@Adrian Salazar的答案,因为我花了一段时间才把它弄好.如果您正在使用"开箱即用"d3时间刻度执行轴.您可以使用timeFormat.multi将其设置为语言环境

var localeFormatter = d3.locale({
    "decimal": ",",
    "thousands": ".",
    "grouping": [3],
    "currency": ["€", ""],
    "dateTime": "%a %b %e %X %Y",
    "date": "%d-%m-%Y",
    "time": "%H:%M:%S",
    "periods": ["AM", "PM"],
    "days": ["Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag"],
    "shortDays": ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"],
    "months": ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"],
    "shortMonths": ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"]
})

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

axis.tickFormat(tickFormat);
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以创建自定义d3时间格式器并使用moment.js进行本地化.例如,要创建具有本地化日期的轴:

var x = d3.time.scale();

var myTimeFormatter = function(date) {
    return moment(date).format("LL");
};

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(myTimeFormatter);
Run Code Online (Sandbox Code Playgroud)

然后使用moment.lang添加对其他语言的支持并切换当前语言.有关详细信息,请参阅http://momentjs.com/docs/#/i18n.