8000 Images: Create image attributes and defaults · rowhit/plotly.js@a9d9860 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9d9860

Browse files
committed
Images: Create image attributes and defaults
As well as creating the attributes and defaults, a default argument has been added to `axes.coerceRef`.
1 parent 82c68a2 commit a9d9860

File tree

5 files changed

+243
-4
lines changed

5 files changed

+243
-4
lines changed

src/components/images/attributes.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* Copyright 2012-2016, 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 cartesianConstants = require('../../plots/cartesian/constants');
12+
13+
14+
module.exports = {
15+
_isLinkedToArray: true,
16+
17+
source: {
18+
valType: 'string',
19+
role: 'info',
20+
description: [
21+
'Specifies the URL of the image to be used.',
22+
'The URL must be accessible from the domain where the',
23+
'plot code is run, and can be either relative or absolute.'
24+
25+
].join(' ')
26+
},
27+
28+
layer: {
29+
valType: 'enumerated',
30+
values: ['below', 'above'],
31+
dflt: 'above',
32+
role: 'info',
33+
description: [
34+
'Specifies whether images are drawn below or above traces.',
35+
'When `xref` and `yref` are both set to `paper`,',
36+
'image is drawn below the entire plot area.'
37+
].join(' ')
38+
},
39+
40+
width: {
41+
valType: 'number',
42+
role: 'info',
43+
dflt: 0,
44+
description: [
45+
'Sets the image container width.',
46+
'The image will be sized based on the `position` value.',
47+
'When `xref` is set to `paper`, units are sized relative',
48+
'to the plot width.'
49+
].join(' ')
50+
},
51+
52+
height: {
53+
valType: 'number',
54+
role: 'info',
55+
dflt: 0,
56+
description: [
57+
'Sets the image container height.',
58+
'The image will be sized based on the `position` value.',
59+
'When `yref` is set to `paper`, units are sized relative',
60+
'to the plot height.'
61+
].join(' ')
62+
},
63+
64+
sizing: {
65+
valType: 'enumerated',
66+
values: ['fill', 'contain', 'stretch'],
67+
dflt: 'contain',
68+
role: 'info',
69+
description: [
70+
'Specifies which dimension of the image to constrain.'
71+
].join(' ')
72+
},
73+
74+
opacity: {
75+
valType: 'number',
76+
role: 'info',
77+
min: 0,
78+
max: 1,
79+
dflt: 1,
80+
description: 'Sets the opacity of the image.'
9E7A
81+
},
82+
83+
x: {
84+
valType: 'number',
85+
role: 'info',
86+
dflt: 0,
87+
description: [
88+
'Sets the image\'s x position.',
89+
'When `xref` is set to `paper`, units are sized relative',
90+
'to the plot height.',
91+
'See `xref` for more info'
92+
].join(' ')
93+
},
94+
95+
y: {
96+
valType: 'number',
97+
role: 'info',
98+
dflt: 0,
99+
description: [
100+
'Sets the image\'s y position.',
101+
'When `yref` is set to `paper`, units are sized relative',
102+
'to the plot height.',
103+
'See `yref` for more info'
104+
].join(' ')
105+
},
106+
107+
xanchor: {
108+
valType: 'enumerated',
109+
values: ['left', 'center', 'right'],
110+
dflt: 'left',
111+
role: 'info',
112+
description: 'Sets the anchor for the x position'
113+
},
114+
115+
yanchor: {
116+
valType: 'enumerated',
117+
values: ['top', 'middle', 'bottom'],
118+
dflt: 'top',
119+
role: 'info',
120+
description: 'Sets the anchor for the y position.'
121+
},
122+
123+
xref: {
124+
valType: 'enumerated',
125+
values: [
126+
'paper',
127+
cartesianConstants.idRegex.x.toString()
128+
],
129+
dflt: 'paper',
130+
role: 'info',
131+
description: [
132+
'Sets the images\'s x coordinate axis.',
133+
'If set to a x axis id (e.g. *x* or *x2*), the `x` position',
134+
'refers to an x data coordinate',
135+
'If set to *paper*, the `x` position refers to the distance from',
136+
'the left of plot in normalized coordinates',
137+
'where *0* (*1*) corresponds to the left (right).'
138+
].join(' ')
139+
},
140+
141+
yref: {
142+
valType: 'enumerated',
143+
values: [
144+
'paper',
145+
cartesianConstants.idRegex.y.toString()
146+
],
147+
dflt: 'paper',
148+
role: 'info',
149+
description: [
150+
'Sets the images\'s y coordinate axis.',
151+
'If set to a y axis id (e.g. *y* or *y2*), the `y` position',
152+
'refers to a y data coordinate.',
153+
'If set to *paper*, the `y` position refers to the distance from',
154+
'the bottom of the plot in normalized coordinates',
155+
'where *0* (*1*) corresponds to the bottom (top).'
156+
].join(' ')
157+
}
158+
};

src/components/images/defaults.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright 2012-2016, 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 Axes = require('../../plots/cartesian/axes');
12+
var Lib = require('../../lib');
13+
var attributes = require('./attributes');
14+
15+
16+
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
17+
18+
if(!layoutIn.images) return;
19+
20+
21+
var containerIn = Array.isArray(layoutIn.images) ?
22+
layoutIn.images : [layoutIn.images],
23+
containerOut = layoutOut.images = [];
24+
25+
26+
for(var i = 0; i < containerIn.length; i++) {
27+
var image = containerIn[i];
28+
29+
if(!image.source) continue;
30+
31+
var defaulted = imageDefaults(containerIn[i] || {}, containerOut[i] || {}, layoutOut);
32+
containerOut.push(defaulted);
33+
}
34+
};
35+
36+
37+
function imageDefaults(imageIn, imageOut, fullLayout) {
38+
39+
imageOut = imageOut || {};
40+
41+
function coerce(attr, dflt) {
42+
return Lib.coerce(imageIn, imageOut, attributes, attr, dflt);
43+
}
44+
45+
coerce('source');
46+
coerce('layer');
47+
coerce('x');
48+
coerce('y');
49+
coerce('xanchor');
50+
coerce('yanchor');
51+
coerce('width');
52+
coerce('height');
53+
coerce('sizing');
54+
coerce('opacity');
55+
56+
for(var i = 0; i < 2; i++) {
57+
var tdMock = { _fullLayout: fullLayout },
58+
axLetter = ['x', 'y'][i];
59+
60+
// 'paper' is the fallback axref
61+
Axes.coerceRef(imageIn, imageOut, tdMock, axLetter, 'paper');
62+
}
63+
64+
return imageOut;
65+
}

src/components/images/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright 2012-2016, 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+
12+
var supplyLayoutDefaults = require('./defaults');
13+
14+
15+
module.exports = {
16+
supplyLayoutDefaults: supplyLayoutDefaults
17+
};

src/components/rangeslider/defaults.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, axName, coun
2121
containerOut = layoutOut[axName].rangeslider = {};
2222

2323
function coerce(attr, dflt) {
24-
return Lib.coerce(containerIn, containerOut,
25-
attributes, attr, dflt);
24+
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt);
2625
}
2726

2827
coerce('bgcolor');

src/plots/cartesian/axes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ axes.getFromTrace = axisIds.getFromTrace;
3838

3939
// find the list of possible axes to reference with an xref or yref attribute
4040
// and coerce it to that list
41-
axes.coerceRef = function(containerIn, containerOut, gd, axLetter) {
41+
axes.coerceRef = function(containerIn, containerOut, gd, axLetter, dflt) {
4242
var axlist = gd._fullLayout._has('gl2d') ? [] : axes.listIds(gd, axLetter),
4343
refAttr = axLetter + 'ref',
4444
attrDef = {};
@@ -48,7 +48,7 @@ axes.coerceRef = function(containerIn, containerOut, gd, axLetter) {
4848
attrDef[refAttr] = {
4949
valType: 'enumerated',
5050
values: axlist.concat(['pap 53CB er']),
51-
dflt: axlist[0] || 'paper'
51+
dflt: dflt || axlist[0] || 'paper'
5252
};
5353

5454
// xref, yref

0 commit comments

Comments
 (0)
0