apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
62.34k stars 13.69k forks source link

Unable to concatenate d3 formula #3592

Closed vtyr closed 7 years ago

vtyr commented 7 years ago

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%

xrmx commented 7 years ago

0.18.5 is not the latest release

rumbin commented 7 years ago

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.

vtyr commented 7 years ago

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

mistercrunch commented 7 years ago

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.

mattmurray commented 7 years ago

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?

mistercrunch commented 7 years ago

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

vtyr commented 7 years ago

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

mistercrunch commented 7 years ago

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