Skip to content

Commit a7e3a9d

Browse files
committed
Transition layout titles from being strings to be objects [882]
1 parent 6687034 commit a7e3a9d

File tree

9 files changed

+66
-27
lines changed

9 files changed

+66
-27
lines changed

src/components/titles/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function draw(gd, titleClass, options) {
7474

7575
var opacity = 1;
7676
var isplaceholder = false;
77-
var txt = (cont.title || '').trim();
77+
var title = cont.title;
78+
var txt = (title && title.text ? title.text : '').trim();
7879

7980
// only make this title editable if we positively identify its property
8081
// as one that has editing enabled.

src/plot_api/helpers.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ exports.cleanLayout = function(layout) {
176176
}
177177
}
178178

179+
// Check for old-style title definitions
180+
if(!Lib.isPlainObject(layout.title)) {
181+
layout.title = {
182+
text: layout.title
183+
};
184+
}
185+
if(layout.xaxis && !Lib.isPlainObject(layout.xaxis.title)) {
186+
layout.xaxis.title = {
187+
text: layout.xaxis.title
188+
};
189+
}
190+
if(layout.yaxis && !Lib.isPlainObject(layout.yaxis.title)) {
191+
layout.yaxis.title = {
192+
text: layout.yaxis.title
193+
};
194+
}
195+
179196
/*
180197
* Moved from rotate -> orbit for dragmode
181198
*/

src/plot_api/subroutines.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ exports.drawMainTitle = function(gd) {
452452

453453
Titles.draw(gd, 'gtitle', {
454454
propContainer: fullLayout,
455-
propName: 'title',
455+
propName: 'title.text',
456456
placeholder: fullLayout._dfltTitle.plot,
457457
attributes: {
458458
x: fullLayout.width / 2,

src/plots/cartesian/axes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ axes.drawTitle = function(gd, ax) {
23872387

23882388
Titles.draw(gd, axId + 'title', {
23892389
propContainer: ax,
2390-
propName: ax._name + '.title',
2390+
propName: ax._name + '.title.text',
23912391
placeholder: fullLayout._dfltTitle[axLetter],
23922392
avoid: avoid,
23932393
transform: transform,

src/plots/cartesian/axis_defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
6868
// try to get default title from splom trace, fallback to graph-wide value
6969
var dfltTitle = splomStash.label || layoutOut._dfltTitle[letter];
7070

71-
coerce('title', dfltTitle);
71+
coerce('title.text', dfltTitle);
7272
Lib.coerceFont(coerce, 'titlefont', {
7373
family: font.family,
7474
size: Math.round(font.size * 1.2),

src/plots/cartesian/layout_attributes.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ module.exports = {
4141
].join(' ')
4242
},
4343
title: {
44-
valType: 'string',
45-
role: 'info',
46-
editType: 'ticks',
47-
description: 'Sets the title of this axis.'
44+
text: {
45+
valType: 'string',
46+
role: 'info',
47+
editType: 'ticks',
48+
description: 'Sets the title of this axis.'
49+
}
4850
},
4951
titlefont: fontAttrs({
5052
editType: 'ticks',

src/plots/layout_attributes.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ globalFont.color.dflt = colorAttrs.defaultLine;
2626
module.exports = {
2727
font: globalFont,
2828
title: {
29-
valType: 'string',
30-
role: 'info',
31-
editType: 'layoutstyle',
32-
description: [
33-
'Sets the plot\'s title.'
34-
].join(' ')
29+
text: {
30+
valType: 'string',
31+
role: 'info',
32+
editType: 'layoutstyle',
33+
description: [
34+
'Sets the plot\'s title.'
35+
].join(' ')
36+
}
3537
},
3638
titlefont: fontAttrs({
3739
editType: 'layoutstyle',

src/plots/plots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut, formatObj) {
13241324

13251325
var globalFont = Lib.coerceFont(coerce, 'font');
13261326

1327-
coerce('title', layoutOut._dfltTitle.plot);
1327+
coerce('title.text', layoutOut._dfltTitle.plot);
13281328

13291329
Lib.coerceFont(coerce, 'titlefont', {
13301330
family: globalFont.family,

test/jasmine/tests/titles_test.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ describe('Plot title', function() {
1212
'use strict';
1313

1414
var data = [{x: [1, 2, 3], y: [1, 2, 3]}];
15-
var layout = {title: 'Plotly line chart'};
15+
var layout = {
16+
title: {
17+
text: 'Plotly line chart'
18+
}
19+
};
1620
var gd;
1721

1822
beforeEach(function() {
@@ -24,11 +28,22 @@ describe('Plot title', function() {
2428
it('is centered horizontally and vertically above the plot by default', function() {
2529
Plotly.plot(gd, data, layout);
2630

31+
expectDefaultCenteredPosition();
32+
});
33+
34+
it('can still be defined as `layout.title` to ensure backwards-compatibility', function() {
35+
Plotly.plot(gd, data, {title: 'Plotly line chart'});
36+
37+
expect(titleSel().text()).toBe('Plotly line chart');
38+
expectDefaultCenteredPosition();
39+
});
40+
41+
function expectDefaultCenteredPosition() {
2742
var containerBB = gd.getBoundingClientRect();
2843

2944
expect(titleX()).toBe(containerBB.width / 2);
3045
expect(titleY()).toBe(gd._fullLayout.margin.t / 2);
31-
});
46+
}
3247

3348
function titleX() {
3449
return Number.parseFloat(titleSel().attr('x'));
@@ -39,7 +54,9 @@ describe('Plot title', function() {
3954
}
4055

4156
function titleSel() {
42-
return d3.select('.infolayer .g-gtitle .gtitle');
57+
var titleSel = d3.select('.infolayer .g-gtitle .gtitle');
58+
expect(titleSel.empty()).toBe(false, 'Title element missing');
59+
return titleSel;
4360
}
4461
});
4562

@@ -117,9 +134,9 @@ describe('editable titles', function() {
117134

118135
it('has hover effects for blank titles', function(done) {
119136
Plotly.plot(gd, data, {
120-
xaxis: {title: ''},
121-
yaxis: {title: ''},
122-
title: ''
137+
xaxis: {title: {text: ''}},
138+
yaxis: {title: {text: ''}},
139+
title: {text: ''}
123140
}, {editable: true})
124141
.then(function() {
125142
return Promise.all([
@@ -133,18 +150,18 @@ describe('editable titles', function() {
133150

134151
it('has no hover effects for titles that used to be blank', function(done) {
135152
Plotly.plot(gd, data, {
136-
xaxis: {title: ''},
137-
yaxis: {title: ''},
138-
title: ''
153+
xaxis: {title: {text: ''}},
154+
yaxis: {title: {text: ''}},
155+
title: {text: ''}
139156
}, {editable: true})
140157
.then(function() {
141-
return editTitle('x', 'xaxis.title', 'XXX');
158+
return editTitle('x', 'xaxis.title.text', 'XXX');
142159
})
143160
.then(function() {
144-
return editTitle('y', 'yaxis.title', 'YYY');
161+
return editTitle('y', 'yaxis.title.text', 'YYY');
145162
})
146163
.then(function() {
147-
return editTitle('g', 'title', 'TTT');
164+
return editTitle('g', 'title.text', 'TTT');
148165
})
149166
.then(function() {
150167
return Promise.all([

0 commit comments

Comments
 (0)