Skip to content

Commit e9a2190

Browse files
committed
fix: apply data and layout changes made while the plot is initializing
1 parent c8051b8 commit e9a2190

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎projects/plotly/src/lib/plotly.component.spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,27 @@ describe('PlotlyComponent', () => {
229229
expect(component.plotlyInstance).toBeUndefined();
230230
});
231231

232+
it('should apply data changes made while the plot is initializing', async () => {
233+
const instance = document.createElement('div') as any;
234+
instance.on = () => undefined;
235+
let resolvePlot!: (value: any) => void;
236+
spyOn(component.plotly, 'newPlot').and.returnValue(new Promise(resolve => resolvePlot = resolve));
237+
spyOn(component, 'updatePlot');
238+
239+
componentRef.setInput('data', [{ y: [10, 15, 13, 17], type: 'box' }]);
240+
component.ngDoCheck();
241+
const pendingPlot = component.createPlot();
242+
243+
componentRef.setInput('data', [{ y: [11, 15, 13, 17], type: 'box' }]);
244+
component.ngDoCheck();
245+
expect(component.updatePlot).not.toHaveBeenCalled();
246+
247+
resolvePlot(instance);
248+
await pendingPlot;
249+
250+
expect(component.updatePlot).toHaveBeenCalledTimes(1);
251+
});
252+
232253
it('should fail when plotlyInstance is undefined', () => {
233254
component.plotlyInstance = undefined;
234255

‎projects/plotly/src/lib/plotly.component.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
4242
public layoutDiffer?: KeyValueDiffer<string, any>;
4343
public dataDiffer?: IterableDiffer<Plotly.Data>;
4444
private destroyed = false;
45+
private pendingUpdate = false;
4546

4647
@ViewChild('plot', { static: true }) plotEl!: ElementRef;
4748

@@ -208,8 +209,12 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
208209
}
209210
}
210211

211-
if (shouldUpdate && this.plotlyInstance) {
212-
this.updatePlot();
212+
if (shouldUpdate) {
213+
if (this.plotlyInstance) {
214+
this.updatePlot();
215+
} else {
216+
this.pendingUpdate = true;
217+
}
213218
}
214219
}
215220

@@ -263,6 +268,11 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
263268
});
264269

265270
this.updateWindowResizeHandler();
271+
272+
if (this.pendingUpdate) {
273+
this.pendingUpdate = false;
274+
void this.updatePlot();
275+
}
266276
}).catch(err => {
267277
console.error('Error while plotting:', err);
268278
this.error.emit(err);

0 commit comments

Comments
 (0)