Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd9c867

Browse files
committed
mv 3d annotations code out of plots/gl3d into component/annotations3d
- N.B. leave (small) autorange block in scene.js
1 parent a31549d commit bd9c867

File tree

9 files changed

+356
-272
lines changed

9 files changed

+356
-272
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var annAtts = require('../annotations/attributes');
13+
14+
module.exports = {
15+
_isLinkedToArray: 'annotation',
16+
17+
visible: annAtts.visible,
18+
x: {
19+
valType: 'any',
20+
role: 'info',
21+
description: [
22+
'Sets the annotation\'s x position.'
23+
].join(' ')
24+
},
25+
y: {
26+
valType: 'any',
27+
role: 'info',
28+
description: [
29+
'Sets the annotation\'s y position.'
30+
].join(' ')
31+
},
32+
z: {
33+
valType: 'any',
34+
role: 'info',
35+
description: [
36+
'Sets the annotation\'s z position.'
37+
].join(' ')
38+
},
39+
ax: {
40+
valType: 'any',
41+
role: 'info',
42+
description: [
43+
'Sets the x component of the arrow tail about the arrow head.'
44+
].join(' ')
45+
},
46+
ay: {
47+
valType: 'any',
48+
role: 'info',
49+
description: [
50+
'Sets the y component of the arrow tail about the arrow head.'
51+
].join(' ')
52+
},
53+
54+
xanchor: annAtts.xanchor,
55+
xshift: annAtts.xshift,
56+
yanchor: annAtts.yanchor,
57+
yshift: annAtts.yshift,
58+
59+
text: annAtts.text,
60+
textangle: annAtts.textangle,
61+
font: annAtts.font,
62+
width: annAtts.width,
63+
height: annAtts.height,
64+
opacity: annAtts.opacity,
65+
align: annAtts.align,
66+
valign: annAtts.valign,
67+
bgcolor: annAtts.bgcolor,
68+
bordercolor: annAtts.bordercolor,
69+
borderpad: annAtts.borderpad,
70+
borderwidth: annAtts.borderwidth,
71+
showarrow: annAtts.showarrow,
72+
arrowcolor: annAtts.arrowcolor,
73+
arrowhead: annAtts.arrowhead,
74+
arrowsize: annAtts.arrowsize,
75+
arrowwidth: annAtts.arrowwidth,
76+
standoff: annAtts.standoff,
77+
78+
// maybes later
79+
// clicktoshow: annAtts.clicktoshow,
80+
// xclick: annAtts.xclick,
81+
// yclick: annAtts.yclick,
82+
83+
// not needed!
84+
// axref: 'pixel'
85+
// ayref: 'pixel'
86+
// xref: 'x'
87+
// yref: 'y
88+
// zref: 'z'
89+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var Lib = require('../../lib');
12+
var Axes = require('../../plots/cartesian/axes');
13+
var attributes = require('./attributes');
14+
15+
module.exports = function convert(scene) {
16+
var fullSceneLayout = scene.fullSceneLayout;
17+
var anns = fullSceneLayout.annotations;
18+
19+
for(var i = 0; i < anns.length; i++) {
20+
mockAnnAxes(anns[i], scene);
21+
}
22+
23+
scene.fullLayout._infolayer
24+
.selectAll('.annotation-' + scene.id)
25+
.remove();
26+
};
27+
28+
function mockAnnAxes(ann, scene) {
29+
var fullSceneLayout = scene.fullSceneLayout;
30+
var domain = fullSceneLayout.domain;
31+
var size = scene.fullLayout._size;
32+
33+
var base = {
34+
// this gets fill in on render
35+
pdata: null,
36+
37+
// to get setConvert to not execute cleanly
38+
type: 'linear',
39+
40+
// don't try to update them on `editable: true`
41+
autorange: false,
42+
43+
// set infinite range so that annotation draw routine
44+
// does not try to remove 'outside-range' annotations,
45+
// this case is handled in the render loop
46+
range: [-Infinity, Infinity]
47+
};
48+
49+
ann._xa = {};
50+
Lib.extendFlat(ann._xa, base);
51+
Axes.setConvert(ann._xa);
52+
ann._xa._offset = size.l + domain.x[0] * size.w;
53+
ann._xa.l2p = function() {
54+
return 0.5 * (1 + ann.pdata[0] / ann.pdata[3]) * size.w * (domain.x[1] - domain.x[0]);
55+
};
56+
57+
ann._ya = {};
58+
Lib.extendFlat(ann._ya, base);
59+
Axes.setConvert(ann._ya);
60+
ann._ya._offset = size.t + (1 - domain.y[1]) * size.h;
61+
ann._ya.l2p = function() {
62+
return 0.5 * (1 - ann.pdata[1] / ann.pdata[3]) * size.h * (domain.y[1] - domain.y[0]);
63+
};
64+
65+
// or do something more similar to 2d
66+
// where Annotations.supplyLayoutDefaults is called after in Plots.doCalcdata
67+
// if category axes are found.
68+
function coerce(attr, dflt) {
69+
return Lib.coerce(ann, ann, attributes, attr, dflt);
70+
}
71+
72+
function coercePosition(axLetter) {
73+
var axName = axLetter + 'axis';
74+
75+
// mock in such way that getFromId grabs correct 3D axis
76+
var gdMock = { _fullLayout: {} };
77+
gdMock._fullLayout[axName] = fullSceneLayout[axName];
78+
79+
return Axes.coercePosition(ann, gdMock, coerce, axLetter, axLetter, 0.5);
80+
}
81+
82+
coercePosition('x');
83+
coercePosition('y');
84+
coercePosition('z');
85+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var Lib = require('../../lib');
12+
var Color = require('../color');
13+
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
14+
var attributes = require('./attributes');
15+
16+
module.exports = function(sceneLayoutIn, sceneLayoutOut, opts) {
17+
handleArrayContainerDefaults(sceneLayoutIn, sceneLayoutOut, {
18+
name: 'annotations',
19+
handleItemDefaults: handleAnnotationDefaults,
20+
font: opts.font,
21+
scene: opts.id
22+
});
23+
};
24+
25+
function handleAnnotationDefaults(annIn, annOut, sceneLayout, opts, itemOpts) {
26+
function coerce(attr, dflt) {
27+
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
28+
}
29+
30+
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
31+
if(!visible) return annOut;
32+
33+
coerce('opacity');
34+
coerce('align');
35+
coerce('bgcolor');
36+
37+
var borderColor = coerce('bordercolor');
38+
var borderOpacity = Color.opacity(borderColor);
39+
40+
coerce('borderpad');
41+
42+
var borderWidth = coerce('borderwidth');
43+
var showArrow = coerce('showarrow');
44+
45+
coerce('text', showArrow ? ' ' : 'new text');
46+
coerce('textangle');
47+
Lib.coerceFont(coerce, 'font', opts.font);
48+
49+
coerce('width');
50+
coerce('align');
51+
52+
var h = coerce('height');
53+
if(h) coerce('valign');
54+
55+
// Do not use Axes.coercePosition here
56+
// as ax._categories aren't filled in at this stage,
57+
// Axes.coercePosition is called during scene.setConvert instead
58+
coerce('x');
59+
coerce('y');
60+
coerce('z');
61+
62+
// if you have one coordinate you should all three
63+
Lib.noneOrAll(annIn, annOut, ['x', 'y', 'z']);
64+
65+
// hard-set here for completeness
66+
annOut.xref = 'x';
67+
annOut.yref = 'y';
68+
annOut.zref = 'z';
69+
70+
coerce('xanchor');
71+
coerce('yanchor');
72+
coerce('xshift');
73+
coerce('yshift');
74+
75+
if(showArrow) {
76+
annOut.axref = 'pixel';
77+
annOut.ayref = 'pixel';
78+
79+
// TODO maybe default values should be bigger than the 2D case?
80+
coerce('ax', -10);
81+
coerce('ay', -30);
82+
83+
// if you have one part of arrow length you should have both
84+
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
85+
86+
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
87+
coerce('arrowhead');
88+
coerce('arrowsize');
89+
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
90+
coerce('standoff');
91+
}
92+
93+
annOut._scene = opts.scene;
94+
95+
return annOut;
96+
}

src/components/annotations3d/draw.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var drawRaw = require('../annotations/draw').drawRaw;
12+
var project = require('../../plots/gl3d/project');
13+
var axLetters = ['x', 'y', 'z'];
14+
15+
module.exports = function draw(scene) {
16+
var fullSceneLayout = scene.fullSceneLayout;
17+
var dataScale = scene.dataScale;
18+
var anns = fullSceneLayout.annotations;
19+
20+
for(var i = 0; i < anns.length; i++) {
21+
var ann = anns[i];
22+
var annotationIsOffscreen = false;
23+
24+
for(var j = 0; j < 3; j++) {
25+
var axLetter = axLetters[j];
26+
var pos = ann[axLetter];
27+
var ax = fullSceneLayout[axLetter + 'axis'];
28+
var posFraction = ax.r2fraction(pos);
29+
30+
if(posFraction < 0 || posFraction > 1) {
31+
annotationIsOffscreen = true;
32+
break;
33+
}
34+
}
35+
36+
if(annotationIsOffscreen) {
37+
scene.fullLayout._infolayer
38+
.select('.annotation-' + scene.id + '[data-index="' + i + '"]')
39+
.remove();
40+
} else {
41+
ann.pdata = project(scene.glplot.cameraParams, [
42+
fullSceneLayout.xaxis.r2l(ann.x) * dataScale[0],
43+
fullSceneLayout.yaxis.r2l(ann.y) * dataScale[1],
44+
fullSceneLayout.zaxis.r2l(ann.z) * dataScale[2]
45+
]);
46+
47+
drawRaw(scene.graphDiv, ann, i, ann._xa, ann._ya);
48+
}
49+
}
50+
};

src/components/annotations3d/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = {
12+
moduleType: 'component',
13+
name: 'annotations3d',
14+
15+
schema: {
16+
layout: {
17+
'scene.annotations': require('./attributes')
18+
}
19+
},
20+
21+
layoutAttributes: require('./attributes'),
22+
handleDefaults: require('./defaults'),
23+
24+
convert: require('./convert'),
25+
draw: require('./draw')
26+
};

src/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ exports.register([
5656
require('./components/fx'),
5757
require('./components/legend'),
5858
require('./components/annotations'),
59+
require('./components/annotations3d'),
5960
require('./components/shapes'),
6061
require('./components/images'),
6162
require('./components/updatemenus'),

0 commit comments

Comments
 (0)