Skip to content

Commit 6a12a60

Browse files
fix(core): fix for better backdrop resizing on axis grids
1 parent 4c1d451 commit 6a12a60

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

packages/core/src/base-axis-chart.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// D3 Imports
22
import { select } from "d3-selection";
33
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";
55
import { min, max } from "d3-array";
66

77
import { BaseChart } from "./base-chart";
@@ -10,6 +10,9 @@ import * as Configuration from "./configuration";
1010
import { ChartConfig, AxisChartOptions } from "./configuration";
1111
import { Tools } from "./tools";
1212

13+
// Misc
14+
import ResizeObserver from "resize-observer-polyfill";
15+
1316
export class BaseAxisChart extends BaseChart {
1417
x: ScaleBand<any>;
1518
y: ScaleLinear<any, any>;
@@ -75,6 +78,7 @@ export class BaseAxisChart extends BaseChart {
7578
this.draw();
7679

7780
this.addDataPointEventListener();
81+
this.gridResizeObserver();
7882
}
7983

8084
update() {
@@ -177,6 +181,22 @@ export class BaseAxisChart extends BaseChart {
177181
};
178182
}
179183

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+
180200
resizeChart() {
181201
// Reposition the legend
182202
this.positionLegend();
@@ -189,7 +209,6 @@ export class BaseAxisChart extends BaseChart {
189209
this.repositionYAxisTitle();
190210
}
191211

192-
this.drawBackdrop();
193212
this.dispatchEvent("resize");
194213
}
195214

@@ -487,17 +506,17 @@ export class BaseAxisChart extends BaseChart {
487506
* Draws the background for the chart grid
488507
*/
489508
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();
501520
}
502521

503522
addOrUpdateThresholds(yGrid, animate?) {

0 commit comments

Comments
 (0)