Skip to content

Commit 3658023

Browse files
authored
Merge pull request #7571 from cminn10/fix-subtitle-not-clearing-properly
Fix subtitle does not properly clear/remove from the chart
2 parents da9e43e + 21562ef commit 3658023

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

draftlogs/7571_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix `layout.title.subtitle` does not properly clear/remove from the chart when `subtitle` object is not in place, or `subtitle.text` set to `null`, empty string, or whitespace-only values. [[#7571](https://github.com/plotly/plotly.js/pull/7571)]

src/components/titles/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function draw(gd, titleClass, options) {
8181
var subtitleEnabled = !!subtitleProp;
8282
var subtitlePlaceholder = options.subtitlePlaceholder;
8383
var subtitle = (cont.title || {}).subtitle || {text: '', font: {}};
84-
var subtitleTxt = subtitle.text.trim();
84+
var subtitleTxt = (subtitle.text || '').trim();
8585
var subtitleIsPlaceholder = false;
8686
var subtitleOpacity = 1;
8787

@@ -160,7 +160,7 @@ function draw(gd, titleClass, options) {
160160
var subtitleClass = titleClass + '-subtitle';
161161
var subtitleElShouldExist = subtitleTxt || editable;
162162

163-
if(subtitleEnabled && subtitleElShouldExist) {
163+
if(subtitleEnabled) {
164164
subtitleEl = group.selectAll('text.' + subtitleClass)
165165
.data(subtitleElShouldExist ? [0] : []);
166166
subtitleEl.enter().append('text');
@@ -231,7 +231,7 @@ function draw(gd, titleClass, options) {
231231
.attr(attributes)
232232
.call(svgTextUtils.convertToTspans, gd, adjustSubtitlePosition);
233233

234-
if(subtitleEl) {
234+
if(subtitleEl && !subtitleEl.empty()) {
235235
// Set subtitle y position based on bottom of title
236236
// We need to check the Mathjax group as well, in case the Mathjax
237237
// has already rendered
@@ -405,7 +405,7 @@ function draw(gd, titleClass, options) {
405405
}
406406

407407
el.classed('js-placeholder', titleIsPlaceholder);
408-
if(subtitleEl) subtitleEl.classed('js-placeholder', subtitleIsPlaceholder);
408+
if(subtitleEl && !subtitleEl.empty()) subtitleEl.classed('js-placeholder', subtitleIsPlaceholder);
409409

410410
return group;
411411
}

test/jasmine/tests/titles_test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,98 @@ describe('Title automargining', function() {
10271027
});
10281028
});
10291029

1030+
describe("Subtitle clearing via relayout", function () {
1031+
"use strict";
1032+
1033+
var data = [{ x: [1, 2, 3], y: [1, 2, 3] }];
1034+
var gd;
1035+
1036+
beforeEach(function () {
1037+
gd = createGraphDiv();
1038+
});
1039+
1040+
afterEach(destroyGraphDiv);
1041+
1042+
it("should properly clear subtitle when set to null", function (done) {
1043+
Plotly.newPlot(gd, data, {
1044+
title: {
1045+
text: "Main Title",
1046+
subtitle: { text: "Subtitle Text" },
1047+
},
1048+
})
1049+
.then(function () {
1050+
var subtitleSel = d3Select(".gtitle-subtitle");
1051+
expect(subtitleSel.empty()).toBe(
1052+
false,
1053+
"Subtitle should exist initially"
1054+
);
1055+
expect(subtitleSel.text()).toBe("Subtitle Text");
1056+
1057+
return Plotly.relayout(gd, { "title.subtitle.text": null });
1058+
})
1059+
.then(function () {
1060+
var subtitleSel = d3Select(".gtitle-subtitle");
1061+
expect(subtitleSel.empty()).toBe(
1062+
true,
1063+
"Subtitle should be removed when set to null"
1064+
);
1065+
})
1066+
.then(done, done.fail);
1067+
});
1068+
1069+
it("should properly clear subtitle when set to empty string", function (done) {
1070+
Plotly.newPlot(gd, data, {
1071+
title: {
1072+
text: "Main Title",
1073+
subtitle: { text: "Subtitle Text" },
1074+
},
1075+
})
1076+
.then(function () {
1077+
var subtitleSel = d3Select(".gtitle-subtitle");
1078+
expect(subtitleSel.empty()).toBe(
1079+
false,
1080+
"Subtitle should exist initially"
1081+
);
1082+
1083+
return Plotly.relayout(gd, { "title.subtitle.text": "" });
1084+
})
1085+
.then(function () {
1086+
var subtitleSel = d3Select(".gtitle-subtitle");
1087+
expect(subtitleSel.empty()).toBe(
1088+
true,
1089+
"Subtitle should be removed when set to empty string"
1090+
);
1091+
})
1092+
.then(done, done.fail);
1093+
});
1094+
1095+
it("should properly clear subtitle when set to whitespace", function (done) {
1096+
Plotly.newPlot(gd, data, {
1097+
title: {
1098+
text: "Main Title",
1099+
subtitle: { text: "Subtitle Text" },
1100+
},
1101+
})
1102+
.then(function () {
1103+
var subtitleSel = d3Select(".gtitle-subtitle");
1104+
expect(subtitleSel.empty()).toBe(
1105+
false,
1106+
"Subtitle should exist initially"
1107+
);
1108+
1109+
return Plotly.relayout(gd, { "title.subtitle.text": " " });
1110+
})
1111+
.then(function () {
1112+
var subtitleSel = d3Select(".gtitle-subtitle");
1113+
expect(subtitleSel.empty()).toBe(
1114+
true,
1115+
"Subtitle should be removed when set to whitespace"
1116+
);
1117+
})
1118+
.then(done, done.fail);
1119+
});
1120+
});
1121+
10301122
function expectTitle(expTitle) {
10311123
expectTitleFn(expTitle)();
10321124
}

0 commit comments

Comments
 (0)