@@ -8,10 +8,14 @@ import {
8
8
import { scaleOrdinal } from "d3-scale" ;
9
9
import { transition , Transition } from "d3-transition" ;
10
10
11
+ // Internal Imports
11
12
import * as Configuration from "./configuration" ;
12
13
import { Tools } from "./tools" ;
13
14
import PatternsService from "./services/patterns" ;
14
15
16
+ // Misc
17
+ import ResizeObserver from "resize-observer-polyfill" ;
18
+
15
19
export class BaseChart {
16
20
static chartCount = 1 ;
17
21
@@ -210,8 +214,6 @@ export class BaseChart {
210
214
211
215
// TODO - Refactor
212
216
getChartSize ( container = this . container ) {
213
- const noAxis = this . options . type === "pie" || this . options . type === "donut" ;
214
-
215
217
let ratio , marginForLegendTop ;
216
218
if ( container . node ( ) . clientWidth > Configuration . charts . widthBreak ) {
217
219
ratio = Configuration . charts . magicRatio ;
@@ -222,24 +224,20 @@ export class BaseChart {
222
224
}
223
225
224
226
// Store computed actual size, to be considered for change if chart does not support axis
225
- const marginsToExclude = noAxis ? 0 : ( Configuration . charts . margin . left + Configuration . charts . margin . right ) ;
227
+ const marginsToExclude = 0 ;
226
228
const computedChartSize = {
227
229
height : container . node ( ) . clientHeight - marginForLegendTop ,
228
230
width : ( container . node ( ) . clientWidth - marginsToExclude ) * ratio
229
231
} ;
230
232
231
233
// If chart is of type pie or donut, width and height should equal to the min of the width and height computed
232
- if ( noAxis ) {
233
- let maxSizePossible = Math . min ( computedChartSize . height , computedChartSize . width ) ;
234
- maxSizePossible = Math . max ( maxSizePossible , Configuration . pie . minWidth ) ;
235
-
236
- return {
237
- height : maxSizePossible ,
238
- width : maxSizePossible
239
- } ;
240
- }
234
+ let maxSizePossible = Math . min ( computedChartSize . height , computedChartSize . width ) ;
235
+ maxSizePossible = Math . max ( maxSizePossible , Configuration . charts . minWidth ) ;
241
236
242
- return computedChartSize ;
237
+ return {
238
+ height : maxSizePossible ,
239
+ width : maxSizePossible
240
+ } ;
243
241
}
244
242
245
243
/*
@@ -299,21 +297,23 @@ export class BaseChart {
299
297
resizeWhenContainerChange ( ) {
300
298
let containerWidth = this . holder . clientWidth ;
301
299
let containerHeight = this . holder . clientHeight ;
302
- const frame = ( ) => {
303
- if ( Math . abs ( containerWidth - this . holder . clientWidth ) > 1
304
- || Math . abs ( containerHeight - this . holder . clientHeight ) > 1 ) {
305
- containerWidth = this . holder . clientWidth ;
306
- containerHeight = this . holder . clientHeight ;
307
300
308
- selectAll ( ".legend-tooltip" ) . style ( "display" , "none" ) ;
301
+ const resizeObserver = new ResizeObserver ( ( entries , observer ) => {
302
+ for ( const entry of entries ) {
303
+ if ( Math . abs ( containerWidth - this . holder . clientWidth ) > 1
304
+ || Math . abs ( containerHeight - this . holder . clientHeight ) > 1 ) {
305
+ containerWidth = this . holder . clientWidth ;
306
+ containerHeight = this . holder . clientHeight ;
309
307
310
- this . hideTooltip ( ) ;
311
- this . resizeChart ( ) ;
312
- }
308
+ selectAll ( ".legend-tooltip" ) . style ( "display" , "none" ) ;
313
309
314
- requestAnimationFrame ( frame ) ;
315
- } ;
316
- requestAnimationFrame ( frame ) ;
310
+ this . hideTooltip ( ) ;
311
+ this . resizeChart ( ) ;
312
+ }
313
+ }
314
+ } ) ;
315
+
316
+ resizeObserver . observe ( this . holder ) ;
317
317
}
318
318
319
319
setClickableLegend ( ) {
0 commit comments