Skip to content

Commit 9ab2241

Browse files
Fix bad formatting of subplots title. Closes #1450
1. Calculate tighter margins necessity for plots 2. Use smaller fonts for x, y and z-label
1 parent 377e7fe commit 9ab2241

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/visualizers/widgets/PlotlyGraph/PlotlyDescExtractor.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ define([], function () {
2727
SCATTER_POINTS_3D: 'scatter3d'
2828
};
2929

30+
const needsTightLayout = function (desc, label) {
31+
let needsTightLayout = false;
32+
for(let subGraph of desc.subGraphs){
33+
needsTightLayout = !!subGraph.title && !!subGraph[label];
34+
if(needsTightLayout){
35+
break;
36+
}
37+
}
38+
return needsTightLayout;
39+
};
40+
3041
const is3D = function (subGraph) {
3142
return subGraph.type === 'plot3D';
3243
};
@@ -47,9 +58,13 @@ define([], function () {
4758
rowCount=0,
4859
sceneCount=1,
4960
axisCount=1;
61+
62+
const xMargin = needsTightLayout(desc, 'xlabel') ? 0.10 : 0.05;
63+
const yMargin = needsTightLayout(desc, 'ylabel') ? 0.10 : 0.05;
64+
5065
desc.subGraphs.forEach((subGraph, index) => {
51-
const xDomain = [colCount * colSep + 0.05, (colCount + 1) * colSep];
52-
const yDomain = [1-(rowCount+1)*rowSep+0.05, 1-rowCount*rowSep];
66+
const xDomain = [colCount * colSep + xMargin, (colCount + 1) * colSep];
67+
const yDomain = [1-(rowCount+1)*rowSep + yMargin, 1-rowCount*rowSep];
5368
++colCount;
5469
if((index+1) % numCols === 0){
5570
colCount = 0;
@@ -168,7 +183,6 @@ define([], function () {
168183
height: 500
169184
};
170185
let axisProperties;
171-
172186
if(descHasMultipleSubPlots(desc)){
173187
const numRows = Math.ceil(desc.subGraphs.length/2);
174188
layout.height = numRows === 1 ? 500 : 250 * numRows;
@@ -235,7 +249,8 @@ define([], function () {
235249

236250
const add3dSceneProperties = function (subGraph) {
237251
const AXES_FONT = {
238-
color: '#7f7f7f'
252+
color: '#7f7f7f',
253+
size: 10
239254
};
240255
const props = {
241256
domain: subGraph.scene.domain,
@@ -269,6 +284,9 @@ define([], function () {
269284
title: {
270285
text: subGraph.xlabel,
271286
color: '#7f7f7f',
287+
font : {
288+
size: 10,
289+
},
272290
standoff: 0
273291
},
274292
visible: subGraph.images.length === 0
@@ -279,6 +297,9 @@ define([], function () {
279297
title: {
280298
text: subGraph.ylabel,
281299
color: '#7f7f7f',
300+
font : {
301+
size: 10,
302+
},
282303
standoff: 0
283304
},
284305
visible: subGraph.images.length === 0
@@ -289,9 +310,13 @@ define([], function () {
289310
// https://github.com/plotly/plotly.js/issues/2746#issuecomment-528342877
290311
// At present the only hacky way to add subplots title
291312
const addAnnotations = function (subGraphs) {
292-
const evenLength = subGraphs.length % 2 === 0 ? subGraphs.length : subGraphs.length + 1;
293-
return subGraphs.map((subGraph, index) => {
294-
const yPosMarker = (index % 2 === 0) ? index : index - 1;
313+
const average = arr => arr.reduce((runningSum, another) => runningSum + another, 0) / arr.length;
314+
if(subGraphs.length === 1){
315+
subGraphs[0].title = '';
316+
}
317+
return subGraphs.map(subGraph => {
318+
const midPointX = average(is3D(subGraph) ? subGraph.scene.domain.x : subGraph.xaxis.domain);
319+
const yPosition = (is3D(subGraph) ? subGraph.scene.domain.y[1] : subGraph.yaxis.domain[1]) + 0.005;
295320
return {
296321
text: `<b>${subGraph.title}</b>`,
297322
font: {
@@ -303,8 +328,10 @@ define([], function () {
303328
xref: 'paper',
304329
yref: 'paper',
305330
align: 'center',
306-
x: (index % 2 === 0) ? 0.15 : 0.85,
307-
y: (1 - yPosMarker / evenLength) * 1.1 - 0.06
331+
xanchor: 'center',
332+
yanchor: 'bottom',
333+
x: midPointX,
334+
y: yPosition
308335
};
309336
});
310337
};

0 commit comments

Comments
 (0)