Skip to content

Commit 1e56d7f

Browse files
committed
PR feedback: enriching event callback data
1 parent 359d50a commit 1e56d7f

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

src/plots/gl2d/scene2d.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function Scene2D(options, fullLayout) {
3333
this.staticPlot = !!options.staticPlot;
3434

3535
this.fullLayout = fullLayout;
36+
this.fullData = null;
3637
this.updateAxes(fullLayout);
3738

3839
this.makeFramework();
@@ -49,6 +50,7 @@ function Scene2D(options, fullLayout) {
4950

5051
// trace set
5152
this.traces = {};
53+
this._inputs = {};
5254

5355
// create axes spikes
5456
this.spikes = createSpikes(this.glplot);
@@ -335,6 +337,8 @@ proto.destroy = function() {
335337
this.container.removeChild(this.svgContainer);
336338
this.container.removeChild(this.mouseContainer);
337339

340+
this.fullData = null;
341+
this._inputs = null;
338342
this.glplot = null;
339343
this.stopped = true;
340344
};
@@ -425,6 +429,8 @@ proto.updateTraces = function(fullData, calcData) {
425429
var traceIds = Object.keys(this.traces);
426430
var i, j, fullTrace;
427431

432+
this.fullData = fullData;
433+
428434
// remove empty traces
429435
trace_id_loop:
430436
for(i = 0; i < traceIds.length; i++) {
@@ -446,7 +452,7 @@ proto.updateTraces = function(fullData, calcData) {
446452
// update / create trace objects
447453
for(i = 0; i < fullData.length; i++) {
448454
fullTrace = fullData[i];
449-
455+
this._inputs[fullTrace.uid] = fullTrace._input;
450456
var calcTrace = calcData[i],
451457
traceObj = this.traces[fullTrace.uid];
452458

@@ -505,13 +511,12 @@ proto.draw = function() {
505511
if(nextSelection && mouseUp) {
506512
this.graphDiv.emit('plotly_click', {
507513
points: [{
508-
trace: nextSelection.trace,
509-
dataCoord: nextSelection.dataCoord.slice(),
510-
traceCoord: nextSelection.traceCoord.slice(),
511-
textLabel: nextSelection.textLabel,
512-
color: nextSelection.color,
513-
name: nextSelection.name,
514-
hoverinfo: nextSelection.hoverinfo
514+
x: nextSelection.traceCoord[0],
515+
y: nextSelection.traceCoord[1],
516+
data: this._inputs[nextSelection.trace.uid],
517+
fullData: this.fullData,
518+
xaxis: this.xaxis,
519+
yaxis: this.yaxis
515520
}]
516521
});
517522
}
@@ -547,14 +552,12 @@ proto.draw = function() {
547552
// also it's important to copy, otherwise data is lost by the time event data is read
548553
this.graphDiv.emit('plotly_hover', {
549554
points: [{
550-
trace: nextSelection.trace,
551-
dataCoord: nextSelection.dataCoord.slice(),
552-
traceCoord: nextSelection.traceCoord.slice(),
553-
textLabel: nextSelection.textLabel,
554-
color: nextSelection.color,
555-
name: nextSelection.name,
556-
hoverinfo: nextSelection.hoverinfo,
557-
screenCoord: nextSelection.screenCoord.slice()
555+
x: nextSelection.traceCoord[0],
556+
y: nextSelection.traceCoord[1],
557+
data: this._inputs[nextSelection.trace.uid],
558+
fullData: this.fullData,
559+
xaxis: this.xaxis,
560+
yaxis: this.yaxis
558561
}]
559562
});
560563

test/jasmine/tests/gl2d_click_test.js

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var hasWebGLSupport = require('../assets/has_webgl_support');
1212
var click = require('../assets/timed_click');
1313
var hover = require('../assets/hover');
1414

15-
describe('Test click interactions:', function() {
15+
describe('Test hover and click interactions', function() {
1616

1717
if(!hasWebGLSupport('gl2d_click_test')) return;
1818

@@ -54,12 +54,14 @@ describe('Test click interactions:', function() {
5454

5555
var pt = futureData.points[0];
5656

57-
expect(Object.keys(pt)).toEqual([
58-
'trace', 'dataCoord', 'traceCoord', 'textLabel', 'color',
59-
'name', 'hoverinfo', 'screenCoord'
60-
]);
57+
expect(Object.keys(pt)).toEqual(['x', 'y', 'data', 'fullData', 'xaxis', 'yaxis']);
6158

62-
expect(pt.traceCoord).toEqual([15.772, 0.387]);
59+
expect(pt.x).toEqual(15.772);
60+
expect(pt.y).toEqual(0.387);
61+
expect(pt.fullData.length).toEqual(1);
62+
expect(typeof pt.data.uid).toEqual('string');
63+
expect(pt.xaxis.domain.length).toEqual(2);
64+
expect(pt.yaxis.domain.length).toEqual(2);
6365

6466
done();
6567
}, 250);
@@ -89,12 +91,14 @@ describe('Test click interactions:', function() {
8991

9092
var pt = futureData.points[0];
9193

92-
expect(Object.keys(pt)).toEqual([
93-
'trace', 'dataCoord', 'traceCoord', 'textLabel', 'color',
94-
'name', 'hoverinfo', 'screenCoord'
95-
]);
94+
expect(Object.keys(pt)).toEqual(['x', 'y', 'data', 'fullData', 'xaxis', 'yaxis']);
9695

97-
expect(pt.traceCoord).toEqual([15.772, 0.387]);
96+
expect(pt.x).toEqual(15.772);
97+
expect(pt.y).toEqual(0.387);
98+
expect(pt.fullData.length).toEqual(1);
99+
expect(typeof pt.data.uid).toEqual('string');
100+
expect(pt.xaxis.domain.length).toEqual(2);
101+
expect(pt.yaxis.domain.length).toEqual(2);
98102

99103
done();
100104
}, 250);
@@ -124,12 +128,15 @@ describe('Test click interactions:', function() {
124128

125129
var pt = futureData.points[0];
126130

127-
expect(Object.keys(pt)).toEqual([
128-
'trace', 'dataCoord', 'traceCoord', 'textLabel', 'color',
129-
'name', 'hoverinfo', 'screenCoord'
130-
]);
131+
expect(Object.keys(pt)).toEqual(['x', 'y', 'data', 'fullData', 'xaxis', 'yaxis']);
132+
133+
expect(pt.x).toEqual(15.772);
134+
expect(pt.y).toEqual(0.387);
135+
expect(pt.fullData.length).toEqual(1);
136+
expect(typeof pt.data.uid).toEqual('string');
137+
expect(pt.xaxis.domain.length).toEqual(2);
138+
expect(pt.yaxis.domain.length).toEqual(2);
131139

132-
expect(pt.traceCoord).toEqual([15.772, 0.387]);
133140

134141
done();
135142
}, 250);
@@ -201,7 +208,17 @@ describe('Test click interactions:', function() {
201208
click(654.7712871743302, 316.97670766680994);
202209

203210
window.setTimeout(function() {
204-
expect(futureData.points[0].traceCoord).toEqual([15.772, 0.387]);
211+
212+
var pt = futureData.points[0];
213+
214+
expect(Object.keys(pt)).toEqual(['x', 'y', 'data', 'fullData', 'xaxis', 'yaxis']);
215+
216+
expect(pt.x).toEqual(15.772);
217+
expect(pt.y).toEqual(0.387);
218+
expect(pt.fullData.length).toEqual(1);
219+
expect(typeof pt.data.uid).toEqual('string');
220+
expect(pt.xaxis.domain.length).toEqual(2);
221+
expect(pt.yaxis.domain.length).toEqual(2);
205222

206223
done();
207224

@@ -231,12 +248,14 @@ describe('Test click interactions:', function() {
231248

232249
var pt = futureData.points[0];
233250

234-
expect(Object.keys(pt)).toEqual([
235-
'trace', 'dataCoord', 'traceCoord', 'textLabel', 'color',
236-
'name', 'hoverinfo', 'screenCoord'
237-
]);
251+
expect(Object.keys(pt)).toEqual(['x', 'y', 'data', 'fullData', 'xaxis', 'yaxis']);
238252

239-
expect(pt.traceCoord).toEqual([15.772, 0.387]);
253+
expect(pt.x).toEqual(15.772);
254+
expect(pt.y).toEqual(0.387);
255+
expect(pt.fullData.length).toEqual(1);
256+
expect(typeof pt.data.uid).toEqual('string');
257+
expect(pt.xaxis.domain.length).toEqual(2);
258+
expect(pt.yaxis.domain.length).toEqual(2);
240259

241260
done();
242261
}, 250);

0 commit comments

Comments
 (0)