1
1
// D3 Imports
2
2
import { select } from "d3-selection" ;
3
3
import { scaleBand , scaleLinear , ScaleBand , ScaleLinear } from "d3-scale" ;
4
- import { axisBottom , axisLeft , axisRight , AxisScale , AxisDomain } from "d3-axis" ;
4
+ import { axisBottom , axisLeft , axisRight } from "d3-axis" ;
5
5
import { min , max } from "d3-array" ;
6
6
7
7
import { BaseChart } from "./base-chart" ;
@@ -10,6 +10,9 @@ import * as Configuration from "./configuration";
10
10
import { ChartConfig , AxisChartOptions } from "./configuration" ;
11
11
import { Tools } from "./tools" ;
12
12
13
+ // Misc
14
+ import ResizeObserver from "resize-observer-polyfill" ;
15
+
13
16
export class BaseAxisChart extends BaseChart {
14
17
x : ScaleBand < any > ;
15
18
y : ScaleLinear < any , any > ;
@@ -75,6 +78,7 @@ export class BaseAxisChart extends BaseChart {
75
78
this . draw ( ) ;
76
79
77
80
this . addDataPointEventListener ( ) ;
81
+ this . gridResizeObserver ( ) ;
78
82
}
79
83
80
84
update ( ) {
@@ -177,6 +181,22 @@ export class BaseAxisChart extends BaseChart {
177
181
} ;
178
182
}
179
183
184
+ /**
185
+ * Adds a resize observer to the x and y grid and resizes the backdrop on change
186
+ */
187
+ gridResizeObserver ( ) {
188
+ const gridWidth = this . holder . querySelector ( ".y.grid" ) ;
189
+ const gridHeight = this . holder . querySelector ( ".x.grid" ) ;
190
+
191
+ const resizeObserver = new ResizeObserver ( ( entries ) => {
192
+ this . drawBackdrop ( ) ;
193
+ } ) ;
194
+
195
+ // watch resizing of both grids
196
+ resizeObserver . observe ( gridWidth ) ;
197
+ resizeObserver . observe ( gridHeight ) ;
198
+ }
199
+
180
200
resizeChart ( ) {
181
201
// Reposition the legend
182
202
this . positionLegend ( ) ;
@@ -189,7 +209,6 @@ export class BaseAxisChart extends BaseChart {
189
209
this . repositionYAxisTitle ( ) ;
190
210
}
191
211
192
- this . drawBackdrop ( ) ;
193
212
this . dispatchEvent ( "resize" ) ;
194
213
}
195
214
@@ -487,17 +506,17 @@ export class BaseAxisChart extends BaseChart {
487
506
* Draws the background for the chart grid
488
507
*/
489
508
drawBackdrop ( ) {
490
- // Get height from the grid
491
- const xGridHeight = this . innerWrap . select ( ".x.grid" ) . node ( ) . getBBox ( ) . height ;
492
- const yGridBBox = this . innerWrap . select ( ".y.grid" ) . node ( ) . getBBox ( ) ;
493
- const backdrop = Tools . appendOrSelect ( this . innerWrap , "rect.chart-grid-backdrop" ) ;
494
-
495
- backdrop
496
- . attr ( "x" , yGridBBox . x )
497
- . attr ( "y" , yGridBBox . y )
498
- . attr ( "width" , yGridBBox . width )
499
- . attr ( "height" , xGridHeight )
500
- . lower ( ) ;
509
+ // Get height from the grid
510
+ const xGridHeight = this . innerWrap . select ( ".x.grid" ) . node ( ) . getBBox ( ) . height ;
511
+ const yGridBBox = this . innerWrap . select ( ".y.grid" ) . node ( ) . getBBox ( ) ;
512
+ const backdrop = Tools . appendOrSelect ( this . innerWrap , "rect.chart-grid-backdrop" ) ;
513
+
514
+ backdrop
515
+ . attr ( "x" , yGridBBox . x )
516
+ . attr ( "y" , yGridBBox . y )
517
+ . attr ( "width" , yGridBBox . width )
518
+ . attr ( "height" , xGridHeight )
519
+ . lower ( ) ;
501
520
}
502
521
503
522
addOrUpdateThresholds ( yGrid , animate ?) {
0 commit comments