Skip to content

Commit f986fd2

Browse files
committed
add global layout defaults step for layout.hoverlabel
- and use coerced values for trace and annotation hoverlabel defaults.
1 parent add85d2 commit f986fd2

File tree

7 files changed

+159
-41
lines changed

7 files changed

+159
-41
lines changed

Diff for: src/components/annotations/annotation_defaults.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
var Lib = require('../../lib');
1313
var Color = require('../color');
14-
var Fx = require('../fx');
1514
var Axes = require('../../plots/cartesian/axes');
1615

1716
var attributes = require('./attributes');
@@ -113,14 +112,21 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
113112
}
114113

115114
var hoverText = coerce('hovertext');
115+
var globalHoverLabel = fullLayout.hoverlabel || {};
116+
116117
if(hoverText) {
117-
var hoverBG = coerce('hoverlabel.bgcolor',
118-
Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine);
119-
var hoverBorder = coerce('hoverlabel.bordercolor', Color.contrast(hoverBG));
118+
var hoverBG = coerce('hoverlabel.bgcolor', globalHoverLabel.bgcolor ||
119+
(Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine)
120+
);
121+
122+
var hoverBorder = coerce('hoverlabel.bordercolor', globalHoverLabel.bordercolor ||
123+
Color.contrast(hoverBG)
124+
);
125+
120126
Lib.coerceFont(coerce, 'hoverlabel.font', {
121-
family: Fx.constants.HOVERFONT,
122-
size: Fx.constants.HOVERFONTSIZE,
123-
color: hoverBorder
127+
family: globalHoverLabel.font.family,
128+
size: globalHoverLabel.font.size,
129+
color: globalHoverLabel.font.color || hoverBorder
124130
});
125131
}
126132
coerce('captureevents', !!hoverText);

Diff for: src/components/fx/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module.exports = {
2525

2626
attributes: require('./attributes'),
2727
layoutAttributes: layoutAttributes,
28+
29+
supplyLayoutGlobalDefaults: require('./layout_global_defaults'),
2830
supplyDefaults: require('./defaults'),
2931
supplyLayoutDefaults: require('./layout_defaults'),
3032

Diff for: src/components/fx/layout_attributes.js

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
'use strict';
1010

11+
var extendFlat = require('../../lib/extend').extendFlat;
12+
var fontAttrs = require('../../plots/font_attributes');
13+
var constants = require('./constants');
14+
1115
module.exports = {
1216
dragmode: {
1317
valType: 'enumerated',
@@ -26,5 +30,31 @@ module.exports = {
2630
role: 'info',
2731
values: ['x', 'y', 'closest', false],
2832
description: 'Determines the mode of hover interactions.'
33+
},
34+
35+
hoverlabel: {
36+
bgcolor: {
37+
valType: 'color',
38+
role: 'style',
39+
description: [
40+
'Sets the background color of all hover labels on graph'
41+
].join(' ')
42+
},
43+
bordercolor: {
44+
valType: 'color',
45+
role: 'style',
46+
description: [
47+
'Sets the border color of all hover labels on graph.'
48+
].join(' ')
49+
},
50+
font: {
51+
family: extendFlat({}, fontAttrs.family, {
52+
dflt: constants.HOVERFONT
53+
}),
54+
size: extendFlat({}, fontAttrs.size, {
55+
dflt: constants.HOVERFONTSIZE
56+
}),
57+
color: extendFlat({}, fontAttrs.color)
58+
}
2959
}
3060
};

Diff for: src/components/fx/layout_global_defaults.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 handleHoverLabelDefaults = require('./hoverlabel_defaults');
13+
var layoutAttributes = require('./layout_attributes');
14+
15+
module.exports = function supplyLayoutGlobalDefaults(layoutIn, layoutOut) {
16+
function coerce(attr, dflt) {
17+
return Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr, dflt);
18+
}
19+
20+
handleHoverLabelDefaults(layoutIn, layoutOut, coerce);
21+
};

Diff for: src/plots/attributes.js

+2-34
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
'use strict';
1010

11-
var constants = require('../components/fx/constants');
12-
var fontAttrs = require('./font_attributes');
13-
var extendFlat = require('../lib/extend').extendFlat;
11+
var fxAttrs = require('../components/fx/attributes');
1412

1513
module.exports = {
1614
type: {
@@ -83,37 +81,7 @@ module.exports = {
8381
'But, if `none` is set, click and hover events are still fired.'
8482
].join(' ')
8583
},
86-
hoverlabel: {
87-
bgcolor: {
88-
valType: 'color',
89-
role: 'style',
90-
arrayOk: true,
91-
description: [
92-
'Sets the background color of the hover label.'
93-
].join(' ')
94-
},
95-
bordercolor: {
96-
valType: 'color',
97-
role: 'style',
98-
arrayOk: true,
99-
description: [
100-
'Sets the border color of the hover label.'
101-
].join(' ')
102-
},
103-
font: {
104-
family: extendFlat({}, fontAttrs.family, {
105-
arrayOk: true,
106-
dflt: constants.HOVERFONT
107-
}),
108-
size: extendFlat({}, fontAttrs.size, {
109-
arrayOk: true,
110-
dflt: constants.HOVERFONTSIZE
111-
}),
112-
color: extendFlat({}, fontAttrs.color, {
113-
arrayOk: true
114-
})
115-
}
116-
},
84+
hoverlabel: fxAttrs.hoverlabel,
11785
stream: {
11886
token: {
11987
valType: 'string',

Diff for: src/plots/plots.js

+4
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,10 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) {
979979
'handleDefaults'
980980
)(layoutIn, layoutOut, 'calendar');
981981

982+
Registry.getComponentMethod(
983+
'fx',
984+
'supplyLayoutGlobalDefaults'
985+
)(layoutIn, layoutOut, coerce);
982986
};
983987

984988
plots.plotAutoSize = function plotAutoSize(gd, layout, fullLayout) {

Diff for: test/jasmine/tests/fx_test.js

+87
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,93 @@ describe('Fx defaults', function() {
9494
expect(layoutOut.hovermode).toBe('x', 'hovermode to x');
9595
expect(layoutOut.dragmode).toBe('zoom', 'dragmode to zoom');
9696
});
97+
98+
it('should coerce trace and annotations hoverlabel using global as defaults', function() {
99+
var out = _supply([{
100+
type: 'bar',
101+
y: [1, 2, 1],
102+
hoverlabel: {
103+
bgcolor: ['red', 'blue', 'black'],
104+
font: { size: 40 }
105+
}
106+
}, {
107+
type: 'scatter3d',
108+
x: [1, 2, 3],
109+
y: [1, 2, 3],
110+
z: [1, 2, 1],
111+
hoverlabel: {
112+
bordercolor: 'yellow',
113+
font: { color: 'red' }
114+
}
115+
}], {
116+
annotations: [{
117+
x: 0,
118+
y: 1,
119+
text: '1',
120+
hovertext: '1'
121+
}, {
122+
x: 2,
123+
y: 1,
124+
text: '2',
125+
hovertext: '2',
126+
hoverlabel: {
127+
bgcolor: 'red',
128+
font: {
129+
family: 'Gravitas'
130+
}
131+
}
132+
}],
133+
hoverlabel: {
134+
bgcolor: 'white',
135+
bordercolor: 'black',
136+
font: {
137+
family: 'Roboto',
138+
size: 20,
139+
color: 'pink'
140+
}
141+
}
142+
});
143+
144+
expect(out.data[0].hoverlabel).toEqual({
145+
bgcolor: ['red', 'blue', 'black'],
146+
bordercolor: 'black',
147+
font: {
148+
family: 'Roboto',
149+
size: 40,
150+
color: 'pink'
151+
}
152+
});
153+
154+
expect(out.data[1].hoverlabel).toEqual({
155+
bgcolor: 'white',
156+
bordercolor: 'yellow',
157+
font: {
158+
family: 'Roboto',
159+
size: 20,
160+
color: 'red'
161+
}
162+
});
163+
164+
expect(out.layout.annotations[0].hoverlabel).toEqual({
165+
bgcolor: 'white',
166+
bordercolor: 'black',
167+
font: {
168+
family: 'Roboto',
169+
size: 20,
170+
color: 'pink'
171+
}
172+
});
173+
174+
expect(out.layout.annotations[1].hoverlabel).toEqual({
175+
bgcolor: 'red',
176+
bordercolor: 'black',
177+
font: {
178+
family: 'Gravitas',
179+
size: 20,
180+
color: 'pink'
181+
}
182+
});
183+
});
97184
});
98185

99186
describe('relayout', function() {

0 commit comments

Comments
 (0)