superset icon indicating copy to clipboard operation
superset copied to clipboard

Unable to concatenate d3 formula

Open vtyr opened this issue 9 years ago • 8 comments

  • [x] I have checked the superset logs for python stacktraces and included it here as text if any
  • [x] I have reproduced the issue with at least the latest released version of superset
  • [x] I have checked the issue tracker for the same issue and I haven't found one similar

I'm unsure if there is a particular way to format percentages (I've asked around) - but we usually concatenate a symbol on to a d3 formula, such as:

var numbers = [0.7, 0.75, 0.9, 1.2, 1.21]; var format = d3.format(".2f") numbers.forEach(function(d){ console.log(format(d)+ "%") })

However, SuperSet does not take code, rather a string to format numbers. This leads to the problem of rounding on percentages when not needed.

Superset version

0.18.5

Expected results

Percentage formatting without rounding to the decimal - ie. 1.2 -> 1.20%

Actual results

Currently unable to have decimal place percentages without rounding (to my knowledge)

Steps to reproduce

.2f returns 1.20 % returns 12%

vtyr avatar Oct 04 '17 11:10 vtyr

0.18.5 is not the latest release

xrmx avatar Oct 04 '17 12:10 xrmx

Here: superset 0.20.1

To my understanding, this issue is not related to any specific release, though. It is more of a fundamental question whether any string can be appended or prepended to the d3 number format string.

Like in the above example, if we assume that the column we consider already is a percentage, we do not want to multiply it by 100, which happens if we specify. e.g., '.2%'. Instead, it makes sense to format the number as is and append a '%' character.

I can imagine that any sort of string concatenation would also be useful for, e.g., currencies we might want to prepend.

rumbin avatar Oct 04 '17 15:10 rumbin

Correct, this is what we're looking to do rumbin.

vtyr avatar Oct 04 '17 16:10 vtyr

Taking code as input is a security issue. We'd like more general support for mathematical expressions using something like math.js, but any use of eval has potential XSS vulnerabilities. I asked folks in our security team and they said that if we used math.js we should do it within sandboxed iframes, which I'd rather not do at this time.

mistercrunch avatar Oct 04 '17 16:10 mistercrunch

Similar to the above comments, I have been trying to find a way to change 'G' (Giga) to 'B' (Billions) because G makes no sense when working with many types of data (in my instance, I'm displaying financial data). Is there are a way of implementing this?

mattmurray avatar Oct 05 '17 12:10 mattmurray

It's been confusing others at Airbnb too, our solution has been to do a bit of education instead of trying to change d3.format. d3.format expressions are pretty powerful and generally work.

All of our d3 formatting goes through a single function that could receive other named presets outside the scope of d3 formatting. You could add other presets here: https://github.com/apache/incubator-superset/blob/master/superset/assets/javascripts/modules/utils.js#L7 And corresponding entries in the dropdown here: https://github.com/apache/incubator-superset/blob/master/superset/assets/javascripts/explore/stores/controls.jsx#L12

mistercrunch avatar Oct 05 '17 15:10 mistercrunch

So it's not possible to convert 1.4 into 1.40% using the current setup?

vtyr avatar Oct 05 '17 18:10 vtyr

You can use an expression (calculated) column or metric to / 100 first. Otherwise to know what's possible you'll have to refer to this: https://github.com/d3/d3-format Which seems to be based off of: https://docs.python.org/3/library/string.html#formatspec

mistercrunch avatar Oct 06 '17 08:10 mistercrunch