definma-ui/src/app/home/home.component.ts

138 lines
3.4 KiB
TypeScript
Raw Normal View History

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));
2021-01-07 12:25:14 +01:00
//console.log(this.login.username);
2020-12-10 11:48:15 +01:00
}
// 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-07 12:25:14 +01:00
temp.push({ id: data[i], count: 0, active: true });
2020-12-10 11:48:15 +01:00
}
this.keys = temp; // invoke update in rb-multiselect
this.calcFieldSelectKeys();
// fetch all samples populated with according group
2021-01-07 12:25:14 +01:00
this.fetchData('/samples?status%5B%5D=validated&status=new&sort=_id-asc&fields%5B%5D=material.group', data => this.countSamples(data));
2020-12-10 11:48:15 +01:00
}
// loop through samples and count
countSamples(data: any) {
for (var i = 0; i < data.length; i++) {
this.keys.forEach(key => {
if (key.id === data[i].material.group) {
key.count += 1;
}
});
}
this.initChart();
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
updateGroups(event: any) {
this.keys.forEach(key => {
if (event.hasOwnProperty(key.id)) {
key.active = event[key.id];
}
});
this.myChart.destroy();
this.initChart();
}
// get data for graph based on active keys
async getData() {
let nameList: string[] = [];
let dataList: number[] = [];
this.keys.forEach(key => {
if (key.active) {
nameList.push(key.id);
dataList.push(key.count);
}
})
2021-01-07 12:25:14 +01:00
return { names: nameList, count: dataList };
2020-12-10 11:48:15 +01:00
}
// draw graph
async initChart() {
const data = await this.getData();
const canvas = document.getElementById('myChart') as HTMLCanvasElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const ctx = canvas.getContext('2d');
var img = new Image(width, height);
img.src = "/assets/imgs/supergraphic.svg";
img.onload = () => {
const fillPattern = ctx.createPattern(img, 'no-repeat');
this.myChart = new Chart("myChart", {
type: 'line',
data: {
labels: data.names,
datasets: [{
label: 'Number of samples per group',
2021-01-07 12:25:14 +01:00
data: data.count,
2020-12-10 11:48:15 +01:00
backgroundColor: fillPattern,
2021-01-07 12:25:14 +01:00
pointRadius: 4,
pointHoverRadius: 7
2020-12-10 11:48:15 +01:00
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
}
2020-05-19 12:49:06 +02:00
}