Skip to content

Commit 20d8c92

Browse files
committed
sankey: fix code style, variable names and cleanup draggers
1 parent 627fd74 commit 20d8c92

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

src/traces/sankey/base_plot.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports.baseLayoutAttrOverrides = overrideAll({
3030
exports.plot = function(gd) {
3131
var calcData = getModuleCalcData(gd.calcdata, SANKEY)[0];
3232
plot(gd, calcData);
33+
exports.updateFx(gd);
3334
};
3435

3536
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
@@ -38,6 +39,7 @@ exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout)
3839

3940
if(hadPlot && !hasPlot) {
4041
oldFullLayout._paperdiv.selectAll('.sankey').remove();
42+
oldFullLayout._paperdiv.selectAll('.bgsankey').remove();
4143
}
4244
};
4345

@@ -47,55 +49,55 @@ exports.updateFx = function(gd) {
4749
}
4850
};
4951

50-
var dragOptions = [];
5152
function subplotUpdateFx(gd, index) {
52-
var i = index;
53-
var fullData = gd._fullData[i];
53+
var trace = gd._fullData[index];
5454
var fullLayout = gd._fullLayout;
5555

5656
var dragMode = fullLayout.dragmode;
5757
var cursor = fullLayout.dragmode === 'pan' ? 'move' : 'crosshair';
58-
var bgRect = fullData._bgRect;
58+
var bgRect = trace._bgRect;
5959

6060
setCursor(bgRect, cursor);
6161

6262
var xaxis = {
6363
_id: 'x',
64-
c2p: function(v) { return v; },
65-
_offset: fullData._sankey.translateX,
66-
_length: fullData._sankey.width
64+
c2p: Lib.identity,
65+
_offset: trace._sankey.translateX,
66+
_length: trace._sankey.width
6767
};
6868
var yaxis = {
6969
_id: 'y',
70-
c2p: function(v) { return v; },
71-
_offset: fullData._sankey.translateY,
72-
_length: fullData._sankey.height
70+
c2p: Lib.identity,
71+
_offset: trace._sankey.translateY,
72+
_length: trace._sankey.height
7373
};
7474

7575
// Note: dragOptions is needed to be declared for all dragmodes because
7676
// it's the object that holds persistent selection state.
77-
dragOptions[i] = {
77+
var dragOptions = {
7878
gd: gd,
7979
element: bgRect.node(),
8080
plotinfo: {
81-
id: i,
81+
id: index,
8282
xaxis: xaxis,
8383
yaxis: yaxis,
8484
fillRangeItems: Lib.noop
8585
},
86-
subplot: i,
86+
subplot: index,
8787
// create mock x/y axes for hover routine
8888
xaxes: [xaxis],
8989
yaxes: [yaxis],
9090
doneFnCompleted: function(selection) {
91+
var traceNow = gd._fullData[index];
9192
var newGroups;
92-
var oldGroups = gd._fullData[i].node.groups.slice();
93+
var oldGroups = traceNow.node.groups.slice();
9394
var newGroup = [];
9495

9596
function findNode(pt) {
96-
return gd._fullData[i]._sankey.graph.nodes.find(function(n) {
97-
return n.pointNumber === pt;
98-
});
97+
var nodes = traceNow._sankey.graph.nodes;
98+
for(var i = 0; i < nodes.length; i++) {
99+
if(nodes[i].pointNumber === pt) return nodes[i];
100+
}
99101
}
100102

101103
for(var j = 0; j < selection.length; j++) {
@@ -109,25 +111,25 @@ function subplotUpdateFx(gd, index) {
109111
newGroup.push(node.childrenNodes[k].pointNumber);
110112
}
111113
// Flag group for removal from existing list of groups
112-
oldGroups[node.pointNumber - fullData.node._count] = false;
114+
oldGroups[node.pointNumber - traceNow.node._count] = false;
113115
} else {
114116
newGroup.push(node.pointNumber);
115117
}
116118
}
117119

118120
newGroups = oldGroups
119-
.filter(function(g) { return g; })
121+
.filter(Boolean)
120122
.concat([newGroup]);
121123

122124
Registry.call('_guiRestyle', gd, {
123125
'node.groups': [ newGroups ]
124-
}, i).catch(Lib.noop); // TODO will this ever fail?
126+
}, index);
125127
}
126128
};
127129

128-
dragOptions[i].prepFn = function(e, startX, startY) {
129-
prepSelect(e, startX, startY, dragOptions[i], dragMode);
130+
dragOptions.prepFn = function(e, startX, startY) {
131+
prepSelect(e, startX, startY, dragOptions, dragMode);
130132
};
131133

132-
dragElement.init(dragOptions[i]);
134+
dragElement.init(dragOptions);
133135
}

src/traces/sankey/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,15 @@ module.exports = function(gd, svg, calcData, layout, callbacks) {
838838

839839
sankey.each(function(d, i) {
840840
gd._fullData[i]._sankey = d;
841-
842841
// Draw dragbox
843-
Lib.ensureSingle(gd._fullLayout._draggers, 'rect', 'bg-' + i, function(el) {
842+
Lib.ensureSingle(gd._fullLayout._draggers, 'rect', 'bgsankey-' + d.guid, function(el) {
844843
el
845844
.style('pointer-events', 'all')
846845
.attr('width', d.width)
847846
.attr('height', d.height)
848847
.attr('x', d.translateX)
849848
.attr('y', d.translateY)
849+
.classed('bgsankey', true)
850850
.style({fill: 'transparent', 'stroke-width': 0});
851851

852852
gd._fullData[i]._bgRect = el;

src/traces/sankey/select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ module.exports = function selectPoints(searchInfo, selectionTester) {
1919
var node = nodes[i];
2020
if(node.partOfGroup) continue; // Those are invisible
2121

22-
// TODO: decide on selection criteria, using centroid for now
22+
// Position of node's centroid
2323
var pos = [(node.x0 + node.x1) / 2, (node.y0 + node.y1) / 2];
2424

2525
// Swap x and y if trace is vertical
2626
if(fullData.orientation === 'v') pos.reverse();
2727

28-
if(selectionTester.contains(pos, false, i, searchInfo)) {
28+
if(selectionTester && selectionTester.contains(pos, false, i, searchInfo)) {
2929
selection.push({
3030
pointNumber: node.pointNumber
3131
// TODO: add eventData

test/jasmine/tests/sankey_test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,20 @@ describe('sankey tests', function() {
379379
});
380380
});
381381

382+
it('Plotly.deleteTraces removes draggers', function(done) {
383+
var mockCopy = Lib.extendDeep({}, mock);
384+
Plotly.plot(gd, mockCopy)
385+
.then(function() {
386+
expect(document.getElementsByClassName('bgsankey').length).toBe(1);
387+
return Plotly.deleteTraces(gd, [0]);
388+
})
389+
.then(function() {
390+
expect(document.getElementsByClassName('bgsankey').length).toBe(0);
391+
})
392+
.catch(failTest)
393+
.then(done);
394+
});
395+
382396
it('Plotly.plot does not show Sankey if \'visible\' is false', function(done) {
383397
var mockCopy = Lib.extendDeep({}, mock);
384398

0 commit comments

Comments
 (0)