File tree Expand file tree Collapse file tree 2 files changed +59
-14
lines changed Expand file tree Collapse file tree 2 files changed +59
-14
lines changed Original file line number Diff line number Diff line change 10
10
11
11
var Lib = require ( '../../lib' ) ;
12
12
13
+ function format ( vRounded ) {
14
+ return (
15
+ vRounded . indexOf ( 'e' ) !== - 1 ? vRounded . replace ( / [ . ] ? 0 + e / , 'e' ) :
16
+ vRounded . indexOf ( '.' ) !== - 1 ? vRounded . replace ( / [ . ] ? 0 + $ / , '' ) :
17
+ vRounded
18
+ ) ;
19
+ }
20
+
13
21
exports . formatPiePercent = function formatPiePercent ( v , separators ) {
14
- var vRounded = ( v * 100 ) . toPrecision ( 3 ) ;
15
- if ( vRounded . lastIndexOf ( '.' ) !== - 1 ) {
16
- vRounded = vRounded . replace ( / [ . ] ? 0 + $ / , '' ) ;
17
- }
22
+ var vRounded = format ( ( v * 100 ) . toPrecision ( 3 ) ) ;
18
23
return Lib . numSeparate ( vRounded , separators ) + '%' ;
19
24
} ;
20
25
21
26
exports . formatPieValue = function formatPieValue ( v , separators ) {
22
- var vRounded = v . toPrecision ( 10 ) ;
23
- if ( vRounded . indexOf ( 'e+' ) !== - 1 ) {
24
- vRounded = Math . round ( v ) ;
25
- } else if ( vRounded . lastIndexOf ( '.' ) !== - 1 ) {
26
- vRounded = vRounded . replace ( / [ . ] ? 0 + $ / , '' ) ;
27
- }
27
+ var vRounded = format ( v . toPrecision ( 10 ) ) ;
28
28
return Lib . numSeparate ( vRounded , separators ) ;
29
29
} ;
30
30
Original file line number Diff line number Diff line change @@ -2076,7 +2076,7 @@ describe('pie value format', function() {
2076
2076
2077
2077
afterEach ( destroyGraphDiv ) ;
2078
2078
2079
- it ( 'should handle rounding big & small numbers ' , function ( done ) {
2079
+ it ( 'should handle rounding big & small values ' , function ( done ) {
2080
2080
Plotly . newPlot ( gd , [ {
2081
2081
type : 'pie' ,
2082
2082
textinfo : 'value' ,
@@ -2099,12 +2099,14 @@ describe('pie value format', function() {
2099
2099
0.000123456789012 ,
2100
2100
0.0000123456789012 ,
2101
2101
0.00000123456789012 ,
2102
+ 0.000000123456789012 ,
2103
+ 0.0000000123456789012
2102
2104
]
2103
2105
} ] )
2104
2106
. then ( function ( ) {
2105
2107
var exp = [
2106
- '123,456,789,012 ' ,
2107
- '12,345,678,901 ' ,
2108
+ '1.23456789e+11 ' ,
2109
+ '1.23456789e+10 ' ,
2108
2110
'1,234,567,890' ,
2109
2111
'123,456,789' ,
2110
2112
'12,345,678.9' ,
@@ -2120,7 +2122,9 @@ describe('pie value format', function() {
2120
2122
'0.00123456789' ,
2121
2123
'0.000123456789' ,
2122
2124
'0.0000123456789' ,
2123
- '0.00000123456789'
2125
+ '0.00000123456789' ,
2126
+ '1.23456789e-7' ,
2127
+ '1.23456789e-8'
2124
2128
] ;
2125
2129
2126
2130
var selection = d3 . selectAll ( SLICES_TEXT_SELECTOR ) ;
@@ -2132,4 +2136,45 @@ describe('pie value format', function() {
2132
2136
. catch ( failTest )
2133
2137
. then ( done ) ;
2134
2138
} ) ;
2139
+
2140
+ it ( 'should handle rounding big & small percents' , function ( done ) {
2141
+ Plotly . newPlot ( gd , [ {
2142
+ type : 'pie' ,
2143
+ textinfo : 'percent' ,
2144
+ values : [
2145
+ 0.9 ,
2146
+ 0.09 ,
2147
+ 0.009 ,
2148
+ 0.0009 ,
2149
+ 0.00009 ,
2150
+ 0.000009 ,
2151
+ 0.0000009 ,
2152
+ 0.00000009 ,
2153
+ 0.000000009 ,
2154
+ 0.0000000009
2155
+ ]
2156
+ } ] )
2157
+ . then ( function ( ) {
2158
+ var exp = [
2159
+ '90%' ,
2160
+ '9%' ,
2161
+ '0.9%' ,
2162
+ '0.09%' ,
2163
+ '0.009%' ,
2164
+ '0.0009%' ,
2165
+ '0.00009%' ,
2166
+ '0.000009%' ,
2167
+ '9e-7%' ,
2168
+ '9e-8%'
2169
+ ] ;
2170
+
2171
+ var selection = d3 . selectAll ( SLICES_TEXT_SELECTOR ) ;
2172
+ for ( var i = 0 ; i < selection [ 0 ] . length ; i ++ ) {
2173
+ var text = selection [ 0 ] [ i ] . innerHTML ;
2174
+ expect ( text ) . toBe ( exp [ i ] ) ;
2175
+ }
2176
+ } )
2177
+ . catch ( failTest )
2178
+ . then ( done ) ;
2179
+ } ) ;
2135
2180
} ) ;
You can’t perform that action at this time.
0 commit comments