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

Hover / unhover events on the 2d WebGL plots #994

Merged
merged 10 commits into from
Oct 5, 2016
44 changes: 42 additions & 2 deletions src/plots/gl2d/scene2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Scene2D(options, fullLayout) {
this.staticPlot = !!options.staticPlot;

this.fullLayout = fullLayout;
this.fullData = null;
this.updateAxes(fullLayout);

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

// trace set
this.traces = {};
this._inputs = {};

// create axes spikes
this.spikes = createSpikes(this.glplot);
Expand All @@ -58,6 +60,9 @@ function Scene2D(options, fullLayout) {
outerFill: true
});

// last button state
this.lastButtonState = 0;

// last pick result
this.pickResult = null;

Expand Down Expand Up @@ -332,6 +337,8 @@ proto.destroy = function() {
this.container.removeChild(this.svgContainer);
this.container.removeChild(this.mouseContainer);

this.fullData = null;
this._inputs = null;
Copy link
Contributor Author

@monfera monfera Oct 3, 2016

Choose a reason for hiding this comment

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

@etpinard I added this.fullData and this._inputs to retain info prerequisite for the event data contents you suggested (below), please let me know if there's a better way. Working on curveNumber and pointNumber now, I suppose these are just the indices.

this.graphDiv.emit('plotly_click', {
                points: [{
                    x: ,
                    y: ,
                    curveNumber: ,
                    pointNumber: ,
                    data: nextSelection.trace._input,
                    fullData: nextSelection.trace,
                    xaxis: this.axis,
                    yaxis: this.yaxis
                }]

Copy link
Contributor

Choose a reason for hiding this comment

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

very nice.

this.glplot = null;
this.stopped = true;
};
Expand Down Expand Up @@ -422,6 +429,8 @@ proto.updateTraces = function(fullData, calcData) {
var traceIds = Object.keys(this.traces);
var i, j, fullTrace;

this.fullData = fullData;

// remove empty traces
trace_id_loop:
for(i = 0; i < traceIds.length; i++) {
Expand All @@ -443,7 +452,7 @@ proto.updateTraces = function(fullData, calcData) {
// update / create trace objects
for(i = 0; i < fullData.length; i++) {
fullTrace = fullData[i];

this._inputs[fullTrace.uid] = fullTrace._input;
var calcTrace = calcData[i],
traceObj = this.traces[fullTrace.uid];

Expand All @@ -463,8 +472,11 @@ proto.draw = function() {
var glplot = this.glplot,
camera = this.camera,
mouseListener = camera.mouseListener,
mouseUp = this.lastButtonState === 1 && mouseListener.buttons === 0,
fullLayout = this.fullLayout;

this.lastButtonState = mouseListener.buttons;

this.cameraChanged();

var x = mouseListener.x * glplot.pixelRatio;
Expand Down Expand Up @@ -494,8 +506,22 @@ proto.draw = function() {
(y / glplot.pixelRatio) - (size.t + (1 - domainY[1]) * size.h)
);

var nextSelection = result && result.object._trace.handlePick(result);

if(nextSelection && mouseUp) {
this.graphDiv.emit('plotly_click', {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
data: this._inputs[nextSelection.trace.uid],
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});
}

if(result && result.object._trace.hoverinfo !== 'skip' && fullLayout.hovermode) {
var nextSelection = result.object._trace.handlePick(result);

if(nextSelection && (
!this.lastPickResult ||
Expand All @@ -522,6 +548,19 @@ proto.draw = function() {
glplot.pixelRatio
];

// this needs to happen before the next block that deletes traceCoord data
// also it's important to copy, otherwise data is lost by the time event data is read
this.graphDiv.emit('plotly_hover', {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
data: this._inputs[nextSelection.trace.uid],
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});

var hoverinfo = selection.hoverinfo;
if(hoverinfo !== 'all') {
var parts = hoverinfo.split('+');
Expand Down Expand Up @@ -549,6 +588,7 @@ proto.draw = function() {
else if(!result && this.lastPickResult) {
this.spikes.update({});
this.lastPickResult = null;
this.graphDiv.emit('plotly_unhover');
Fx.loneUnhover(this.svgContainer);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/jasmine/assets/hover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var mouseEvent = require('./mouse_event');

module.exports = function hover(x, y) {
mouseEvent('mousemove', x, y);
};
17 changes: 17 additions & 0 deletions test/jasmine/assets/timed_click.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var mouseEvent = require('./mouse_event');

module.exports = function click(x, y) {
mouseEvent('mousemove', x, y, {buttons: 0});

window.setTimeout(function() {

mouseEvent('mousedown', x, y, {buttons: 1});

window.setTimeout(function() {

mouseEvent('mouseup', x, y, {buttons: 0});

}, 50);

}, 150);
};
Loading