progress-view.component.ts 7.54 KB
Newer Older
1
2
import { Component, OnInit } from '@angular/core';
import {CacheService} from '../cache.service';
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
3
import * as d3 from 'd3';
4
5
6
7
8
9
10

@Component({
  selector: 'app-progress-view',
  templateUrl: './progress-view.component.html',
  styleUrls: ['./progress-view.component.css']
})
export class ProgressViewComponent implements OnInit {
11
  public plot;
12
13
  public data;
  public layout;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
14
  public amountOfCandidates;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
15
  public hover = 0;
16

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
17
  private _sliderValue = 0;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
18

19
20
21
  constructor(private cache: CacheService) { }

  ngOnInit(): void {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
22
23
    // this.cache.onNewSimilarity.subscribe(() => { this.showgraph(); });
    this.cache.onNewTables.subscribe(() => { this.showgraph(); });
24
25
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
26
  averagePlot(averages) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
27
28
29
30
    console.log(averages);
    let highest = 0;
    averages.map((average, i) => {
      if (average.average.length !== 0 && i > highest) {
31
32
        highest = i;
      }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
33
34
    });
    let data = averages.map((average, i) => {
35
36
37
38
39
40
      return [
        {
          x: [...Array(average.average.length).keys()],
          y: average.average,
          type: 'line',
          visible: i === highest,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
41
42
43
44
          line: {
            color: 'red',
            width: 3
          }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        },
        {
          x: [...Array(average.average.length).keys()],
          y: average.max,
          type: 'scatter',
          visible: i === highest,
          fill: null,
          mode: 'lines',
          line: {
            color: 'rgb(55, 128, 191)',
            width: 3
          }
          },
        {
          x: [...Array(average.average.length).keys()],
          y: average.min,
          type: 'scatter',
          visible: i === highest,
          fill: 'tonexty',
          mode: 'lines',
          line: {
            color: 'rgb(55, 128, 191)',
            width: 3
          }
          },
      ];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
71
    });
72
73
    data = [].concat(...data);
    const visibility = Array(averages.length * 3).fill(false);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
74
    const steps = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
75
    for (let i = 0; i < this.cache.nrOfTables; i++) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
76
      const v = visibility.slice();
77
78
79
      v[i * 3 + 2] = true;
      v[i * 3 + 1] = true;
      v[i * 3] = true;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
80
      steps.push({
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
81
        label: (100 * (i + 1) / this.cache.nrOfTables).toString() + '%',
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
82
83
84
85
        method: 'restyle',
        args: ['visible', v]
      });
    }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
86
    this._sliderValue = highest;
87
88
89
90
91
92
93
94
95
96
    this.data = data;
    this.layout = {
      showlegend: false,
      hovermode: 'closest',
      autosize: true,
      margin: {
        l: 50,
        r: 30,
        t: 30,
        pad: 4
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
97
      },
98
      xaxis: {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
99
100
101
        showgrid: false,
        zeroline: false,
        showticklabels: false,
102
103
      },
      yaxis: {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
104
105
        zeroline: false,
        showticklabels: false,
106
107
      },
      height: 300,
108
      width: 300,
109
      sliders: [{
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
110
        active: highest,
111
112
113
114
115
116
117
118
119
120
121
        pad: {t: 30},
        currentvalue: {
          xanchor: 'right',
          prefix: 'Similarity: ',
          font: {
            color: '#888',
            size: 10
          }
        },
        steps
      }]
122
123
124
    };
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  hoverPlot(averages) {
    this.data = averages.map((average, i) => {
      return [
        {
          x: [...Array(average.average.length).keys()],
          y: average.average,
          type: 'line',
          line: {
            color: 'red',
            width: 3
          }
        },
        {
          x: [...Array(average.average.length).keys()],
          y: average.max,
          type: 'scatter',
          fill: null,
          mode: 'lines',
          line: {
            color: 'rgb(55, 128, 191)',
            width: 3
          }
        },
        {
          x: [...Array(average.average.length).keys()],
          y: average.min,
          type: 'scatter',
          fill: 'tonexty',
          mode: 'lines',
          line: {
            color: 'rgb(55, 128, 191)',
            width: 3
          }
        },
      ];
    });
    this.layout = {
      showlegend: false,
      hovermode: 'closest',
      autosize: true,
      margin: {
        l: 50,
        r: 30,
        t: 30,
        pad: 4
      },
      xaxis: {
        showgrid: false,
        zeroline: false,
        showticklabels: false,
      },
      yaxis: {
        zeroline: false,
        showticklabels: false,
      },
      height: 300,
      width: 300,
    };
183
184
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
185
186
187
188
  public set sliderValue(v: number) {
    this._sliderValue = v;
    d3.selectAll('circle').transition().style('stroke', undefined);
    d3.select('#node-' + v).transition().style('stroke', 'black').style('stroke-width', 20);
189
  }
190

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
191
192
  public get sliderValue(): number {
    return this._sliderValue;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
193
194
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
195
196
197
198
199
200
  public get maxLength(): number {
    return Object.keys(this.similarity).length;
}

  public get similarity() {
    return this.cache.tables;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
201
  }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
202

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  async showgraph() {
    const nodes = [];
    const links = [];
    const temp = (await this.cache.getAverageWindows(Object.values(this.similarity)));
    const keys = Object.keys(this.similarity);
    console.log(temp);
    this.hoverPlot(temp.averages);
    const distances = temp.distances;
    console.log(this.similarity);
    for (const key in this.similarity) {
      const size = this.similarity[key].length;
      nodes.push({id: key, group: Number(key), size: size});
    }
    for (const key in this.similarity) {
      for (const key2 in this.similarity) {
        if (key === key2) {
          continue;
        }
        links.push({source: key, target: key2, value: 0.001 * (100 - 5 * distances[keys.indexOf(key)][keys.indexOf(key2)])});
      }
    }


    const graph = {nodes, links};

    console.log(graph);

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
230
    console.log('building');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
231
    const svg = d3.select('#visual');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
232
233
234
235
236
237
238
239
240
    const width = +svg.attr('width');
    const height = +svg.attr('height');

    const color = d3.scaleOrdinal(d3.schemeCategory10);

    const simulation = d3.forceSimulation()
      .force('link', d3.forceLink().id((d: any) => d.id))
      .force('charge', d3.forceManyBody().strength(100)) // Gravity force
      .force('collide', d3.forceCollide().radius(25).iterations(3)) // Repulsion force
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
241
      .force('center', d3.forceCenter(width / 2, height / 2)); // Position force
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
242

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    const link = svg.append('g')
      .selectAll('line')
      .data(graph.links)
      .enter().append('line')
      .attr('stroke', 'grey')
      .attr('stroke-width', (d: any) => d.value);

    const node = svg.append('g')
      .selectAll('circle')
      .data(graph.nodes)
      .enter().append('circle')
      .attr('r', (d: any) => 5 * Math.log(d.size) / Math.log(10))
      .attr('fill', (d: any) => this.getColor(d.group / graph.nodes.length))
      .attr('id', (d: any) => 'node-' + d.group)
      .on('mouseover', (d: any) => {this.sliderValue = d.group; })
      .call(d3.drag()
        .on('start', dragstarted)
        .on('drag', dragged)
        .on('end', dragended));

    simulation
      .nodes(graph.nodes as any)
      .on('tick', ticked);

    simulation.force<any>('link')
      .links(graph.links);

    function ticked() {
      link
        .attr('x1', (d: any) => d.source.x)
        .attr('y1', (d: any) => d.source.y)
        .attr('x2', (d: any) => d.target.x)
        .attr('y2', (d: any) => d.target.y);

      node
        .attr('cx', (d: any) => d.x)
        .attr('cy', (d: any) => d.y);
    }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302

    function dragstarted(d) {
      if (!d3.event.active) {
        simulation.alphaTarget(0.1).restart();
      }
      d.fx = d.x;
      d.fy = d.y;
    }

    function dragged(d) {
      d.fx = d3.event.x;
      d.fy = d3.event.y;
    }

    function dragended(d) {
      if (!d3.event.active) {
        simulation.alphaTarget(0);
      }
      d.fx = null;
      d.fy = null;
    }
  }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
303
304
305
306
  getColor(value) {
    const hue=((1-value)*120).toString(10);
    return ["hsl(",hue,",100%,50%)"].join("");
  }
307
}