@@ -60,6 +60,7 @@ function setLayoutDims(layouts, params) {
6060
6161function buildLayoutBoxes ( boxes ) {
6262 const layoutBoxes = wrapBoxes ( boxes ) ;
63+ const fullSize = sortByWeight ( layoutBoxes . filter ( wrap => wrap . box . fullSize ) , true ) ;
6364 const left = sortByWeight ( filterByPosition ( layoutBoxes , 'left' ) , true ) ;
6465 const right = sortByWeight ( filterByPosition ( layoutBoxes , 'right' ) ) ;
6566 const top = sortByWeight ( filterByPosition ( layoutBoxes , 'top' ) , true ) ;
@@ -68,6 +69,7 @@ function buildLayoutBoxes(boxes) {
6869 const centerVertical = filterDynamicPositionByAxis ( layoutBoxes , 'y' ) ;
6970
7071 return {
72+ fullSize,
7173 leftAndTop : left . concat ( top ) ,
7274 rightAndBottom : right . concat ( centerVertical ) . concat ( bottom ) . concat ( centerHorizontal ) ,
7375 chartArea : filterByPosition ( layoutBoxes , 'chartArea' ) ,
@@ -80,13 +82,20 @@ function getCombinedMax(maxPadding, chartArea, a, b) {
8082 return Math . max ( maxPadding [ a ] , chartArea [ a ] ) + Math . max ( maxPadding [ b ] , chartArea [ b ] ) ;
8183}
8284
85+ function updateMaxPadding ( maxPadding , boxPadding ) {
86+ maxPadding . top = Math . max ( maxPadding . top , boxPadding . top ) ;
87+ maxPadding . left = Math . max ( maxPadding . left , boxPadding . left ) ;
88+ maxPadding . bottom = Math . max ( maxPadding . bottom , boxPadding . bottom ) ;
89+ maxPadding . right = Math . max ( maxPadding . right , boxPadding . right ) ;
90+ }
91+
8392function updateDims ( chartArea , params , layout ) {
8493 const box = layout . box ;
8594 const maxPadding = chartArea . maxPadding ;
8695
8796 if ( isObject ( layout . pos ) ) {
8897 // dynamically placed boxes are not considered
89- return ;
98+ return { same : false , other : false } ;
9099 }
91100 if ( layout . size ) {
92101 // this layout was already counted for, lets first reduce old size
@@ -96,23 +105,23 @@ function updateDims(chartArea, params, layout) {
96105 chartArea [ layout . pos ] += layout . size ;
97106
98107 if ( box . getPadding ) {
99- const boxPadding = box . getPadding ( ) ;
100- maxPadding . top = Math . max ( maxPadding . top , boxPadding . top ) ;
101- maxPadding . left = Math . max ( maxPadding . left , boxPadding . left ) ;
102- maxPadding . bottom = Math . max ( maxPadding . bottom , boxPadding . bottom ) ;
103- maxPadding . right = Math . max ( maxPadding . right , boxPadding . right ) ;
108+ updateMaxPadding ( maxPadding , box . getPadding ( ) ) ;
104109 }
105110
106111 const newWidth = Math . max ( 0 , params . outerWidth - getCombinedMax ( maxPadding , chartArea , 'left' , 'right' ) ) ;
107112 const newHeight = Math . max ( 0 , params . outerHeight - getCombinedMax ( maxPadding , chartArea , 'top' , 'bottom' ) ) ;
108113
109- if ( newWidth !== chartArea . w || newHeight !== chartArea . h ) {
114+ const widthChanged = newWidth !== chartArea . w ;
115+ const heightChanged = newHeight !== chartArea . h ;
116+ if ( widthChanged || heightChanged ) {
110117 chartArea . w = newWidth ;
111118 chartArea . h = newHeight ;
112-
113- // return true if chart area changed in layout's direction
114- return layout . horizontal ? newWidth !== chartArea . w : newHeight !== chartArea . h ;
115119 }
120+
121+ // return booleans on the changes per direction
122+ return layout . horizontal
123+ ? { same : widthChanged , other : heightChanged }
124+ : { same : heightChanged , other : widthChanged } ;
116125}
117126
118127function handleMaxPadding ( chartArea ) {
@@ -158,13 +167,15 @@ function fitBoxes(boxes, chartArea, params) {
158167 layout . height || chartArea . h ,
159168 getMargins ( layout . horizontal , chartArea )
160169 ) ;
161- if ( updateDims ( chartArea , params , layout ) ) {
170+ const { same, other} = updateDims ( chartArea , params , layout ) ;
171+ if ( same && refitBoxes . length ) {
172+ // Dimensions changed and there were non full width boxes before this
173+ // -> we have to refit those
174+ refit = true ;
175+ }
176+ if ( other ) {
177+ // Chart area changed in the opposite direction
162178 changed = true ;
163- if ( refitBoxes . length ) {
164- // Dimensions changed and there were non full width boxes before this
165- // -> we have to refit those
166- refit = true ;
167- }
168179 }
169180 if ( ! box . fullSize ) { // fullSize boxes don't need to be re-fitted in any case
170181 refitBoxes . push ( layout ) ;
@@ -365,7 +376,10 @@ export default {
365376
366377 setLayoutDims ( verticalBoxes . concat ( horizontalBoxes ) , params ) ;
367378
368- // First fit vertical boxes
379+ // First fit the fullSize boxes, to reduce probability of re-fitting.
380+ fitBoxes ( boxes . fullSize , chartArea , params ) ;
381+
382+ // Then fit vertical boxes
369383 fitBoxes ( verticalBoxes , chartArea , params ) ;
370384
371385 // Then fit horizontal boxes
0 commit comments