question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature] Global customise-able d3 locale

See original GitHub issue

Feature description

The default locale (notably for currency values) is US English. For other countries where currency might be in Euros or Pounds (or who might want to use commas or stops differently), this makes number formatting hard.

To do this we would need to set a locale for d3 before we call d3.format() on anything as per the docs: https://github.com/d3/d3-3.x-api-reference/blob/master/Localization.md

I think this should be an option in superset_config.py so that it can be optionally set for any given deployment.

As a bonus point, I think it could be neat to override locales for given metrics. This could be powerful for organisations which have data in multiple currencies. For example setting one column in yen, one in euros and one in pounds. If this is easy to implement we could just do this instead of enabling a global override locale.

Potential implementation

For the basic implementation we would need to pass through a json object which was set in a config variable to each visualisation.

in superset_config.py (for UK):

SUPERSET_D3_LOCALE = """
{
  "decimal": ".",
  "thousands": ",",
  "grouping": [3],
  "currency": ["£", ""],
  "dateTime": "%a %e %b %X %Y",
  "date": "%d/%m/%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": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  "shortMonths": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
} """

and then somewhere in the visualisation javascript we would call:

// for d3 v4
 d3.formatDefaultLocale(definition)
// for d4 v3
 d3.locale(definition)

where definition is that JSON object which we defined in superset.py

column level formatting

If we wanted to also allow column level formatting instructions then one option is allowing a JSON payload on the SqlMetric model (and somewhere on the druid metric too?) which was then passed to d3.format() as an override locale.

@mistercrunch @graceguo-supercat @xrmx - does this sound like: a) a good idea for a feature? b) a good idea for implementation? c) what’s the best way to pass through the global locale config to the right javascript? I assume on page load but I don’t know exactly where would make the most sense. Would appreciate some guidance.

Any other feedback welcome - I’m happy to do the legwork to actually implement it because we’ve got enough users that would value it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
botasky11commented, Dec 6, 2017

I agree this is a nice feature,recently I encountered this problem。

4reactions
PeterCahncommented, Sep 25, 2019

@tristix I have found a way to change the default locale for the number formatting in the Docker image.

You need to modify the file superset/assets/node_modules/d3-format/src/defaultLocale.js which is there after the command npm ci which installs the node_modules.

At this point I used sed to edit that file, like in these commands:

sed -i 's/\([[:space:]]*\)decimal: "\.",/\1decimal: ",",/g' /home/superset/superset/assets/node_modules/d3-format/src/defaultLocale.js
sed -i 's/\([[:space:]]*\)thousands: ",",/\1thousands: "\.",/g' /home/superset/superset/assets/node_modules/d3-format/src/defaultLocale.js
sed -i 's/\([[:space:]]*\)currency: \["$"\(.*\)/\1currency: \["€"\2/g'  /home/superset/superset/assets/node_modules/d3-format/src/defaultLocale.js

In this way the npm run build apply the modifications in the javascripts used by Superset.

I am able also to change the file defaultLocal.js at runtime when the image is running in development mode, with the folder superset mounted and accessible from inside the container.

I am afraid that even if you are not using Docker, the only way is to rebuild everything.

Hope this help, waiting for the new feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[feature] Global customise-able d3 locale · Issue #3972 - GitHub
There seems to be an alignment issue between Superset and D3 locales. For example, Brazilian Portuguese in Superset is PT_BR and the D3...
Read more >
d3.locale() in d3.js v4, localization - Stack Overflow
It was found that d3.locale() doesn't work anymore since the translation all date formats are on different language. How can I fix that?...
Read more >
Date Internationalization With D3.js | HTML Goodies
A web development tutorial covering date internalization with D3.js. Learn how to format locales outside of en-US.
Read more >
D3.js 7 documentation - DevDocs
D3.js 7.6.1 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Read more >
D3-Legend - Susie Lu
Tired of making legends for your data visualizations? This is a component for d3 that allows you to reuse the scales you've made...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found