@@ -11,6 +11,7 @@ const MILLISECONDS_IN_SECOND = 1000;
1111const MILLISECONDS_IN_MINUTE = MILLISECONDS_IN_SECOND * 60 ; // eslint-disable-line
1212const MILLISECONDS_IN_DAY = MILLISECONDS_IN_MINUTE * 60 * 24 ; // eslint-disable-line
1313const DAYS_IN_MONTH = 30 ;
14+ const MONTHS_IN_YEAR = 12 ; //eslint-disable-line
1415
1516function getSmallestUnit ( milliseconds ) {
1617 const units = {
@@ -41,7 +42,9 @@ class UnconnectedBinSize extends Component {
4142
4243 const initialUnit =
4344 props . fullValue && typeof props . fullValue === 'string' && props . fullValue [ 0 ] === 'M'
44- ? 'months'
45+ ? parseInt ( props . fullValue . substring ( 1 ) , 10 ) % MONTHS_IN_YEAR === 0
46+ ? 'years'
47+ : 'months'
4548 : getSmallestUnit ( props . fullValue ) ;
4649
4750 this . state = {
@@ -52,6 +55,10 @@ class UnconnectedBinSize extends Component {
5255 update ( value ) {
5356 let adjustedValue = value < 0 ? 0 : value ;
5457
58+ if ( this . state . units === 'years' ) {
59+ adjustedValue = 'M' + adjustedValue * MONTHS_IN_YEAR ;
60+ }
61+
5562 if ( this . state . units === 'months' ) {
5663 adjustedValue = 'M' + adjustedValue ;
5764 }
@@ -80,7 +87,8 @@ class UnconnectedBinSize extends Component {
8087 : this . props . fullValue ;
8188
8289 this . setState ( { units : value } ) ;
83- if ( value === 'months' ) {
90+
91+ if ( [ 'years' , 'months' ] . includes ( value ) ) {
8492 this . props . updatePlot ( 'M' + Math . round ( milliseconds / MILLISECONDS_IN_DAY / DAYS_IN_MONTH ) ) ;
8593 } else {
8694 this . props . updatePlot ( milliseconds ) ;
@@ -91,10 +99,12 @@ class UnconnectedBinSize extends Component {
9199 const numericValue =
92100 typeof value === 'string' && value [ 0 ] === 'M' ? parseInt ( value . substring ( 1 ) , 10 ) : value ;
93101
102+ if ( this . state . units === 'years' ) {
103+ return numericValue / MONTHS_IN_YEAR ;
104+ }
94105 if ( this . state . units === 'months' ) {
95106 return numericValue ;
96107 }
97-
98108 if ( this . state . units === 'days' ) {
99109 return Math . round ( numericValue / MILLISECONDS_IN_DAY ) ;
100110 }
@@ -121,6 +131,7 @@ class UnconnectedBinSize extends Component {
121131 < Field { ...this . props } >
122132 < Dropdown
123133 options = { [
134+ { value : 'years' , label : _ ( 'Years' ) } ,
124135 { value : 'months' , label : _ ( 'Months' ) } ,
125136 { value : 'days' , label : _ ( 'Days' ) } ,
126137 { value : 'minutes' , label : _ ( 'Minutes' ) } ,
0 commit comments