optimized charts rendering

This commit is contained in:
2026-11-15 00:03:52 +01:00
parent 739669021b
commit 23b112beb9
@@ -0,0 +1,58 @@
import { Directive, ElementRef, inject } from '@angular/core';
const PASSIVE_WHEEL_SELECTOR = '[data-passive-wheel]';
let wheelPatchInstalled = false;
/**
* Chrome flags non-passive `wheel`/`mousewheel` listeners as scroll-blocking.
* zrender (ECharts) mounts them on the chart's inner viewport without listener
* options. The hierarchy tree only uses `roam: 'move'` and never calls
* `preventDefault()` on wheel events, so marking them passive is safe.
*
* The patch is global but only affects wheel listeners inside an element that
* carries this directive.
*/
function installPassiveWheelPatch(): void {
if (wheelPatchInstalled) return;
wheelPatchInstalled = true;
const nativeAddEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (
this: EventTarget,
type: string,
listener: EventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void {
const isWheelEvent = type === 'wheel' || type === 'mousewheel';
const hasOptions = options !== undefined && typeof options !== 'boolean';
if (
isWheelEvent &&
!hasOptions &&
this instanceof Element &&
this.closest(PASSIVE_WHEEL_SELECTOR)
) {
nativeAddEventListener.call(this, type, listener, {
capture: options === true,
passive: true,
});
return;
}
nativeAddEventListener.call(this, type, listener, options);
};
}
@Directive({
selector: '[appPassiveWheel]',
standalone: true,
})
export class PassiveWheelDirective {
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);
constructor() {
this.host.nativeElement.setAttribute('data-passive-wheel', '');
installPassiveWheelPatch();
}
}