Skip to content

Commit d3ca33e

Browse files
mariohmolclaude
andcommitted
fix: redraw chart on window resize to correct zoom-related misalignment
When the browser zoom level changes, the browser fires a resize event but jsgantt-improved does not redraw the chart, causing task bars and date headers to visually misalign. Add a debounced window:resize @HostListener that redraws the Gantt chart 200 ms after the last resize event. Also implement ngOnDestroy to cancel any pending timer on component teardown. Closes #29 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b8f2989 commit d3ca33e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

ng-gantt/src/gantt/gantt.component.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
Component, ElementRef, Input, OnInit, ViewChild
2+
Component, ElementRef, HostListener, Input, OnDestroy, OnInit, ViewChild
33
} from '@angular/core';
44
// import { JSGantt } from 'jsgantt-improved';
55
import * as JSGantt from 'jsgantt-improved';
@@ -11,11 +11,12 @@ import { GanttEditorOptions } from './gantt.editoroptions';
1111
standalone: false,
1212
template: '<div [id]="id" #ganttEditorContainer></div>'
1313
})
14-
export class GanttEditorComponent implements OnInit {
14+
export class GanttEditorComponent implements OnInit, OnDestroy {
1515
private editor: any;
1616
public id = 'anggantteditor' + Math.floor(Math.random() * 1000000);
1717
public optionsChanged = false;
1818
public formats = ['Hour', 'Day', 'Week', 'Month', 'Quarter'];
19+
private _resizeTimer: any;
1920

2021
@ViewChild('ganttEditorContainer', { static: true }) ganttEditorContainer: ElementRef;
2122

@@ -89,6 +90,21 @@ export class GanttEditorComponent implements OnInit {
8990
// this.editor.destroy();
9091
}
9192

93+
ngOnDestroy() {
94+
clearTimeout(this._resizeTimer);
95+
}
96+
97+
@HostListener('window:resize')
98+
onWindowResize() {
99+
clearTimeout(this._resizeTimer);
100+
this._resizeTimer = setTimeout(() => {
101+
if (this.editor) {
102+
this.destroy();
103+
this.ngOnInit();
104+
}
105+
}, 200);
106+
}
107+
92108
public getEditor(){
93109
return this.editor;
94110
}

0 commit comments

Comments
 (0)