Skip to content

Commit

Permalink
Optimize scrolling event so it doesn't occure too frequently
Browse files Browse the repository at this point in the history
  • Loading branch information
adiletelf committed Sep 23, 2024
1 parent 671e895 commit 237fe57
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/timeLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,17 @@ export class Timeline implements powerbiVisualsApi.extensibility.visual.IVisual

this.addElements();

let ticking = false;
this.rootSelection.on("scroll", (event) => {
const target = event.target as HTMLDivElement;
const scrollLeft: number = target?.scrollLeft || 0;
this.headerSelection.attr("transform", `translate(${scrollLeft}, 0)`);
if (!ticking) {
window.requestAnimationFrame(() => {
const target = event.target as HTMLDivElement;
const scrollLeft: number = target?.scrollLeft || 0;
this.headerSelection.attr("transform", `translate(${scrollLeft}, 0)`);
ticking = false;
});
ticking = true;
}
});
}

Expand Down Expand Up @@ -698,6 +705,7 @@ export class Timeline implements powerbiVisualsApi.extensibility.visual.IVisual


this.adjustHeightOfElements(options.viewport.width);
this.resetScrollPosition();

this.timelineGranularityData = new GranularityData(this.datePeriod.startDate, this.datePeriod.endDate);

Expand Down Expand Up @@ -1275,6 +1283,10 @@ export class Timeline implements powerbiVisualsApi.extensibility.visual.IVisual
.attr("height", this.timelineProperties.legendHeight);
}

private resetScrollPosition(): void {
this.headerSelection.attr("transform", null);
}

private renderGranularityFrame(granularity: GranularityType): void {
d3SelectAll("g." + Timeline.TimelineSelectors.TimelineSlicer.className).remove();

Expand Down Expand Up @@ -1399,9 +1411,6 @@ export class Timeline implements powerbiVisualsApi.extensibility.visual.IVisual

this.renderTimeRangeText(timelineData, settings.rangeHeader);

// reset scroll position
this.headerSelection.attr("transform", null);

this.rootSelection
.attr("drag-resize-disabled", true)
.style("overflow-x", Timeline.DefaultOverflow)
Expand Down

0 comments on commit 237fe57

Please sign in to comment.