@@ -3,6 +3,7 @@ import FusionCharts from 'fusioncharts';
33import Charts from 'fusioncharts/fusioncharts.charts' ;
44import FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion' ;
55import ReactFC from '../lib/ReactFC' ;
6+ import PropTypes from 'prop-types' ;
67
78Charts ( FusionCharts ) ;
89FusionTheme ( FusionCharts ) ;
@@ -12,34 +13,58 @@ class DrillDown extends React.Component {
1213 super ( props ) ;
1314
1415 this . state = {
16+ // Absolute position of Overlay Button
17+ positionH : ( this . props . overlayBtn && this . props . overlayBtn . placement && this . props . overlayBtn . placement . includes ( '-' ) ) ? this . props . overlayBtn . placement . split ( '-' ) [ 1 ] : 'right' ,
18+ positionV : ( this . props . overlayBtn && this . props . overlayBtn . placement && this . props . overlayBtn . placement . includes ( '-' ) ) ? this . props . overlayBtn . placement . split ( '-' ) [ 0 ] : 'top' ,
1519 selectedChild : 0 ,
1620 showDrillDown : false ,
17- // Parent Chat 's Data Source
21+ // Parent Chart 's Data Source
1822 dataSource : this . props . dataSource ,
1923 // An array of indices which maps each
2024 // data plot of parent with its nested Chart Component.
2125 mappedIds : this . props . mappedIds ,
2226 borderColor : ( this . props . overlayBtn && this . props . overlayBtn . borderColor ) ? this . props . overlayBtn . borderColor : '#000' ,
2327 backgroundColor : ( this . props . overlayBtn && this . props . overlayBtn . backgroundColor ) ? this . props . overlayBtn . backgroundColor : '#F6F6F6' ,
2428 color : ( this . props . overlayBtn && this . props . overlayBtn . color ) ? this . props . overlayBtn . color : '#000' ,
25- fontSize : ( this . props . overlayBtn && this . props . overlayBtn . fontSize ) ? this . props . overlayBtn . fontSize : '14px'
29+ fontSize : ( this . props . overlayBtn && this . props . overlayBtn . fontSize ) ? this . props . overlayBtn . fontSize : '14px' ,
30+ padding : ( this . props . overlayBtn && this . props . overlayBtn . padding ) ? this . props . overlayBtn . padding : '3px' ,
31+ fontWeight : ( this . props . fontWeight && this . props . overlayBtn . fontWeight ) ? this . props . overlayBtn . fontWeight : 'bold' ,
32+ margin : ( this . props . overlayBtn && this . props . overlayBtn . margin ) ? this . props . overlayBtn . margin : '10px'
2633 } ;
2734 }
2835
2936 // Listens to clicks on individual data plot clicks and
3037 // replaces the original chart with that corresponding
3138 // data plot's drilled down chart.
3239 plotClicked ( e ) {
33- //Index of the data plot that is clicked.
40+ // Index of the data plot that is clicked.
3441 let index = e . data . index ;
35- //Index of Drilled Down Chart.
36- let plotPosition = this . state . mappedIds [ index ] ;
42+ // Number of children passed in parent chart
43+ let idArrLength = this . props . children . length ;
3744
38- if ( ! this . state . showDrillDown && plotPosition >= 0 ) {
39- this . setState ( {
40- showDrillDown : true ,
41- selectedChild : plotPosition
42- } ) ;
45+ // This code will execute if array of integers is passed
46+ if ( typeof this . state . mappedIds [ 0 ] == 'number' ) {
47+ for ( let i = 0 ; i < this . state . mappedIds . length ; i ++ ) {
48+ let plotPosition = this . state . mappedIds [ i ] ;
49+ if ( ! this . state . showDrillDown && index == i && plotPosition && plotPosition < idArrLength ) {
50+ this . setState ( {
51+ showDrillDown : true ,
52+ selectedChild : plotPosition
53+ } ) ;
54+ }
55+ }
56+ // This code will execute if array of objects is passed
57+ } else if ( typeof this . state . mappedIds [ 0 ] == 'object' ) {
58+ for ( let i = 0 ; i < this . state . mappedIds . length ; i ++ ) {
59+ let plotPosition = this . state . mappedIds [ i ] . plotPosition ;
60+ let childPosition = this . state . mappedIds [ i ] . childPosition ;
61+ if ( index == plotPosition && ! this . state . showDrillDown && childPosition < idArrLength ) {
62+ this . setState ( {
63+ showDrillDown : true ,
64+ selectedChild : childPosition
65+ } ) ;
66+ }
67+ }
4368 }
4469 }
4570
@@ -51,42 +76,68 @@ class DrillDown extends React.Component {
5176 color : `${ this . state . color } ` ,
5277 fontFamily : 'Verdana, sans' ,
5378 fontSize : `${ this . state . fontSize } ` ,
54- padding : '3px' ,
55- fontWeight : 'bold' ,
79+ padding : ` ${ this . state . padding } ` ,
80+ fontWeight : ` ${ this . state . fontWeight } ` ,
5681 position : 'absolute' ,
57- top : '0px' ,
58- left : `${ this . props . width } px` ,
82+ [ this . state . positionH ] : `${ this . state . margin } ` ,
83+ // left: `${this.props.width - 50}px`,
84+ [ this . state . positionV ] : `${ this . state . margin } ` ,
5985 cursor : 'pointer' ,
6086 } ;
6187
62- return (
63- < div style = { {
64- position : 'relative' ,
65- } } >
66- { this . state . showDrillDown ?
67- < div style = { { position : 'relative' } } >
68- < span style = { btnStyle }
69- onClick = { ( ) => this . setState ( { showDrillDown : false } ) } >
88+ console . log ( btnStyle ) ;
89+
90+ // In-line style for root element of DrillDown component
91+ const rootStyle = {
92+ position : 'relative' ,
93+ display : 'inline-block'
94+ }
95+
96+ const props = this . props ;
97+
98+ if ( this . state . showDrillDown ) {
99+ return (
100+ < div style = { rootStyle } >
101+ { /* Displaying Correct Drilled Down Chart. */ }
102+ { React . cloneElement (
103+ this . props . children [ this . state . selectedChild ] ,
104+ { width : this . props . width , height : this . props . height }
105+ ) }
106+ < span style = { btnStyle }
107+ onClick = { ( ) => this . setState ( { showDrillDown : false } ) } >
70108 {
71109 this . props . overlayBtn && this . props . overlayBtn . message ?
72- this . props . overlayBtn . message : 'Revert '
110+ this . props . overlayBtn . message : 'Back '
73111 }
74- </ span >
75- { /* Displaying Correct Drilled Down Chart. */ }
76- { this . props . children [ this . state . selectedChild ] }
77- </ div > :
78- < ReactFC
79- type = { this . props . type }
80- width = { this . props . width }
81- height = { this . props . height }
82- dataFormat = { this . props . dataFormat }
83- dataSource = { this . props . dataSource }
112+ </ span >
113+ </ div >
114+ ) ;
115+ } else {
116+ return (
117+ < ReactFC
118+ { ...props }
84119 fcEvent-dataplotClick = { this . plotClicked . bind ( this ) }
85120 />
86- }
87- </ div >
88- ) ;
121+ ) ;
122+ }
89123 }
90124}
91125
126+ DrillDown . propTypes = {
127+ overlayBtn : PropTypes . objectOf ( PropTypes . string ) ,
128+ mappedIds : PropTypes . oneOfType ( [
129+ PropTypes . arrayOf ( PropTypes . shape ( {
130+ plotPosition : PropTypes . number ,
131+ childPosition : PropTypes . number
132+ } ) ) ,
133+ PropTypes . arrayOf ( PropTypes . number )
134+ ] ) ,
135+ dataSource : PropTypes . object ,
136+ dataFormat : PropTypes . string ,
137+ type : PropTypes . string ,
138+ height : PropTypes . string ,
139+ width : PropTypes . string ,
140+ props : PropTypes . object
141+ }
142+
92143export default DrillDown ;
0 commit comments