Skip to content

Commit cf5657e

Browse files
author
Roee Zolantz
authored
Fix Duplicates bug in Basic-Sunburst showcase (uber#1300)
1 parent 712ea62 commit cf5657e

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

showcase/sunbursts/basic-sunburst.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,38 @@ function getKeyPath(node) {
4646
);
4747
}
4848

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+
4955
/**
5056
* 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
5460
* @returns {Object} Updated tree structure
5561
*/
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 = {
6368
fill: EXTENDED_DISCRETE_COLOR_RANGE[5]
6469
};
6570
}
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
6976
};
77+
78+
node.children && node.children.forEach(curr => updateData(curr, hasToActivate ? removeFirst(path) : []))
7079

71-
return data;
80+
return node;
7281
}
7382

7483
const decoratedData = updateData(D3FlareData, false);
@@ -97,14 +106,10 @@ export default class BasicSunburst extends React.Component {
97106
return;
98107
}
99108
const path = getKeyPath(node).reverse();
100-
const pathAsMap = path.reduce((res, row) => {
101-
res[row] = true;
102-
return res;
103-
}, {});
104109
this.setState({
105110
finalValue: path[path.length - 1],
106111
pathValue: path.join(' > '),
107-
data: updateData(decoratedData, pathAsMap)
112+
data: updateData(decoratedData, path)
108113
});
109114
}}
110115
onValueMouseOut={() =>

0 commit comments

Comments
 (0)