import { Component, OnInit } from '@angular/core'; import {CacheService} from '../cache.service'; import * as d3 from 'd3'; @Component({ selector: 'app-progress-view', templateUrl: './progress-view.component.html', styleUrls: ['./progress-view.component.css'] }) export class ProgressViewComponent implements OnInit { public plot; public data; public layout; public amountOfCandidates; private _sliderValue; constructor(private cache: CacheService) { } ngOnInit(): void { this.showgraph(); this.cache.onNewSimilarity.subscribe(() => { this.initializeInfo(); }); } averagePlot(averages) { console.log(averages); let highest = 0; averages.map((average, i) => { if (average.average.length !== 0 && i > highest) { highest = i; } }); let data = averages.map((average, i) => { return [ { x: [...Array(average.average.length).keys()], y: average.average, type: 'line', visible: i === highest, line: { color: 'red', width: 3 } }, { 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 } }, ]; }); data = [].concat(...data); const visibility = Array(averages.length * 3).fill(false); const steps = []; for (let i = 0; i < this.cache.nrOfTables; i++) { const v = visibility.slice(); v[i * 3 + 2] = true; v[i * 3 + 1] = true; v[i * 3] = true; steps.push({ label: (100 * (i + 1) / this.cache.nrOfTables).toString() + '%', method: 'restyle', args: ['visible', v] }); } this._sliderValue = highest; this.data = data; 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, sliders: [{ active: highest, pad: {t: 30}, currentvalue: { xanchor: 'right', prefix: 'Similarity: ', font: { color: '#888', size: 10 } }, steps }] }; } async initializeInfo(): Promise { console.log('Updating progress view'); console.log(this.similarity); const allWindows = []; const keys = Object.keys(this.similarity); for (let i = this.cache.nrOfTables; i >= 1; i--) { if (keys.indexOf(i.toString()) === -1) { allWindows.push([]); } else { allWindows.push(this.similarity[i.toString()]); } } const averages = await this.cache.getAverageProgressWindows(allWindows); this.plot = this.averagePlot(averages); let candidates = []; for (let i = this._sliderValue; i < this.cache.nrOfTables; i++) { candidates = candidates.concat(this.cache.windowSimilarity[i.toString()]); } this.amountOfCandidates = candidates.length; } public get similarity() { return this.cache.windowSimilarity; } public setSliderValue(v) { this._sliderValue = v.slider.active; let candidates = []; for (let i = this._sliderValue; i < this.cache.nrOfTables; i++) { candidates = candidates.concat(this.cache.windowSimilarity[i.toString()]); } this.amountOfCandidates = candidates.length; } public showCandidates() { this.cache.sliderValue = this._sliderValue; } public showgraph() { console.log('building'); const svg = d3.select('svg'); 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 .force('center', d3.forceCenter(width / 2, height / 2));// Position force d3.json('assets/miserables.json') .catch((error) => { throw error; }) .then((graph) => { const link = svg.append('g') .selectAll('line') .data(graph.links) .enter().append('line') .attr('stroke', 'blue') .attr('fill', 'purple') .attr('stroke-width', (d: any) => d.value * .08); const node = svg.append('g') .selectAll('circle') .data(graph.nodes) .enter().append('circle') .attr('r', 5) .attr('fill', (d: any) => color(d.group)) .call(d3.drag() .on('start', dragstarted) .on('drag', dragged) .on('end', dragended)); simulation .nodes(graph.nodes) .on('tick', ticked); simulation.force('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); } }); 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; } } }