Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes: #1300 : Fix Duplicates bug in Basic-Sunburst showcase #1301

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions showcase/sunbursts/basic-sunburst.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,38 @@ function getKeyPath(node) {
);
}

/**
* Removes the first item of the given array (Mutable)
* @param {Array} arr - the array
*/
const removeFirst = arr => Array.isArray(arr) && arr.slice(1, arr.length)

/**
* Recursively modify data depending on whether or not each cell has been selected by the hover/highlight
* @param {Object} data - the current node being considered
* @param {Object|Boolean} keyPath - a map of keys that are in the highlight path
* if this is false then all nodes are marked as selected
* @param {Object} node - the current node being considered
* @param {Array|Boolean} keyPath - an array of keys that are in the highlight path
* if this is false then all nodes are marked as selected, and if it is [] - nothing will be marked
* @returns {Object} Updated tree structure
*/
function updateData(data, keyPath) {
if (data.children) {
data.children.map(child => updateData(child, keyPath));
}
// add a fill to all the uncolored cells
if (!data.hex) {
data.style = {
function updateData(node, path) {
const hasToActivate = !path || path[0] === 'root' || node.name === path[0]

// Add a fill to all the uncolored cells
if (!node.hex) {
node.style = {
fill: EXTENDED_DISCRETE_COLOR_RANGE[5]
};
}
data.style = {
...data.style,
fillOpacity: keyPath && !keyPath[data.name] ? 0.2 : 1

// Manipulate the opacity of the hovered path
node.style = {
...node.style,
fillOpacity: hasToActivate ? 1 : 0.2
};

node.children && node.children.forEach(curr => updateData(curr, hasToActivate ? removeFirst(path) : []))

return data;
return node;
}

const decoratedData = updateData(D3FlareData, false);
Expand Down Expand Up @@ -97,14 +106,10 @@ export default class BasicSunburst extends React.Component {
return;
}
const path = getKeyPath(node).reverse();
const pathAsMap = path.reduce((res, row) => {
res[row] = true;
return res;
}, {});
this.setState({
finalValue: path[path.length - 1],
pathValue: path.join(' > '),
data: updateData(decoratedData, pathAsMap)
data: updateData(decoratedData, path)
});
}}
onValueMouseOut={() =>
Expand Down