Skip to content

Commit 238fcc9

Browse files
committed
Creating a memory-leak example on demo
1 parent 72de7fb commit 238fcc9

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

src/app/demo/demo.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ <h5>Angular Plotly</h5>
1111
<li><a [routerLink]="['/linear-charts']">Linear Charts</a></li>
1212
<li><a [routerLink]="['/ajax']">Ajax</a></li>
1313
<li><a [routerLink]="['/fancy-plot']">Fancy Plot</a></li>
14+
<li><a [routerLink]="['/memory-leak']">Memory Leak</a></li>
1415
</ul>
1516
</div>
1617
</div>

src/app/demo/demo.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { BoxPlotComponent } from './box-plots/box-plots.component';
1515
import { LinearChartsComponent } from './linear-charts/linear-charts.component';
1616
import { AjaxComponent } from './ajax/ajax.component';
1717
import { FancyplotComponent } from './fancyplot/fancyplot.component';
18+
import { MemoryLeakComponent } from './memory-leak/memory-leak.component';
1819

1920

2021
const demoRoutes: Routes = [
@@ -23,6 +24,7 @@ const demoRoutes: Routes = [
2324
{ path: 'linear-charts', component: LinearChartsComponent, data: { title: 'Linear Charts' } },
2425
{ path: 'ajax', component: AjaxComponent, data: { title: 'Ajax' } },
2526
{ path: 'fancy-plot', component: FancyplotComponent, data: { title: 'Fancy Plot' } },
27+
{ path: 'memory-leak', component: MemoryLeakComponent, data: { title: 'Memory leak' } },
2628

2729
{ path: '', redirectTo: '/home', pathMatch: 'full' },
2830
];
@@ -36,7 +38,7 @@ const demoRoutes: Routes = [
3638
// PlotlyViaWindowModule,
3739
RouterModule.forRoot(demoRoutes, { enableTracing: true }),
3840
],
39-
declarations: [HomeComponent, DemoComponent, BoxPlotComponent, LinearChartsComponent, AjaxComponent, FancyplotComponent],
41+
declarations: [HomeComponent, DemoComponent, BoxPlotComponent, LinearChartsComponent, AjaxComponent, FancyplotComponent, MemoryLeakComponent],
4042
exports: [DemoComponent],
4143
})
4244
export class DemoModule { }

src/app/demo/memory-leak/memory-leak.component.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div>
2+
<p>
3+
This page was created to target a memory leak issue reported on:
4+
<a href="https://github.com/plotly/angular-plotly.js/issues/28" target="_blank">https://github.com/plotly/angular-plotly.js/issues/28</a>
5+
</p>
6+
7+
<plotly-plot [data]="lines" [layout]="layout" [revision]="0" [debug]="debug" [useResizeHandler]="useResizeHandler"></plotly-plot>
8+
9+
<p>&nbsp;</p>
10+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'plotly-memory-leak',
5+
templateUrl: './memory-leak.component.html',
6+
styleUrls: ['./memory-leak.component.css']
7+
})
8+
export class MemoryLeakComponent implements OnInit {
9+
public debug = true;
10+
public useResizeHandler = true;
11+
12+
public num_lines = 4;
13+
public timespan = 10;
14+
public t_int = 0.01;
15+
public update_interval = 0.2;
16+
17+
public lines = [];
18+
public t = 0;
19+
20+
public data: any[] = [
21+
{ x: [1, 2, 3, 4], y: [10, 15, 13, 17], type: 'bar', mode: 'markers', name: 'Bar' },
22+
{ x: [2, 3, 4, 1], y: [16, 5, 11, 9], type: 'scattergl', mode: 'lines', name: 'Lines' },
23+
{ x: [1, 2, 3, 4], y: [12, 9, 15, 12], type: 'markers', mode: 'lines+markers', name: 'Scatter + Lines' },
24+
];
25+
26+
public layout: any = {
27+
title: 'Adding Names to Line and Scatter Plot',
28+
width: 480,
29+
height: 400,
30+
};
31+
32+
public ngOnInit() {
33+
for (let i = 0; i < this.num_lines; ++i) {
34+
this.lines.push({
35+
x: [],
36+
y: [],
37+
type: 'scatter'
38+
});
39+
40+
for (this.t = 0; this.t < this.timespan; this.t += this.t_int) {
41+
this.lines[i].x.push(this.t);
42+
this.lines[i].y.push(2 * i + Math.random());
43+
}
44+
}
45+
46+
setInterval(this.tick.bind(this), this.update_interval * 1000);
47+
}
48+
49+
public tick() {
50+
const n = this.update_interval / this.t_int;
51+
const update = { x: [], y: [] };
52+
53+
for (let i = 0; i < this.num_lines; ++i) {
54+
this.lines[i].x = this.lines[i].x.slice(n);
55+
this.lines[i].y = this.lines[i].y.slice(n);
56+
}
57+
58+
const t_init = this.t;
59+
for (let i = 0; i < this.num_lines; ++i) {
60+
this.t = t_init;
61+
for (let j = 0; j < n; ++j, this.t += this.t_int) {
62+
this.lines[i].x.push(this.t);
63+
this.lines[i].y.push(2 * i + Math.random());
64+
}
65+
66+
update.x.push(this.lines[i].x);
67+
update.y.push(this.lines[i].y);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)