2020-05-19 12:49:06 +02:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2021-01-07 12:25:14 +01:00
|
|
|
import { LoginService } from '../services/login.service';
|
2020-12-10 11:48:15 +01:00
|
|
|
import { ApiService } from '../services/api.service';
|
2021-01-07 12:25:14 +01:00
|
|
|
import { Chart } from 'chart.js';
|
2020-12-10 11:48:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
interface KeyInterface {
|
|
|
|
id: string;
|
|
|
|
count: number;
|
|
|
|
active: boolean;
|
|
|
|
}
|
2020-05-19 12:49:06 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-home',
|
|
|
|
templateUrl: './home.component.html',
|
|
|
|
styleUrls: ['./home.component.scss']
|
|
|
|
})
|
|
|
|
export class HomeComponent implements OnInit {
|
|
|
|
|
2020-12-10 11:48:15 +01:00
|
|
|
keys: KeyInterface[] = [];
|
|
|
|
isActiveKey: { [key: string]: boolean } = {}; // object to check if key is currently active
|
|
|
|
myChart: Chart;
|
|
|
|
|
2020-08-24 12:43:39 +02:00
|
|
|
constructor(
|
2020-12-10 11:48:15 +01:00
|
|
|
public login: LoginService,
|
|
|
|
public api: ApiService
|
2020-08-24 12:43:39 +02:00
|
|
|
) { }
|
2020-05-19 12:49:06 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-12-10 11:48:15 +01:00
|
|
|
// fetch all available groups
|
|
|
|
this.fetchData('/material/groups', data => this.createGroup(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
// api access with callback
|
|
|
|
async fetchData(URL: string, processor: any) {
|
|
|
|
this.api.get(URL, (sData, err, headers) => {
|
|
|
|
processor(sData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill interface with data
|
|
|
|
createGroup(data: any) {
|
|
|
|
let temp: KeyInterface[] = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < data.length; i++) {
|
2021-01-11 10:35:18 +01:00
|
|
|
temp.push({ id: data[i], count: 0, active: false });
|
2020-12-10 11:48:15 +01:00
|
|
|
}
|
|
|
|
this.keys = temp; // invoke update in rb-multiselect
|
2021-01-11 10:35:18 +01:00
|
|
|
this.initChart();
|
|
|
|
|
2021-01-18 13:39:50 +01:00
|
|
|
// only neccesary if keys get preselected
|
|
|
|
//this.calcFieldSelectKeys();
|
|
|
|
|
2020-12-10 11:48:15 +01:00
|
|
|
// fetch all samples populated with according group
|
2021-01-11 10:35:18 +01:00
|
|
|
this.getSamples();
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:22:42 +01:00
|
|
|
getSamples() {
|
2021-01-11 10:35:18 +01:00
|
|
|
let query = '/samples?status%5B%5D=validated&status=new&filters%5B%5D=%7B%22mode%22%3A%22in%22%2C%22field%22%3A%22material.group%22%2C%22values%22%3A%5B';
|
|
|
|
let temp = '';
|
2021-01-18 13:22:42 +01:00
|
|
|
this.keys.forEach(key => {
|
|
|
|
temp += key.active ? '%22' + key.id.split("%").join("%25") + '%22%2C' : ""; // replace split().join() with replaceAll()
|
2021-01-11 10:35:18 +01:00
|
|
|
});
|
2021-01-18 13:22:42 +01:00
|
|
|
if (temp === '') {
|
2021-01-11 10:35:18 +01:00
|
|
|
this.countSamples('');
|
2021-01-18 13:22:42 +01:00
|
|
|
} else {
|
|
|
|
query = query + temp.substr(0, temp.length - 3) + '%5D%7D&fields%5B%5D=material.group';
|
2021-01-11 10:35:18 +01:00
|
|
|
this.fetchData(query, data => this.countSamples(data));
|
|
|
|
}
|
2020-12-10 11:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// loop through samples and count
|
|
|
|
countSamples(data: any) {
|
2021-01-11 10:35:18 +01:00
|
|
|
this.keys.map(key => key.count = 0);
|
2020-12-10 11:48:15 +01:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
this.keys.forEach(key => {
|
|
|
|
if (key.id === data[i].material.group) {
|
|
|
|
key.count += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-01-18 13:39:50 +01:00
|
|
|
const datasets = this.updateGraph();
|
2020-05-19 12:49:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:48:15 +01:00
|
|
|
// preset select
|
|
|
|
calcFieldSelectKeys() {
|
|
|
|
this.keys.forEach(key => {
|
|
|
|
this.isActiveKey[key.id] = key.active;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// update keys based on select
|
2021-01-11 10:35:18 +01:00
|
|
|
updateGroups(activeKeys: any) {
|
2020-12-10 11:48:15 +01:00
|
|
|
this.keys.forEach(key => {
|
2021-01-11 10:35:18 +01:00
|
|
|
if (activeKeys.hasOwnProperty(key.id)) {
|
|
|
|
key.active = activeKeys[key.id];
|
2020-12-10 11:48:15 +01:00
|
|
|
}
|
|
|
|
});
|
2021-01-11 10:35:18 +01:00
|
|
|
this.getSamples();
|
2020-12-10 11:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get data for graph based on active keys
|
2021-01-18 13:39:50 +01:00
|
|
|
updateGraph() {
|
2020-12-10 11:48:15 +01:00
|
|
|
let nameList: string[] = [];
|
|
|
|
let dataList: number[] = [];
|
|
|
|
|
|
|
|
this.keys.forEach(key => {
|
|
|
|
if (key.active) {
|
|
|
|
nameList.push(key.id);
|
|
|
|
dataList.push(key.count);
|
|
|
|
}
|
|
|
|
})
|
2021-01-18 13:39:50 +01:00
|
|
|
this.myChart.data.labels = nameList;
|
|
|
|
this.myChart.data.datasets[0].data = dataList;
|
|
|
|
this.myChart.update();
|
2020-12-10 11:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw graph
|
|
|
|
async initChart() {
|
2021-01-18 13:22:42 +01:00
|
|
|
this.myChart = new Chart("myChart", {
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
2021-01-18 13:39:50 +01:00
|
|
|
labels: [],
|
2021-01-18 13:22:42 +01:00
|
|
|
datasets: [{
|
|
|
|
label: 'Number of samples per group',
|
2021-01-18 13:39:50 +01:00
|
|
|
data: [],
|
2021-01-18 13:56:47 +01:00
|
|
|
backgroundColor: 'rgba(0, 86, 145, 1)'
|
2021-01-18 13:22:42 +01:00
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
scales: {
|
|
|
|
yAxes: [{
|
|
|
|
ticks: {
|
|
|
|
beginAtZero: true
|
|
|
|
}
|
2020-12-10 11:48:15 +01:00
|
|
|
}]
|
|
|
|
}
|
2021-01-18 13:22:42 +01:00
|
|
|
}
|
|
|
|
});
|
2020-12-10 11:48:15 +01:00
|
|
|
}
|
2020-05-19 12:49:06 +02:00
|
|
|
}
|