@@ -238,22 +238,14 @@ function drawOne(gd, opts) {
238238
239239 Lib . syncOrAsync ( [
240240 Plots . previousPromises ,
241- function ( ) { return computeLegendDimensions ( gd , groups , traces , legendObj ) ; } ,
241+ function ( ) { return computeLegendDimensions ( gd , groups , traces , legendObj , scrollBox ) ; } ,
242242 function ( ) {
243243 var gs = fullLayout . _size ;
244244 var bw = legendObj . borderwidth ;
245245 var isPaperX = legendObj . xref === 'paper' ;
246246 var isPaperY = legendObj . yref === 'paper' ;
247247
248- // re-calculate title position after legend width is derived. To allow for horizontal alignment
249248 if ( title . text ) {
250- horizontalAlignTitle ( titleEl , legendObj , bw ) ;
251-
252- // Position click target for the title after dimensions are computed
253- if ( ! inHover && ( legendObj . titleclick || legendObj . titledoubleclick ) ) {
254- positionTitleToggle ( scrollBox , legendObj , legendId ) ;
255- }
256-
257249 // Toggle opacity of legend titles if all items in the legend are hidden
258250 const shapes = ( fullLayout . shapes || [ ] ) . filter ( function ( s ) { return s . showlegend ; } ) ;
259251 const anyVisible = gd . _fullData . concat ( shapes ) . some ( function ( item ) {
@@ -740,25 +732,6 @@ function setupTitleToggle(scrollBox, gd, legendObj, legendId) {
740732 } ) ;
741733}
742734
743- function positionTitleToggle ( scrollBox , legendObj , legendId ) {
744- const titleToggle = scrollBox . select ( '.' + legendId + 'titletoggle' ) ;
745- if ( ! titleToggle . size ( ) ) return ;
746-
747- const side = legendObj . title . side || 'top' ;
748- const bw = legendObj . borderwidth ;
749- var x = bw ;
750- const width = legendObj . _titleWidth + 2 * constants . titlePad ;
751- const height = legendObj . _titleHeight + 2 * constants . titlePad ;
752-
753-
754- if ( side === 'top center' ) {
755- x = bw + 0.5 * ( legendObj . _width - 2 * bw - width ) ;
756- } else if ( side === 'top right' ) {
757- x = legendObj . _width - bw - width ;
758- }
759-
760- titleToggle . attr ( { x : x , y : bw , width : width , height : height } ) ;
761- }
762735
763736function textLayout ( s , g , gd , legendObj , aTitle ) {
764737 if ( legendObj . _inHover ) s . attr ( 'data-notex' , true ) ; // do not process MathJax for unified hover
@@ -884,7 +857,7 @@ function getTitleSize(legendObj) {
884857 * - _width: legend width
885858 * - _maxWidth (for orientation:h only): maximum width before starting new row
886859 */
887- function computeLegendDimensions ( gd , groups , traces , legendObj ) {
860+ function computeLegendDimensions ( gd , groups , traces , legendObj , scrollBox ) {
888861 var fullLayout = gd . _fullLayout ;
889862 var legendId = helpers . getId ( legendObj ) ;
890863 if ( ! legendObj ) {
@@ -1091,6 +1064,25 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
10911064 }
10921065 Drawing . setRect ( traceToggle , 0 , - h / 2 , w , h ) ;
10931066 } ) ;
1067+
1068+ // align legend title horizontally
1069+ var titleEl = scrollBox . select ( '.' + legendId + 'titletext' ) ;
1070+ if ( titleEl . node ( ) ) {
1071+ horizontalAlignTitle ( titleEl , legendObj , bw ) ;
1072+ }
1073+
1074+ // position title click target to cover the title text, parallel to traceToggle above
1075+ var titleToggle = scrollBox . select ( '.' + legendId + 'titletoggle' ) ;
1076+ if ( titleToggle . size ( ) && titleEl . node ( ) ) {
1077+ var titleX = titleEl . attr ( 'x' ) || 0 ;
1078+ var pad = constants . titlePad ;
1079+ Drawing . setRect ( titleToggle ,
1080+ titleX - pad ,
1081+ bw ,
1082+ legendObj . _titleWidth + 2 * pad ,
1083+ legendObj . _titleHeight + 2 * pad
1084+ ) ;
1085+ }
10941086}
10951087
10961088function expandMargin ( gd , legendId , lx , ly ) {
0 commit comments