|
| 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