本文介绍了如何为 D3js 按月/日进行本地化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a way to do localization on 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" ];

D3 内,但由于它们是 D3 的本地/私有,我(显然)无法访问

inside D3 but as they are local/privat to D3 i (obviously) cant access them.

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

Any hints would be great, thanks :)

似乎只有重新编译才能发挥作用 - 但不能被购买 - 所以最终在缩小版本中对编辑进行了硬编码 - 对我来说是耻辱......

Seems only a recompile can do the magic - but can't be boughtered - so ended up hard coding the edits in the minified version - shame on me...

将尝试研究如何让 D3 接受动态"本地化设置,例如 fx moment.js 这样做:

Will try to look into how to make D3 accept an "on the fly" localization setting like fx moment.js does it:

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

推荐答案

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

I just had the same issue, and I found the way to fix it without recompiling d3.

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

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

The documentation mentions the function d3.locale, this one builds the functions used for formatting numbers and dates. So if you call it with your own localization rules, you're half way there.

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

下一步是实际告诉 d3 使用这个新的格式化函数.因此,要使用新的时间格式化程序,请执行以下操作:

The next step is to actually tell d3 to use this new formatting functions. So for using the new time formatter do this:

d3.time.format = myFormatters.timeFormat;

这篇关于如何为 D3js 按月/日进行本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:45