plot icon indicating copy to clipboard operation
plot copied to clipboard

A top-level locale option

Open ericemc3 opened this issue 4 years ago • 10 comments

Wherever English formatting appears by default, a simple possibility of taking into account any other language could/should be proposed.

E.g.: formatting of numbers or dates on the axes.

Suggestion: a high level locale property in a Plot specification, applying to the whole plot. By default, locale is set to "en".

This would work in a similar way to what has very usefully appeared in the Table component.

Note: vega-lite allows you to globally specify a locale for numbers and for dates:

vl.vega.timeFormatLocale(locale);
vl.vega.formatLocale(locale);

PS: Plot is a great library, very intellectually stimulating, and the 21 beautifully written articles in the https://observablehq.com/collection/@observablehq/plot collection should be read by all data scientists!

ericemc3 avatar May 12 '21 14:05 ericemc3

I propose this is a top-level Plot.plot option, i.e.,

Plot.plot({
  locale: "de",
  marks: …
})

The plot should then inherit this locale rather than the default "en-US". This is possibly a little tricky with things like Plot.formatWeekday since that function currently needs to be supplied the locale upon creation, whereas here we would need to somehow pass the plot’s locale into the format function. One way to do that might be to pass the plot’s locale to the format function instead:

function formatMonth(month = "short") {
  return (i, locale = "en-US") => {
    if (i != null && !isNaN(i = new Date(Date.UTC(2000, +i)))) {
      return i.toLocaleString(locale, {timeZone: "UTC", month});
    }
  };
}

(I think this would also be slower since we couldn’t reuse the same Intl.DateFormatInstance, but we could address that separately using memoization.)

In addition, we could do something like Plot.defaults:

plot = Plot.defaults({locale: "de"})
plot({marks: …})

mbostock avatar May 12 '21 16:05 mbostock

And more specifically: let’s not use global mutation, e.g. Plot.setLocale(locale), since this makes it hard to support multiple locales on the page and introduces ordering problems where the behavior of Plot.plot will change before or after a call to Plot.setLocale.

mbostock avatar May 12 '21 16:05 mbostock

This would also impact the automatic word "frequency", #395, and maybe other language elements.

(Regarding autodetection of "date"/"year" to omit the axis label, it is based on the field name so I would tend to not consider it in this context.)

Fil avatar May 13 '21 16:05 Fil

This would be very important for us spanish users! Date parse as mentioned for axis it's a must!

rusosnith avatar May 02 '22 18:05 rusosnith

See also https://blog.datawrapper.de/right-to-left-visualizations/

Fil avatar May 07 '22 06:05 Fil

MAybe this should be handled on notebook level? as in an Observable configuration itself?

that would be really handy

https://github.com/observablehq/feedback/issues/411#issuecomment-1172407235

rusosnith avatar Jul 01 '22 14:07 rusosnith

Any suggestion how to set ticks locale now, while the locale option is not implemented?

I am aware that I can set scale.tickFormat, but don't want to copypaste the implementation of multi-scale time format which is used in d3 by default. Is there a simpler option?

oluckyman avatar Jan 25 '23 08:01 oluckyman

don't want to copypaste

why?

Is there a simpler option?

I don't think so. The only alternative I can think of is to work on implementing this in Plot, so it can be simpler in the future.

Fil avatar Jan 25 '23 08:01 Fil

why?

I think it's irrational. Observable + Plot usually work like magic and allow to stay in the flow longer, so any obstacle feels frustrating. But it's not a problem. I'll make my own multi-scale time format to reuse in Plots. Just wondered if there was a solution that I was not aware of.

oluckyman avatar Jan 25 '23 11:01 oluckyman

I'm back here to add more push to this. So sad to see other-than-english languages are left out of the Plot magic sad emoji

rusosnith avatar Apr 10 '24 22:04 rusosnith