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

Fix branchvalues:total for generated sunburst/treemap sectors #4253

Merged
merged 2 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/traces/sunburst/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ exports.calc = function(gd, trace) {
if(impliedRoots.length === 1) {
k = impliedRoots[0];
cd.unshift({
hasImpliedRoot: true,
id: k,
pid: '',
label: k
Expand Down Expand Up @@ -153,12 +154,20 @@ exports.calc = function(gd, trace) {
break;
case 'total':
hierarchy.each(function(d) {
var v = d.data.data.v;
var cdi = d.data.data;
var v = cdi.v;

if(d.children) {
var partialSum = d.children.reduce(function(a, c) {
return a + c.data.data.v;
}, 0);

// N.B. we must fill in `value` for generated sectors
// with the partialSum to compute the correct partition
if(cdi.hasImpliedRoot || cdi.hasMultipleRoots) {
v = partialSum;
}

if(v < partialSum) {
failed = true;
return Lib.warn([
Expand Down
50 changes: 49 additions & 1 deletion test/jasmine/tests/sunburst_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('Test sunburst calc:', function() {
function extractPt(k) {
var out = gd.calcdata.map(function(cd) {
return cd[0].hierarchy.descendants().map(function(pt) {
return pt[k];
return Lib.nestedProperty(pt, k).get();
});
});
return out.length > 1 ? out : out[0];
Expand Down Expand Up @@ -317,6 +317,54 @@ describe('Test sunburst calc:', function() {
expect(extract('id')).toEqual(['true', '1', '2', '3', '4', '5', '6', '7', '8']);
expect(extract('pid')).toEqual(['', 'true', 'true', '2', '2', 'true', 'true', '6', 'true']);
});

it('should compute correct sector *value* for generated implied root', function() {
_calc([{
labels: [ 'A', 'B', 'b'],
parents: ['Root', 'Root', 'B'],
values: [1, 2, 1],
branchvalues: 'remainder'
}, {
labels: [ 'A', 'B', 'b'],
parents: ['Root', 'Root', 'B'],
values: [1, 2, 1],
branchvalues: 'total'
}]);

expect(extractPt('data.data.id')).toEqual([
['Root', 'B', 'A', 'b'],
['Root', 'B', 'A', 'b']
]);
expect(extractPt('value')).toEqual([
[4, 3, 1, 1],
[3, 2, 1, 1]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, this was [undefined, 2, 1, 1]

]);
});

it('should compute correct sector *value* for generated "root of roots"', function() {
spyOn(Lib, 'randstr').and.callFake(function() { return 'dummy'; });

_calc([{
labels: [ 'A', 'B', 'b'],
parents: ['', '', 'B'],
values: [1, 2, 1],
branchvalues: 'remainder'
}, {
labels: [ 'A', 'B', 'b'],
parents: ['', '', 'B'],
values: [1, 2, 1],
branchvalues: 'total'
}]);

expect(extractPt('data.data.id')).toEqual([
['dummy', 'B', 'A', 'b'],
['dummy', 'B', 'A', 'b']
]);
expect(extractPt('value')).toEqual([
[4, 3, 1, 1],
[3, 2, 1, 1]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To note, before this PR that this line was [undefined, 2, 1, 1] leading to SVG rendering errors

]);
});
});

describe('Test sunburst hover:', function() {
Expand Down
5 changes: 4 additions & 1 deletion test/jasmine/tests/treemap_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,10 @@ describe('Test treemap tweening:', function() {
'M284.375,188.5L548.375,188.5L548.375,308.5L284.375,308.5Z'
);
_assert('move B text to new position', 'transform', 'B', [220.25126, 0]);
_assert('enter b text to new position', 'transform', 'b', [286.16071428571433, 35714285714286]);

// TODO this node gets cleared out of viewport to some
// machine-dependent position - skip for now
// _assert('enter b text to new position', 'transform', 'b', [286.16071428571433, 35714285714286]);
})
.catch(failTest)
.then(done);
Expand Down