time axis localization v4

221 views
Skip to first unread message

Zlatan Sadibasic

unread,
Dec 5, 2016, 12:59:50 PM12/5/16
to d3-js
Is there an example on how to localize time x-axis using different locales. For example I'd like to localize it in Russian.

Mike Bostock

unread,
Dec 5, 2016, 1:33:06 PM12/5/16
to d3-js
The easiest way is to call d3.timeFormatDefaultLocale to assign the default locale before creating the time scale:


Another way to do it is to create a locale and then define your own custom format:


On Mon, Dec 5, 2016 at 9:59 AM Zlatan Sadibasic <zsad...@gmail.com> wrote:
Is there an example on how to localize time x-axis using different locales. For example I'd like to localize it in Russian.

--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zlatan Sadibasic

unread,
Dec 5, 2016, 1:50:51 PM12/5/16
to d3-js
That's awesome. Thank you.

Zlatan Sadibasic

unread,
Dec 5, 2016, 3:32:20 PM12/5/16
to d3-js
Just one more question, I do see that we have localized json files. Is there a feature request to make d3 compatible with moment?

After all, moment() does provide us with localized formatting options based on the loaded locales:

On Monday, December 5, 2016 at 12:59:50 PM UTC-5, Zlatan Sadibasic wrote:

Mike Bostock

unread,
Dec 5, 2016, 3:45:52 PM12/5/16
to d3-js
What do you mean by “make d3 compatible with moment”? You can use both libraries together if you prefer moment’s API to d3-time and d3-time-format; in that sense they are already compatible. For example, you can pass a custom function to axis.tickFormat to override how ticks are formatted. D3 APIs that work with dates expect JavaScript Date objects (or values that coerce to milliseconds since UNIX epoch), so for example you can pass a moment instance to a time scale’s domain and it will be correctly coerced to a standard JavaScript Date.

--

Zlatan Sadibasic

unread,
Dec 5, 2016, 6:27:21 PM12/5/16
to d3-js, mi...@ocks.org
I thought we had to always have to use 
timeFormatLocale in order to get formatting to work with d3. Now I see how I can map moment's formatting into this. Sorry for my confusion.
Reply all
Reply to author
Forward
0 new messages