@@ -27,6 +27,17 @@ define([], function () {
27
27
SCATTER_POINTS_3D : 'scatter3d'
28
28
} ;
29
29
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
+
30
41
const is3D = function ( subGraph ) {
31
42
return subGraph . type === 'plot3D' ;
32
43
} ;
@@ -47,9 +58,13 @@ define([], function () {
47
58
rowCount = 0 ,
48
59
sceneCount = 1 ,
49
60
axisCount = 1 ;
61
+
62
+ const xMargin = needsTightLayout ( desc , 'xlabel' ) ? 0.10 : 0.05 ;
63
+ const yMargin = needsTightLayout ( desc , 'ylabel' ) ? 0.10 : 0.05 ;
64
+
50
65
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 ] ;
53
68
++ colCount ;
54
69
if ( ( index + 1 ) % numCols === 0 ) {
55
70
colCount = 0 ;
@@ -168,7 +183,6 @@ define([], function () {
168
183
height : 500
169
184
} ;
170
185
let axisProperties ;
171
-
172
186
if ( descHasMultipleSubPlots ( desc ) ) {
173
187
const numRows = Math . ceil ( desc . subGraphs . length / 2 ) ;
174
188
layout . height = numRows === 1 ? 500 : 250 * numRows ;
@@ -235,7 +249,8 @@ define([], function () {
235
249
236
250
const add3dSceneProperties = function ( subGraph ) {
237
251
const AXES_FONT = {
238
- color : '#7f7f7f'
252
+ color : '#7f7f7f' ,
253
+ size : 10
239
254
} ;
240
255
const props = {
241
256
domain : subGraph . scene . domain ,
@@ -269,6 +284,9 @@ define([], function () {
269
284
title : {
270
285
text : subGraph . xlabel ,
271
286
color : '#7f7f7f' ,
287
+ font : {
288
+ size : 10 ,
289
+ } ,
272
290
standoff : 0
273
291
} ,
274
292
visible : subGraph . images . length === 0
@@ -279,6 +297,9 @@ define([], function () {
279
297
title : {
280
298
text : subGraph . ylabel ,
281
299
color : '#7f7f7f' ,
300
+ font : {
301
+ size : 10 ,
302
+ } ,
282
303
standoff : 0
283
304
} ,
284
305
visible : subGraph . images . length === 0
@@ -289,9 +310,13 @@ define([], function () {
289
310
// https://github.com/plotly/plotly.js/issues/2746#issuecomment-528342877
290
311
// At present the only hacky way to add subplots title
291
312
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 ;
295
320
return {
296
321
text : `<b>${ subGraph . title } </b>` ,
297
322
font : {
@@ -303,8 +328,10 @@ define([], function () {
303
328
xref : 'paper' ,
304
329
yref : 'paper' ,
305
330
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
308
335
} ;
309
336
} ) ;
310
337
} ;
0 commit comments