Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AG-13951 Fix legend marker rendering when browser zooming #3505

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/ag-charts-community/src/chart/cartesianChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export class CartesianChart extends Chart {
super(options, resources);
}

private firstSeriesTranslation = true;

override onAxisChange(newValue: ChartAxis[], oldValue?: ChartAxis[]) {
super.onAxisChange(newValue, oldValue);

Expand All @@ -61,7 +59,8 @@ export class CartesianChart extends Chart {
override destroySeries(series: Series<unknown, any, any>[]) {
super.destroySeries(series);

this.firstSeriesTranslation = true;
this.lastLayoutWidth = NaN;
this.lastLayoutHeight = NaN;
}

override getChartType() {
Expand Down Expand Up @@ -90,22 +89,23 @@ export class CartesianChart extends Chart {
this.ctx.updateService.dispatchProcessData({ series: { shouldFlipXY: this.shouldFlipXY() } });
}

private lastLayoutWidth = NaN;
private lastLayoutHeight = NaN;
protected performLayout(ctx: LayoutContext) {
const { firstSeriesTranslation, seriesRoot, annotationRoot } = this;
const { seriesRoot, annotationRoot } = this;
const { clipSeries, seriesRect, visible } = this.updateAxes(ctx.layoutBox);

this.seriesRoot.visible = visible;
this.seriesRect = seriesRect;
this.animationRect = ctx.layoutBox;

const { x, y } = seriesRect;
if (firstSeriesTranslation) {
if (ctx.width !== this.lastLayoutWidth || ctx.height !== this.lastLayoutHeight) {
// For initial rendering, don't animate.
for (const group of [seriesRoot, annotationRoot]) {
group.translationX = Math.floor(x);
group.translationY = Math.floor(y);
}
this.firstSeriesTranslation = false;
} else {
// Animate seriesRect movements - typically caused by axis size changes.
const { translationX, translationY } = seriesRoot;
Expand All @@ -120,6 +120,9 @@ export class CartesianChart extends Chart {
);
}

this.lastLayoutWidth = ctx.width;
this.lastLayoutHeight = ctx.height;

// Recreate padding object to prevent issues with getters in `BBox.shrink()`
const seriesPaddedRect = seriesRect.clone().grow(this.seriesArea.padding);

Expand Down
10 changes: 8 additions & 2 deletions packages/ag-charts-community/src/util/sizeMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ export class SizeMonitor {
});
}

// The resize observer should most pixel ratio changes
// with the exception of moving the browser to a monitor with a different scaling
// The resize observer will re-read the pixel ratio
// so make sure this fires after the resize observer to avoid double rendering
let animationFrame: NodeJS.Timeout;
this.pixelRatioObserver = new PixelRatioObserver(() => {
this.checkPixelRatio();
clearTimeout(animationFrame);
animationFrame = setTimeout(() => this.checkPixelRatio(), 0);
});

this.documentReady = getDocument('readyState') === 'complete';
Expand Down Expand Up @@ -84,7 +90,7 @@ export class SizeMonitor {
if (!entry) return;

if (width !== entry.size?.width || height !== entry.size?.height) {
const pixelRatio = entry.size?.pixelRatio ?? this.pixelRatioObserver?.pixelRatio ?? 1;
const pixelRatio = this.pixelRatioObserver?.pixelRatio ?? 1;
entry.size = { width, height, pixelRatio };
entry.cb(entry.size, element);
}
Expand Down