@@ -46,29 +46,38 @@ function getKeyPath(node) {
46
46
) ;
47
47
}
48
48
49
+ /**
50
+ * Removes the first item of the given array (Mutable)
51
+ * @param {Array } arr - the array
52
+ */
53
+ const removeFirst = arr => Array . isArray ( arr ) && arr . slice ( 1 , arr . length )
54
+
49
55
/**
50
56
* Recursively modify data depending on whether or not each cell has been selected by the hover/highlight
51
- * @param {Object } data - the current node being considered
52
- * @param {Object |Boolean } keyPath - a map of keys that are in the highlight path
53
- * if this is false then all nodes are marked as selected
57
+ * @param {Object } node - the current node being considered
58
+ * @param {Array |Boolean } keyPath - an array of keys that are in the highlight path
59
+ * if this is false then all nodes are marked as selected, and if it is [] - nothing will be marked
54
60
* @returns {Object } Updated tree structure
55
61
*/
56
- function updateData ( data , keyPath ) {
57
- if ( data . children ) {
58
- data . children . map ( child => updateData ( child , keyPath ) ) ;
59
- }
60
- // add a fill to all the uncolored cells
61
- if ( ! data . hex ) {
62
- data . style = {
62
+ function updateData ( node , path ) {
63
+ const hasToActivate = ! path || path [ 0 ] == 'root' || node . name == path [ 0 ]
64
+
65
+ // Add a fill to all the uncolored cells
66
+ if ( ! node . hex ) {
67
+ node . style = {
63
68
fill : EXTENDED_DISCRETE_COLOR_RANGE [ 5 ]
64
69
} ;
65
70
}
66
- data . style = {
67
- ...data . style ,
68
- fillOpacity : keyPath && ! keyPath [ data . name ] ? 0.2 : 1
71
+
72
+ // Manipulate the opacity of the hovered path
73
+ node . style = {
74
+ ...node . style ,
75
+ fillOpacity : hasToActivate ? 1 : 0.2
69
76
} ;
77
+
78
+ node . children && node . children . forEach ( curr => updateData ( curr , hasToActivate ? removeFirst ( path ) : [ ] ) )
70
79
71
- return data ;
80
+ return node ;
72
81
}
73
82
74
83
const decoratedData = updateData ( D3FlareData , false ) ;
@@ -97,14 +106,10 @@ export default class BasicSunburst extends React.Component {
97
106
return ;
98
107
}
99
108
const path = getKeyPath ( node ) . reverse ( ) ;
100
- const pathAsMap = path . reduce ( ( res , row ) => {
101
- res [ row ] = true ;
102
- return res ;
103
- } , { } ) ;
104
109
this . setState ( {
105
110
finalValue : path [ path . length - 1 ] ,
106
111
pathValue : path . join ( ' > ' ) ,
107
- data : updateData ( decoratedData , pathAsMap )
112
+ data : updateData ( decoratedData , path )
108
113
} ) ;
109
114
} }
110
115
onValueMouseOut = { ( ) =>
0 commit comments