admin管理员组

文章数量:1318987

I'm currently using Superset and trying to format the y-axis of a graph to show the number as a percentage.

Values are 0.7, 0.75, 0.9, 1.2, 1.21 and so on.

I'm looking to format these as 0.70%, 0.75%, 0.90%, 1.20%, 1.21% etc. I'm having trouble formatting it in this way. When I use the '.2%' it returns two decimal places, ie. 1.21 turns into 121.00%. Using ',0.01' will return 1.2, 1.21 etc but when adding the percentage to the end, it doesn't show it - also, it doesn't add the second decimal place when it doesn't exist.

Anyone know how to format 2 point decimal places in d3 (specifically for superset, which will only take a string rather than code)

I'm currently using Superset and trying to format the y-axis of a graph to show the number as a percentage.

Values are 0.7, 0.75, 0.9, 1.2, 1.21 and so on.

I'm looking to format these as 0.70%, 0.75%, 0.90%, 1.20%, 1.21% etc. I'm having trouble formatting it in this way. When I use the '.2%' it returns two decimal places, ie. 1.21 turns into 121.00%. Using ',0.01' will return 1.2, 1.21 etc but when adding the percentage to the end, it doesn't show it - also, it doesn't add the second decimal place when it doesn't exist.

Anyone know how to format 2 point decimal places in d3 (specifically for superset, which will only take a string rather than code)

Share Improve this question edited Nov 30, 2022 at 20:30 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Oct 2, 2017 at 9:09 ah1ah1 711 gold badge3 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

In the column tab of edit table in superset you can add a new column of type float , in the sql expression you can give (column_name)/100. so 0.70 will now bee 0.0070 , now you can apply for .2% in the y-axis format .

You can check it here , try just giving the string alone

http://koaning.io/d3-format-tutorial.html

Why don't you simply concatenate them with the string "%"?

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)+ "%")
})
<script src="https://d3js/d3.v4.min.js"></script>

本文标签: javascriptFormatting a number as a percentage in d3js for SupersetStack Overflow