Skip to content

Commit 954da8a

Browse files
authored
Merge pull request #1427 from plotly/gl2d-hover-curve-number
Show correct curve number in gl2d hover data
2 parents b252075 + cf6d18f commit 954da8a

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

src/plots/gl2d/scene2d.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ function Scene2D(options, fullLayout) {
5151

5252
// trace set
5353
this.traces = {};
54-
this._inputs = {};
5554

5655
// create axes spikes
5756
this.spikes = createSpikes(this.glplot);
@@ -362,7 +361,6 @@ proto.destroy = function() {
362361
this.container.removeChild(this.mouseContainer);
363362

364363
this.fullData = null;
365-
this._inputs = null;
366364
this.glplot = null;
367365
this.stopped = true;
368366
};
@@ -487,7 +485,6 @@ proto.updateTraces = function(fullData, calcData) {
487485
// update / create trace objects
488486
for(i = 0; i < fullData.length; i++) {
489487
fullTrace = fullData[i];
490-
this._inputs[fullTrace.uid] = i;
491488
var calcTrace = calcData[i],
492489
traceObj = this.traces[fullTrace.uid];
493490

@@ -500,16 +497,22 @@ proto.updateTraces = function(fullData, calcData) {
500497
};
501498

502499
proto.emitPointAction = function(nextSelection, eventType) {
500+
var uid = nextSelection.trace.uid;
501+
var trace;
503502

504-
var curveIndex = this._inputs[nextSelection.trace.uid];
503+
for(var i = 0; i < this.fullData.length; i++) {
504+
if(this.fullData[i].uid === uid) {
505+
trace = this.fullData[i];
506+
}
507+
}
505508

506509
this.graphDiv.emit(eventType, {
507510
points: [{
508511
x: nextSelection.traceCoord[0],
509512
y: nextSelection.traceCoord[1],
510-
curveNumber: curveIndex,
513+
curveNumber: trace.index,
511514
pointNumber: nextSelection.pointIndex,
512-
data: this.fullData[curveIndex]._input,
515+
data: trace._input,
513516
fullData: this.fullData,
514517
xaxis: this.xaxis,
515518
yaxis: this.yaxis

test/jasmine/karma.conf.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function func(config) {
3535
// See CONTRIBUTING.md for additional notes on reporting.
3636
func.defaultConfig.logLevel = config.LOG_INFO;
3737

38+
// without this, console logs in the plotly.js code don't print to
39+
// the terminal since karma v1.5.0
40+
//
41+
// See https://github.com/karma-runner/karma/commit/89a7a1c#commitcomment-21009216
42+
func.defaultConfig.browserConsoleLogOptions = {
43+
level: 'log'
44+
};
45+
3846
config.set(func.defaultConfig);
3947
}
4048

test/jasmine/tests/gl2d_click_test.js

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -371,38 +371,50 @@ describe('Test hover and click interactions', function() {
371371

372372
var modifiedMockCopy = Lib.extendDeep({}, mock4);
373373

374-
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout)
375-
376-
.then(new Promise(function() {
377-
378-
gd.on('plotly_hover', function(data) {
379-
futureData = data;
380-
});
381-
382-
hover(435, 216);
383-
384-
window.setTimeout(function() {
385-
386-
expect(futureData.points.length).toEqual(1);
387-
388-
var pt = futureData.points[0];
374+
function _hover() {
375+
hover(435, 216);
389376

390-
expect(Object.keys(pt)).toEqual([
391-
'x', 'y', 'curveNumber', 'pointNumber', 'data', 'fullData', 'xaxis', 'yaxis'
392-
]);
393-
394-
expect(pt.x).toEqual(8);
395-
expect(pt.y).toEqual(18);
396-
expect(pt.curveNumber).toEqual(2);
397-
expect(pt.pointNumber).toEqual(0);
398-
expect(pt.fullData.length).toEqual(3);
399-
expect(typeof pt.data.uid).toEqual('string');
400-
expect(pt.xaxis.domain.length).toEqual(2);
401-
expect(pt.yaxis.domain.length).toEqual(2);
377+
return new Promise(function(resolve) {
378+
setTimeout(resolve, 350);
379+
});
380+
}
402381

403-
done();
404-
}, 350);
405-
}));
382+
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout).then(function() {
383+
gd.on('plotly_hover', function(data) {
384+
futureData = data;
385+
});
386+
})
387+
.then(_hover)
388+
.then(function() {
389+
expect(futureData.points.length).toEqual(1);
390+
391+
var pt = futureData.points[0];
392+
393+
expect(Object.keys(pt)).toEqual([
394+
'x', 'y', 'curveNumber', 'pointNumber', 'data', 'fullData', 'xaxis', 'yaxis'
395+
]);
396+
397+
expect(pt.x).toEqual(8);
398+
expect(pt.y).toEqual(18);
399+
expect(pt.curveNumber).toEqual(2);
400+
expect(pt.pointNumber).toEqual(0);
401+
expect(pt.fullData.length).toEqual(3);
402+
expect(typeof pt.data.uid).toEqual('string');
403+
expect(pt.xaxis.domain.length).toEqual(2);
404+
expect(pt.yaxis.domain.length).toEqual(2);
405+
406+
return Plotly.restyle(gd, 'visible', false, [1, 2]);
407+
})
408+
.then(_hover)
409+
.then(function() {
410+
var pt = futureData.points[0];
411+
412+
expect(pt.x).toEqual(8);
413+
expect(pt.y).toEqual(18);
414+
expect(pt.curveNumber).toEqual(2, 'matches input data index');
415+
expect(pt.pointNumber).toEqual(0);
416+
})
417+
.then(done);
406418
});
407419

408420
it('scattergl-fancy', function(done) {

0 commit comments

Comments
 (0)