overview-window.component.ts 2.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import { CacheService } from '../cache.service';
import {throwError} from 'rxjs';

@Component({
  selector: 'app-overview-window',
  templateUrl: './overview-window.component.html',
  styleUrls: ['./overview-window.component.css']
})
export class OverviewWindowComponent implements OnInit {
  public defaultColors: string[] = [];
  public defaultSizes: number[] = [];
  public defaultOpacity: number[] = [];
  public selectedIndex: number;
  public showPlot = false;

  public data;
  public layout;

  constructor(private service: CacheService) { }

  async ngOnInit(): Promise<void> {
    this.service.onNewWindows.subscribe(() => { this.initializePlot(); });
    this.service.onNewTables.subscribe(() => {
      if (this.selectedIndex) {
        this.updatePlot();
      }
    });
  }

  async initializePlot() {
    this.selectedIndex = undefined;
    for (const _ of this.service.rawValues) {
      this.defaultColors.push('#a3a7e4');
      this.defaultSizes.push(5);
      this.defaultOpacity.push(1);
    }
    this.data = [{
      x: this.service.rawIndices,
      y: this.service.rawValues,
      type: 'scatter',
      mode: 'markers',
      marker: {
        size: this.defaultSizes.slice(),
        color: this.defaultColors.slice()}
    }];
    this.layout = {
      hovermode: 'closest',
      autosize: true,
      margin: {
        l: 0,
        r: 0,
        b: 40,
        t: 0,
        pad: 4
      },
57
58
      height: 200,
      width: screen.width * 0.7
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    };
    this.showPlot = true;
  }

  async clicked(clickData) {
    for (const point of clickData.points) {
      this.selectedIndex = point.pointNumber;
    }
    await this.updatePlot();
  }

  async updatePlot() {
    const colors: string[] = [];
    const sizes: number[]  = [];
    const opacity: number[]  = [];

    const windowSimilarity = await this.service.getSimilarWindows(this.service.windows[this.selectedIndex]);
    for (const frequency in windowSimilarity){
      for (const index of windowSimilarity[frequency]) {
        colors[index] = this.getColor(Number(frequency) / this.service.nrOfTables);
        sizes[index] = (Number(frequency) / this.service.nrOfTables) * 10;
        opacity[index] = Number(frequency) / this.service.nrOfTables;
      }
    }
    this.data[0].marker.color = colors;
  }

  public getColor(value: number) {
    // value from 0 to 1
    const hueValue = Math.min((1 - value) , 1);
    const hue = ((1 - hueValue) * 120).toString(10);
    const sat = value * 100;
    return `hsl(${hue}, ${sat}%, 50%)`;
  }
}