File tree Expand file tree Collapse file tree 2 files changed +124
-8
lines changed Expand file tree Collapse file tree 2 files changed +124
-8
lines changed Original file line number Diff line number Diff line change 1010
1111var Lib = require ( '../../lib' ) ;
1212
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+
1321exports . 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 ) ) ;
1823 return Lib . numSeparate ( vRounded , separators ) + '%' ;
1924} ;
2025
2126exports . formatPieValue = function formatPieValue ( v , separators ) {
22- var vRounded = v . toPrecision ( 10 ) ;
23- if ( vRounded . lastIndexOf ( '.' ) !== - 1 ) {
24- vRounded = vRounded . replace ( / [ . ] ? 0 + $ / , '' ) ;
25- }
27+ var vRounded = format ( v . toPrecision ( 10 ) ) ;
2628 return Lib . numSeparate ( vRounded , separators ) ;
2729} ;
2830
Original file line number Diff line number Diff line change @@ -2064,3 +2064,117 @@ describe('pie uniformtext', function() {
20642064 . then ( done ) ;
20652065 } ) ;
20662066} ) ;
2067+
2068+ describe ( 'pie value format' , function ( ) {
2069+ 'use strict' ;
2070+
2071+ var gd ;
2072+
2073+ beforeEach ( function ( ) {
2074+ gd = createGraphDiv ( ) ;
2075+ } ) ;
2076+
2077+ afterEach ( destroyGraphDiv ) ;
2078+
2079+ it ( 'should handle rounding big & small values' , function ( done ) {
2080+ Plotly . newPlot ( gd , [ {
2081+ type : 'pie' ,
2082+ textinfo : 'value' ,
2083+ values : [
2084+ 123456789012 ,
2085+ 12345678901.2 ,
2086+ 1234567890.12 ,
2087+ 123456789.012 ,
2088+ 12345678.9012 ,
2089+ 1234567.89012 ,
2090+ 123456.789012 ,
2091+ 12345.6789012 ,
2092+ 1234.56789012 ,
2093+ 123.456789012 ,
2094+ 12.3456789012 ,
2095+ 1.23456789012 ,
2096+ 0.123456789012 ,
2097+ 0.0123456789012 ,
2098+ 0.00123456789012 ,
2099+ 0.000123456789012 ,
2100+ 0.0000123456789012 ,
2101+ 0.00000123456789012 ,
2102+ 0.000000123456789012 ,
2103+ 0.0000000123456789012
2104+ ]
2105+ } ] )
2106+ . then ( function ( ) {
2107+ var exp = [
2108+ '1.23456789e+11' ,
2109+ '1.23456789e+10' ,
2110+ '1,234,567,890' ,
2111+ '123,456,789' ,
2112+ '12,345,678.9' ,
2113+ '1,234,567.89' ,
2114+ '123,456.789' ,
2115+ '12,345.6789' ,
2116+ '1,234.56789' ,
2117+ '123.456789' ,
2118+ '12.3456789' ,
2119+ '1.23456789' ,
2120+ '0.123456789' ,
2121+ '0.0123456789' ,
2122+ '0.00123456789' ,
2123+ '0.000123456789' ,
2124+ '0.0000123456789' ,
2125+ '0.00000123456789' ,
2126+ '1.23456789e-7' ,
2127+ '1.23456789e-8'
2128+ ] ;
2129+
2130+ var selection = d3 . selectAll ( SLICES_TEXT_SELECTOR ) ;
2131+ for ( var i = 0 ; i < selection [ 0 ] . length ; i ++ ) {
2132+ var text = selection [ 0 ] [ i ] . getAttribute ( 'data-unformatted' ) ;
2133+ expect ( text ) . toBe ( exp [ i ] ) ;
2134+ }
2135+ } )
2136+ . catch ( failTest )
2137+ . then ( done ) ;
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+ } ) ;
2180+ } ) ;
You can’t perform that action at this time.
0 commit comments