Skip to content

Commit 003f52e

Browse files
authored
Merge pull request #4241 from plotly/modebar-toggle-spikes-fixup
Modebar toggle spikes fixup
2 parents 9abf36a + 98f5f37 commit 003f52e

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

src/components/modebar/buttons.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function handleCartesian(gd, ev) {
199199
var fullLayout = gd._fullLayout;
200200
var aobj = {};
201201
var axList = axisIds.list(gd, null, true);
202-
var allSpikesEnabled = 'on';
202+
var allSpikesEnabled = fullLayout._cartesianSpikesEnabled;
203203

204204
var ax, i;
205205

@@ -214,15 +214,18 @@ function handleCartesian(gd, ev) {
214214

215215
if(!ax.fixedrange) {
216216
axName = ax._name;
217-
if(val === 'auto') aobj[axName + '.autorange'] = true;
218-
else if(val === 'reset') {
217+
if(val === 'auto') {
218+
aobj[axName + '.autorange'] = true;
219+
} else if(val === 'reset') {
219220
if(ax._rangeInitial === undefined) {
220221
aobj[axName + '.autorange'] = true;
221222
} else {
222223
var rangeInitial = ax._rangeInitial.slice();
223224
aobj[axName + '.range[0]'] = rangeInitial[0];
224225
aobj[axName + '.range[1]'] = rangeInitial[1];
225226
}
227+
228+
// N.B. "reset" also resets showspikes
226229
if(ax._showSpikeInitial !== undefined) {
227230
aobj[axName + '.showspikes'] = ax._showSpikeInitial;
228231
if(allSpikesEnabled === 'on' && !ax._showSpikeInitial) {
@@ -245,25 +248,18 @@ function handleCartesian(gd, ev) {
245248
}
246249
}
247250
}
248-
fullLayout._cartesianSpikesEnabled = allSpikesEnabled;
249251
} else {
250252
// if ALL traces have orientation 'h', 'hovermode': 'x' otherwise: 'y'
251253
if(astr === 'hovermode' && (val === 'x' || val === 'y')) {
252254
val = fullLayout._isHoriz ? 'y' : 'x';
253255
button.setAttribute('data-val', val);
254-
} else if(astr === 'hovermode' && val === 'closest') {
255-
for(i = 0; i < axList.length; i++) {
256-
ax = axList[i];
257-
if(allSpikesEnabled === 'on' && !ax.showspikes) {
258-
allSpikesEnabled = 'off';
259-
}
260-
}
261-
fullLayout._cartesianSpikesEnabled = allSpikesEnabled;
262256
}
263257

264258
aobj[astr] = val;
265259
}
266260

261+
fullLayout._cartesianSpikesEnabled = allSpikesEnabled;
262+
267263
Registry.call('_guiRelayout', gd, aobj);
268264
}
269265

@@ -581,26 +577,22 @@ modeBarButtons.toggleSpikelines = {
581577
val: 'on',
582578
click: function(gd) {
583579
var fullLayout = gd._fullLayout;
580+
var allSpikesEnabled = fullLayout._cartesianSpikesEnabled;
584581

585-
fullLayout._cartesianSpikesEnabled = fullLayout._cartesianSpikesEnabled === 'on' ? 'off' : 'on';
586-
587-
var aobj = setSpikelineVisibility(gd);
588-
589-
Registry.call('_guiRelayout', gd, aobj);
582+
fullLayout._cartesianSpikesEnabled = allSpikesEnabled === 'on' ? 'off' : 'on';
583+
Registry.call('_guiRelayout', gd, setSpikelineVisibility(gd));
590584
}
591585
};
592586

593587
function setSpikelineVisibility(gd) {
594588
var fullLayout = gd._fullLayout;
589+
var areSpikesOn = fullLayout._cartesianSpikesEnabled === 'on';
595590
var axList = axisIds.list(gd, null, true);
596591
var aobj = {};
597592

598-
var ax, axName;
599-
600593
for(var i = 0; i < axList.length; i++) {
601-
ax = axList[i];
602-
axName = ax._name;
603-
aobj[axName + '.showspikes'] = fullLayout._cartesianSpikesEnabled === 'on' ? true : ax._showSpikeInitial;
594+
var ax = axList[i];
595+
aobj[ax._name + '.showspikes'] = areSpikesOn ? true : ax._showSpikeInitial;
604596
}
605597

606598
return aobj;

test/jasmine/tests/modebar_test.js

+33
Original file line numberDiff line numberDiff line change
@@ -1201,20 +1201,23 @@ describe('ModeBar', function() {
12011201
expect(gd._fullLayout.hovermode).toBe('x');
12021202
assertActive(hovermodeButtons, buttonCompare);
12031203
});
1204+
12041205
it('should makes spikelines visible', function() {
12051206
buttonToggle.click();
12061207
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
12071208

12081209
buttonToggle.click();
12091210
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
12101211
});
1212+
12111213
it('should not become disabled when hovermode is switched off closest', function() {
12121214
buttonToggle.click();
12131215
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
12141216

12151217
buttonCompare.click();
12161218
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
12171219
});
1220+
12181221
it('should keep the state on changing the hovermode', function() {
12191222
buttonToggle.click();
12201223
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
@@ -1228,6 +1231,36 @@ describe('ModeBar', function() {
12281231
buttonClosest.click();
12291232
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
12301233
});
1234+
1235+
it('should work after clicking on "autoScale2d"', function() {
1236+
var buttonAutoScale = selectButton(modeBar, 'autoScale2d');
1237+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
1238+
1239+
buttonAutoScale.click();
1240+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
1241+
1242+
buttonToggle.click();
1243+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
1244+
1245+
buttonToggle.click();
1246+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
1247+
});
1248+
1249+
it('should work after clicking on "resetScale2d"', function() {
1250+
var buttonResetScale = selectButton(modeBar, 'resetScale2d');
1251+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
1252+
1253+
buttonToggle.click();
1254+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
1255+
1256+
buttonResetScale.click();
1257+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
1258+
1259+
buttonToggle.click();
1260+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on');
1261+
buttonToggle.click();
1262+
expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off');
1263+
});
12311264
});
12321265
});
12331266

0 commit comments

Comments
 (0)