Skip to content

Commit 1d5c76b

Browse files
authored
Merge pull request #1301 from plotly/annotations-gl2d
gl2d data-referenced annotations
2 parents 49106aa + 5f36071 commit 1d5c76b

File tree

9 files changed

+195
-57
lines changed

9 files changed

+195
-57
lines changed

src/plot_api/subroutines.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ exports.doModeBar = function(gd) {
295295
var subplotIds, i;
296296

297297
ModeBar.manage(gd);
298-
Plotly.Fx.supplyLayoutDefaults(gd.layout, gd._fullLayout, gd._fullData);
299298
Plotly.Fx.init(gd);
300299

301300
subplotIds = Plots.getSubplotIds(fullLayout, 'gl3d');
@@ -304,11 +303,8 @@ exports.doModeBar = function(gd) {
304303
scene.updateFx(fullLayout.dragmode, fullLayout.hovermode);
305304
}
306305

307-
subplotIds = Plots.getSubplotIds(fullLayout, 'gl2d');
308-
for(i = 0; i < subplotIds.length; i++) {
309-
var scene2d = fullLayout._plots[subplotIds[i]]._scene2d;
310-
scene2d.updateFx(fullLayout);
311-
}
306+
// no need to do this for gl2d subplots,
307+
// Plots.linkSubplots takes care of it all.
312308

313309
subplotIds = Plots.getSubplotIds(fullLayout, 'geo');
314310
for(i = 0; i < subplotIds.length; i++) {

src/plots/cartesian/axes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ axes.getFromTrace = axisIds.getFromTrace;
5959
*/
6060
axes.coerceRef = function(containerIn, containerOut, gd, attr, dflt, extraOption) {
6161
var axLetter = attr.charAt(attr.length - 1),
62-
axlist = gd._fullLayout._has('gl2d') ? [] : axes.listIds(gd, axLetter),
62+
axlist = axes.listIds(gd, axLetter),
6363
refAttr = attr + 'ref',
6464
attrDef = {};
6565

src/plots/cartesian/graph_interact.js

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ var fx = module.exports = {};
3434
// copy on Fx for backward compatible
3535
fx.unhover = dragElement.unhover;
3636

37-
fx.layoutAttributes = {
38-
};
39-
4037
fx.supplyLayoutDefaults = function(layoutIn, layoutOut, fullData) {
4138

4239
function coerce(attr, dflt) {

src/plots/gl2d/camera.js

+9
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function createCamera(scene) {
9191
updateRange(1, result.boxStart[1], result.boxEnd[1]);
9292
unSetAutoRange();
9393
result.boxEnabled = false;
94+
scene.relayoutCallback();
9495
}
9596
break;
9697

@@ -110,9 +111,15 @@ function createCamera(scene) {
110111

111112
scene.setRanges(dataBox);
112113

114+
result.panning = true;
113115
result.lastInputTime = Date.now();
114116
unSetAutoRange();
115117
scene.cameraChanged();
118+
scene.handleAnnotations();
119+
}
120+
else if(result.panning) {
121+
result.panning = false;
122+
scene.relayoutCallback();
116123
}
117124
break;
118125
}
@@ -152,6 +159,8 @@ function createCamera(scene) {
152159
result.lastInputTime = Date.now();
153160
unSetAutoRange();
154161
scene.cameraChanged();
162+
scene.handleAnnotations();
163+
scene.relayoutCallback();
155164
break;
156165
}
157166

src/plots/gl2d/scene2d.js

+49-32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
'use strict';
1111

12+
var Registry = require('../../registry');
1213
var Axes = require('../../plots/cartesian/axes');
1314
var Fx = require('../../plots/cartesian/graph_interact');
1415

@@ -33,9 +34,8 @@ function Scene2D(options, fullLayout) {
3334
this.id = options.id;
3435
this.staticPlot = !!options.staticPlot;
3536

36-
this.fullLayout = fullLayout;
3737
this.fullData = null;
38-
this.updateAxes(fullLayout);
38+
this.updateRefs(fullLayout);
3939

4040
this.makeFramework();
4141

@@ -237,6 +237,11 @@ proto.updateSize = function(canvas) {
237237
};
238238

239239
proto.computeTickMarks = function() {
240+
this.xaxis.setScale();
241+
this.yaxis.setScale();
242+
243+
// override _length from backward compatibility
244+
// even though setScale 'should' give the correct result
240245
this.xaxis._length =
241246
this.glplot.viewBox[2] - this.glplot.viewBox[0];
242247
this.yaxis._length =
@@ -272,41 +277,40 @@ function compareTicks(a, b) {
272277
return false;
273278
}
274279

275-
proto.updateAxes = function(options) {
280+
proto.updateRefs = function(newFullLayout) {
281+
this.fullLayout = newFullLayout;
282+
276283
var spmatch = Axes.subplotMatch,
277284
xaxisName = 'xaxis' + this.id.match(spmatch)[1],
278285
yaxisName = 'yaxis' + this.id.match(spmatch)[2];
279286

280-
this.xaxis = options[xaxisName];
281-
this.yaxis = options[yaxisName];
282-
};
283-
284-
proto.updateFx = function(options) {
285-
var fullLayout = this.fullLayout;
286-
287-
fullLayout.dragmode = options.dragmode;
288-
fullLayout.hovermode = options.hovermode;
287+
this.xaxis = this.fullLayout[xaxisName];
288+
this.yaxis = this.fullLayout[yaxisName];
289289
};
290290

291-
var relayoutCallback = function(scene) {
292-
293-
var xrange = scene.xaxis.range,
294-
yrange = scene.yaxis.range;
295-
296-
// Update the layout on the DIV
297-
scene.graphDiv.layout.xaxis.autorange = scene.xaxis.autorange;
298-
scene.graphDiv.layout.xaxis.range = xrange.slice(0);
299-
scene.graphDiv.layout.yaxis.autorange = scene.yaxis.autorange;
300-
scene.graphDiv.layout.yaxis.range = yrange.slice(0);
301-
302-
// Make a meaningful value to be passed on to the possible 'plotly_relayout' subscriber(s)
303-
var update = { // scene.camera has no many useful projection or scale information
304-
lastInputTime: scene.camera.lastInputTime // helps determine which one is the latest input (if async)
291+
proto.relayoutCallback = function() {
292+
var graphDiv = this.graphDiv,
293+
xaxis = this.xaxis,
294+
yaxis = this.yaxis,
295+
layout = graphDiv.layout;
296+
297+
// update user layout
298+
layout.xaxis.autorange = xaxis.autorange;
299+
layout.xaxis.range = xaxis.range.slice(0);
300+
layout.yaxis.autorange = yaxis.autorange;
301+
layout.yaxis.range = yaxis.range.slice(0);
302+
303+
// make a meaningful value to be passed on to the possible 'plotly_relayout' subscriber(s)
304+
// scene.camera has no many useful projection or scale information
305+
// helps determine which one is the latest input (if async)
306+
var update = {
307+
lastInputTime: this.camera.lastInputTime
305308
};
306-
update[scene.xaxis._name] = xrange.slice();
307-
update[scene.yaxis._name] = yrange.slice();
308309

309-
scene.graphDiv.emit('plotly_relayout', update);
310+
update[xaxis._name] = xaxis.range.slice(0);
311+
update[yaxis._name] = yaxis.range.slice(0);
312+
313+
graphDiv.emit('plotly_relayout', update);
310314
};
311315

312316
proto.cameraChanged = function() {
@@ -321,7 +325,20 @@ proto.cameraChanged = function() {
321325
this.glplotOptions.ticks = nextTicks;
322326
this.glplotOptions.dataBox = camera.dataBox;
323327
this.glplot.update(this.glplotOptions);
324-
relayoutCallback(this);
328+
this.handleAnnotations();
329+
}
330+
};
331+
332+
proto.handleAnnotations = function() {
333+
var gd = this.graphDiv,
334+
annotations = this.fullLayout.annotations;
335+
336+
for(var i = 0; i < annotations.length; i++) {
337+
var ann = annotations[i];
338+
339+
if(ann.xref === this.xaxis._id && ann.yref === this.yaxis._id) {
340+
Registry.getComponentMethod('annotations', 'drawOne')(gd, i);
341+
}
325342
}
326343
};
327344

@@ -350,8 +367,7 @@ proto.destroy = function() {
350367
proto.plot = function(fullData, calcData, fullLayout) {
351368
var glplot = this.glplot;
352369

353-
this.fullLayout = fullLayout;
354-
this.updateAxes(fullLayout);
370+
this.updateRefs(fullLayout);
355371
this.updateTraces(fullData, calcData);
356372

357373
var width = fullLayout.width,
@@ -406,6 +422,7 @@ proto.plot = function(fullData, calcData, fullLayout) {
406422
ax._length = options.viewBox[i + 2] - options.viewBox[i];
407423

408424
Axes.doAutoRange(ax);
425+
ax.setScale();
409426
}
410427

411428
options.ticks = this.computeTickMarks();

src/plots/plots.js

+4
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
655655

656656
if(oldSubplot) {
657657
plotinfo = newSubplots[id] = oldSubplot;
658+
659+
if(plotinfo._scene2d) {
660+
plotinfo._scene2d.updateRefs(newFullLayout);
661+
}
658662
}
659663
else {
660664
plotinfo = newSubplots[id] = {};
49.8 KB
Loading
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"data":[{
3+
"type": "scattergl",
4+
"x":[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5],
5+
"y":[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5],
6+
"mode":"markers"
7+
}],
8+
"layout": {
9+
"yaxis":{"autorange":false,"range":[1,5],"showgrid":false,"zeroline":false,"showticklabels":false},
10+
"xaxis":{"autorange":false,"range":[1,5],"showgrid":false,"zeroline":false,"showticklabels":false},
11+
"height":500,
12+
"width":800,
13+
"margin": {"l":100,"r":100,"top":80,"bottom":80,"pad":0},
14+
"showlegend":false,
15+
"annotations":[
16+
{"text":"left top","showarrow":false,"xref":"paper","yref":"paper","xanchor":"left","yanchor":"top","x":0,"y":1},
17+
{"text":"center middle","showarrow":false,"xref":"paper","yref":"paper","xanchor":"center","yanchor":"middle","x":0.25,"y":1},
18+
{"text":"right bottom","showarrow":false,"xref":"paper","yref":"paper","xanchor":"right","yanchor":"bottom","x":0.5,"y":1},
19+
{"text":"move with page","xref":"paper","yref":"paper","x":0.75,"y":1},
20+
{"text":"opacity","opacity":0.5,"x":5,"y":5},
21+
{"text":"not-visible", "visible": false},
22+
{"text":"left<br>justified","showarrow":false,"align":"left","x":1,"y":4},
23+
{"text":"center<br>justified","showarrow":false,"x":2,"y":4},
24+
{"text":"right<br>justified","showarrow":false,"align":"right","x":3,"y":4},
25+
{"text":"no arrow<br>page auto TR","showarrow":false,"xref":"paper","yref":"paper","x":0.75,"y":0.75},
26+
{"text":"no arrow<br>page auto ML","showarrow":false,"xref":"paper","yref":"paper","x":0.25,"y":0.5},
27+
{"text":"no arrow<br>page auto BC","showarrow":false,"xref":"paper","yref":"paper","x":0.5,"y":0.25},
28+
{"text":"default"},
29+
{"text":"no arrow","x":5,"y":4,"showarrow":false},
30+
{"text":"border","showarrow":false,"bordercolor":"rgb(148, 103, 189)","x":4,"y":3},
31+
{"text":"border width","showarrow":false,"bordercolor":"rgb(0, 0, 255)","borderwidth":3,"x":5,"y":3},
32+
{"text":"background","showarrow":false,"bgcolor":"rgb(255, 127, 14)","x":4,"y":2},
33+
{"text":"padding","showarrow":false,"bordercolor":"rgb(0, 0, 0)","borderpad":3,"x":5,"y":2},
34+
{"text":"angle<br>Bottom R","showarrow":false,"textangle":40,"x":1,"y":1,"xanchor":"right","yanchor":"bottom"},
35+
{"text":"angle<br>Middle C","showarrow":false,"textangle":-70,"x":2,"y":1,"xanchor":"center","yanchor":"middle"},
36+
{"text":"angle<br>Top L","showarrow":false,"textangle":160,"x":3,"y":1,"xanchor":"left","yanchor":"top"},
37+
{
38+
"text":"arrowhead styling","arrowcolor":"rgb(214, 39, 40)","arrowwidth":4.1,"arrowhead":7,
39+
"ax":-34,"ay":37,"arrowsize":2,"x":4,"y":1
40+
},
41+
{
42+
"text":"All together<br>with<sup style=\"font-family:raleway;fill:rgb(0,255,255)\">STYLE</sup>",
43+
"opacity":0.6,"arrowwidth":5,"arrowhead":3,"ax":-77,"ay":-5,
44+
"bordercolor":"rgb(255, 0, 0)","borderwidth":4,"bgcolor":"rgba(255,255,0,0.5)",
45+
"font":{"color":"rgb(0, 0, 255)","size":20},
46+
"arrowcolor":"rgb(166, 28, 0)","borderpad":3,"textangle":50,"x":5,"y":1
47+
},
48+
{"text":"","showarrow":true,"borderwidth":1.2,"arrowhead":2,"axref":"x","ayref":"y","x":5,"y":3,"ax":4,"ay":5},
49+
{"text":"","showarrow":true,"borderwidth":1.2,"arrowhead":2,"axref":"x","ayref":"y","x":6,"y":2,"ax":3,"ay":3}
50+
]
51+
}
52+
}

0 commit comments

Comments
 (0)