Moved graph to own component

This commit is contained in:
Mats Paul Tončik
2026-05-30 23:53:40 +02:00
parent d323b75e12
commit af39d73ba6
11 changed files with 311 additions and 311 deletions
@@ -1,9 +1,4 @@
<cdk-virtual-scroll-viewport
[itemSize]="52"
[maxBufferPx]="1200"
[minBufferPx]="400"
class="table"
>
<div class="table">
<table mat-table [dataSource]="dataSource" [trackBy]="trackBy" matSort>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>ID</th>
@@ -16,7 +11,7 @@
</td>
</ng-container>
<ng-container matColumnDef="count">
<th mat-header-cell *matHeaderCellDef>Häufigkeit</th>
<th mat-header-cell *matHeaderCellDef mat-sort-header>Häufigkeit</th>
<td mat-cell *matCellDef="let element">
{{ element.works.length }}
</td>
@@ -43,7 +38,7 @@
[cdkDetailRowTpl]="tpl"
></tr>
</table>
</cdk-virtual-scroll-viewport>
</div>
<ng-template #tpl let-element>
<div class="mat-row detail-row" [@detailExpand] style="overflow: hidden">
@@ -6,12 +6,11 @@ import {
ViewChild,
} from '@angular/core';
import { CollaborationNetwork } from '../../api';
import { Router, RouterModule } from '@angular/router';
import { RouterModule } from '@angular/router';
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { MatSort, MatSortModule } from '@angular/material/sort';
import { CdkDetailRowDirective } from '../../directives/cdk-detail-row.directive';
@@ -30,7 +29,6 @@ type Collaborator = {
@Component({
selector: 'app-person-network-collaborators',
imports: [
ScrollingModule,
MatTableModule,
MatIconModule,
MatSortModule,
@@ -53,21 +51,47 @@ export class PersonNetworkCollaboratorsComponent implements AfterViewInit {
@ViewChild(MatSort) sort!: MatSort;
constructor(private readonly router: Router) {}
constructor() {
effect(() => {
const collaborationNetwork = this.collaborationNetwork();
this.dataSource = this.buildCollaboratorsDataSource(collaborationNetwork);
this.dataSource.sortingDataAccessor = (item, property) => {
if (property === 'count') {
return item.works.length;
}
return (item as Record<string, unknown>)[property] as string | number;
};
this.dataSource.filterPredicate = (data, filter) => {
const value = filter.trim().toLowerCase();
return (
data.id.toLowerCase().includes(value) ||
data.name.toLowerCase().includes(value)
);
};
if (this.sort) {
this.dataSource.sort = this.sort;
this.setDefaultSort();
}
const currentSearchTerm = this.searchTerm().trim().toLowerCase();
this.dataSource.filter = currentSearchTerm;
});
effect(() => {
const searchTerm = this.searchTerm();
const normalizedTerm = searchTerm.trim().toLowerCase();
this.dataSource.filter = normalizedTerm;
});
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
onCollaborationNetworkChange = effect(() => {
const collaborationNetwork = this.collaborationNetwork();
this.dataSource = this.buildCollaboratorsDataSource(collaborationNetwork);
});
onSearchTermChange = effect(() => {
const searchTerm = this.searchTerm();
this.dataSource.filter = searchTerm.trim().toLowerCase();
});
private setDefaultSort(): void {
this.sort.active = 'name';
this.sort.direction = 'asc';
this.sort.sortChange.emit({ active: 'name', direction: 'asc' });
}
buildCollaboratorsDataSource(
collaborationNetwork: CollaborationNetwork,
@@ -1 +1,16 @@
<p>person-network-graph works!</p>
<mat-card appearance="outlined">
<mat-card-header>
<mat-card-title
>Kollaborations Netzwerk von {{ authorDisplayName }}</mat-card-title
>
</mat-card-header>
<mat-card-content>
<div
class="graph"
echarts
[options]="options"
[autoResize]="true"
(wheel)="onGraphWheel($event)"
></div>
</mat-card-content>
</mat-card>
@@ -0,0 +1,25 @@
:host {
display: flex;
width: 100%;
}
mat-card {
display: flex;
flex-direction: column;
width: 100%;
}
mat-card-content {
flex: 1 1 auto;
min-height: 0;
}
.graph {
width: 100%;
height: 100%;
min-height: 400px;
overscroll-behavior: contain;
touch-action: none;
user-select: none;
cursor: grab;
}
@@ -1,23 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { PersonNetworkGraphComponent } from './person-network-graph.component';
describe('PersonNetworkGraphComponent', () => {
let component: PersonNetworkGraphComponent;
let fixture: ComponentFixture<PersonNetworkGraphComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [PersonNetworkGraphComponent]
})
.compileComponents();
fixture = TestBed.createComponent(PersonNetworkGraphComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -1,11 +1,136 @@
import { Component } from '@angular/core';
import { Component, effect, input } from '@angular/core';
import { MatCardModule } from '@angular/material/card';
import { CollaborationNetwork } from '../../api';
import { NgxEchartsDirective } from 'ngx-echarts';
import { EChartsCoreOption } from 'echarts/types/dist/core';
import * as echarts from 'echarts/core';
import { GridComponent, TitleComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import { GraphChart } from 'echarts/charts';
echarts.use([GridComponent, CanvasRenderer, GraphChart, TitleComponent]);
@Component({
selector: 'app-person-network-graph',
imports: [],
imports: [MatCardModule, NgxEchartsDirective],
templateUrl: './person-network-graph.component.html',
styleUrl: './person-network-graph.component.scss',
})
export class PersonNetworkGraphComponent {
collaborationNetwork = input<CollaborationNetwork>();
authorDisplayName: string = '';
options!: EChartsCoreOption;
constructor() {
effect(() => {
const network = this.collaborationNetwork();
if (!network) {
return;
}
this.updateGraphOptions(network);
});
}
onGraphWheel(event: WheelEvent): void {
event.preventDefault();
}
private updateGraphOptions(collaborationNetwork: CollaborationNetwork): void {
if (!collaborationNetwork) {
return;
}
const datas = [
{
nodes: collaborationNetwork.nodes.map((node) => {
const category = (() => {
if (node.category === 'institution') {
return 'institution';
}
if (node.category === 'work') {
return 'work';
}
if (node.raw_id === collaborationNetwork.raw_person_id) {
return 'author';
}
return 'collaborator';
})();
if (node.category === 'work') {
return {
id: node.raw_id + '',
name: node.display_name,
category,
};
}
return {
id: node.raw_id + '',
name: node.display_name,
category,
label: {
show: true,
},
};
}),
links: collaborationNetwork.edges.map((edge) => {
return [edge.raw_source_id + '', edge.raw_target_id + ''];
}),
categories: [
{
name: 'author',
},
{
name: 'collaborator',
},
{
name: 'institution',
},
{
name: 'work',
},
],
},
];
this.authorDisplayName =
collaborationNetwork.nodes.find(
(node) => node.raw_id === collaborationNetwork.raw_person_id,
)?.display_name || 'Unknown Author';
this.options = {
series: datas.map((item) => {
return {
name: this.authorDisplayName,
type: 'graph',
layout: 'force',
symbolSize: 20,
animation: false,
data: item.nodes,
roam: true,
force: {
repulsion: 10000,
edgeLength: 125,
gravity: 0.2,
},
links: item.links.map((e) => {
return {
source: e[0] + '',
target: e[1] + '',
};
}),
categories: item.categories,
roamTrigger: 'global', // + 2 hours because of incomplete documentation https://github.com/apache/echarts/issues/17214
emphasis: {
focus: 'adjacency',
lineStyle: {
width: 5,
},
},
};
}),
};
}
}
@@ -1,9 +1,4 @@
<cdk-virtual-scroll-viewport
[itemSize]="52"
[maxBufferPx]="1200"
[minBufferPx]="400"
class="table"
>
<div class="table">
<table mat-table [dataSource]="dataSource" [trackBy]="trackBy" matSort>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>ID</th>
@@ -43,7 +38,7 @@
[cdkDetailRowTpl]="tpl"
></tr>
</table>
</cdk-virtual-scroll-viewport>
</div>
<ng-template #tpl let-element>
<div class="mat-row detail-row" [@detailExpand] style="overflow: hidden">
@@ -6,13 +6,12 @@ import {
ViewChild,
} from '@angular/core';
import { CollaborationNetwork } from '../../api';
import { Router, RouterModule } from '@angular/router';
import { RouterModule } from '@angular/router';
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { MatSort } from '@angular/material/sort';
import { MatSort, MatSortModule } from '@angular/material/sort';
import { CdkDetailRowDirective } from '../../directives/cdk-detail-row.directive';
type Collaborator = {
@@ -32,9 +31,9 @@ type Work = {
imports: [
MatTableModule,
MatIconModule,
MatSortModule,
RouterModule,
CdkDetailRowDirective,
ScrollingModule,
MatButtonModule,
],
templateUrl: './person-network-publications.component.html',
@@ -52,21 +51,41 @@ export class PersonNetworkPublicationsComponent implements AfterViewInit {
@ViewChild(MatSort) sort!: MatSort;
constructor(private readonly router: Router) {}
constructor() {
effect(() => {
const collaborationNetwork = this.collaborationNetwork();
if (!collaborationNetwork) {
return;
}
this.dataSource = this.buildWorksDataSource(collaborationNetwork);
this.dataSource.sortingDataAccessor = (item, property) => {
if (property === 'contributors') {
return item.collaborators.length;
}
return (item as Record<string, unknown>)[property] as string | number;
};
if (this.sort) {
this.dataSource.sort = this.sort;
this.setDefaultSort();
}
});
effect(() => {
const searchTerm = this.searchTerm();
this.dataSource.filter = searchTerm.trim().toLowerCase();
});
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
onCollaborationNetworkChange = effect(() => {
const collaborationNetwork = this.collaborationNetwork();
this.dataSource = this.buildWorksDataSource(collaborationNetwork);
});
onSearchTermChange = effect(() => {
const searchTerm = this.searchTerm();
this.dataSource.filter = searchTerm.trim().toLowerCase();
});
private setDefaultSort(): void {
this.sort.active = 'title';
this.sort.direction = 'asc';
this.sort.sortChange.emit({ active: 'title', direction: 'asc' });
}
buildWorksDataSource(
collaborationNetwork: CollaborationNetwork,
@@ -1,19 +1,5 @@
<div class="content">
<mat-card appearance="outlined">
<mat-card-header>
<mat-card-title
>Kollaborations Netzwerk von {{ authorDisplayName }}</mat-card-title
>
</mat-card-header>
<mat-card-content>
<div
class="graph"
echarts
[options]="options"
(chartClick)="onChartEvent($event, 'chartClick')"
></div>
</mat-card-content>
</mat-card>
<app-person-network-graph [collaborationNetwork]="collaborationNetwork" />
<div class="right-card">
<form class="network-filter-form" [formGroup]="filterForm">
@@ -62,10 +48,9 @@
}}</mat-label>
<input
matInput
[value]="searchTerm()"
(keyup)="changeSearchTerm($event)"
#input
appearance="outline"
formControlName="searchTerm"
/>
<mat-icon matSuffix>search</mat-icon>
</mat-form-field>
@@ -3,19 +3,20 @@
flex-direction: row;
padding: 40px;
gap: 20px;
max-height: 1000px;
align-items: stretch;
}
.graph {
width: 75vh;
height: 75vh;
.content > app-person-network-graph,
.right-card {
flex: 1 1 0;
min-width: 0;
}
.table {
overflow: auto;
width: 100%;
max-height: 1000px;
height: 1200px;
max-height: 70vh;
height: 100%;
}
.table table {
@@ -55,24 +56,4 @@
.toggle-group .mat-button-toggle {
text-transform: none;
}
.element-row {
position: relative;
}
.element-row:not(.expanded) {
cursor: pointer;
}
.element-row:not(.expanded):hover {
background: #f5f5f5;
}
.element-row.expanded {
border-bottom-color: transparent;
}
.network-filter-form {
}
@@ -1,26 +1,14 @@
import { toSignal } from '@angular/core/rxjs-interop';
import {
AfterViewInit,
Component,
computed,
effect,
OnInit,
signal,
ViewChild,
WritableSignal,
} from '@angular/core';
import { Component, computed, OnInit } from '@angular/core';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import type { EChartsCoreOption } from 'echarts/core';
import { MatCardModule } from '@angular/material/card';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatRippleModule } from '@angular/material/core';
import { NgxEchartsDirective, provideEchartsCore } from 'ngx-echarts';
import { provideEchartsCore } from 'ngx-echarts';
import * as echarts from 'echarts/core';
import { GridComponent, TitleComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import { GraphChart } from 'echarts/charts';
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
import { MatTableModule } from '@angular/material/table';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { ScrollingModule } from '@angular/cdk/scrolling';
@@ -33,13 +21,13 @@ import {
} from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import { MatSliderModule } from '@angular/material/slider';
import { MatSort, MatSortModule } from '@angular/material/sort';
import { MatSortModule } from '@angular/material/sort';
import { LiveAnnouncer } from '@angular/cdk/a11y';
import { CollaborationNetwork } from '../../api';
import { PersonNetworkCollaboratorsComponent } from '../person-network-collaborators/person-network-collaborators.component';
import { PersonNetworkPublicationsComponent } from '../person-network-publications/person-network-publications.component';
import { debounceTime } from 'rxjs';
echarts.use([GridComponent, CanvasRenderer, GraphChart, TitleComponent]);
import { PersonNetworkGraphComponent } from '../person-network-graph/person-network-graph.component';
type Collaborator = {
id: string;
@@ -56,7 +44,6 @@ type Works = {
@Component({
selector: 'app-person-network',
imports: [
NgxEchartsDirective,
MatCardModule,
MatTableModule,
MatButtonModule,
@@ -74,19 +61,15 @@ type Works = {
MatSliderModule,
ReactiveFormsModule,
FormsModule,
PersonNetworkGraphComponent,
],
templateUrl: './person-network.component.html',
styleUrl: './person-network.component.scss',
providers: [provideEchartsCore({ echarts })],
})
export class PersonNetworkComponent implements OnInit, AfterViewInit {
export class PersonNetworkComponent implements OnInit {
collaborationNetwork!: CollaborationNetwork;
displayedTable = signal<'collaborators' | 'publications'>('collaborators');
searchTerm = signal('');
minimumCollaborations: WritableSignal<number> = signal(5);
filterForm = new FormGroup({
searchTerm: new FormControl(''),
startYear: new FormControl(2000),
@@ -95,201 +78,77 @@ export class PersonNetworkComponent implements OnInit, AfterViewInit {
displayedTable: new FormControl('collaborators'),
});
readonly formValue = toSignal(this.filterForm.valueChanges);
readonly debouncedFormValue = toSignal(
this.filterForm.valueChanges.pipe(debounceTime(300)),
);
displayedTable = computed(() => {
return this.formValue()?.displayedTable;
});
readonly searchTerm = computed(() => {
console.log('Search term computed:', this.debouncedFormValue()?.searchTerm);
return this.debouncedFormValue()?.searchTerm || '';
});
readonly activatedRouteData = toSignal(this.activatedRoute.data);
formatYearLabel = (value: number) => `${value}`;
collaboratorsDataSource: MatTableDataSource<Collaborator> =
new MatTableDataSource();
worksDataSource: MatTableDataSource<Works> = new MatTableDataSource();
options!: EChartsCoreOption;
displayedCollaboratorsColumns: string[] = ['id', 'name', 'actions'];
displayedWorksColumns: string[] = ['id', 'title'];
constructor(
private readonly activatedRoute: ActivatedRoute,
private readonly router: Router,
private readonly _liveAnnouncer: LiveAnnouncer,
) {}
authorDisplayName: string = '';
@ViewChild(MatSort) collaboratorsSort!: MatSort;
@ViewChild(MatSort) worksSort!: MatSort;
trackBy = (index: number, el: Collaborator) => el.id;
ngAfterViewInit() {
this.collaboratorsDataSource.sort = this.collaboratorsSort;
this.worksDataSource.sort = this.worksSort;
}
ngOnInit(): void {
this.activatedRoute.data.subscribe(({ collaborationNetwork }) => {
this.collaborationNetwork = collaborationNetwork;
console.log('Collaboration Network:', this.collaborationNetwork);
this.filterForm.valueChanges
.pipe(debounceTime(300))
.subscribe((value) => {
this.router.navigate([], {
relativeTo: this.activatedRoute,
queryParams: {
table: value.displayedTable,
search_term: value.searchTerm,
start_year: value.startYear,
end_year: value.endYear,
},
// replaceUrl: true,
});
});
const currentParams = this.activatedRoute.snapshot.queryParams;
this.filterForm
.get('startYear')!
.setValue(
Number(currentParams['start_year']) ||
this.collaborationNetwork.filter_information.min_possible_year,
);
this.filterForm
.get('endYear')!
.setValue(
Number(currentParams['end_year']) ||
this.collaborationNetwork.filter_information.max_possible_year,
);
const initialStartYear =
Number(currentParams['start_year']) ||
this.collaborationNetwork.filter_information.min_possible_year;
const initialEndYear =
Number(currentParams['end_year']) ||
this.collaborationNetwork.filter_information.max_possible_year;
this.activatedRoute.queryParams.subscribe((params) => {
const table = params['table'];
if (table === 'collaborators' || table === 'publications') {
this.displayedTable.set(table);
} else {
this.displayedTable.set('collaborators');
}
const searchTerm = params['search_term'] || '';
this.searchTerm.set(searchTerm);
this.filterForm.setValue({
searchTerm: currentParams['search_term'] || '',
startYear: initialStartYear,
endYear: initialEndYear,
minimumCollaborations:
this.filterForm.get('minimumCollaborations')?.value ?? 1,
displayedTable: currentParams['table'] || 'collaborators',
});
});
this.updateGraphOptions();
this.filterForm.valueChanges.pipe(debounceTime(300)).subscribe((value) => {
const displayedTable = value.displayedTable;
const searchTerm = value.searchTerm;
const startYear = value.startYear;
const endYear = value.endYear;
this.router.navigate([], {
relativeTo: this.activatedRoute,
queryParams: {
table: displayedTable,
search_term: searchTerm,
start_year: startYear,
end_year: endYear,
},
});
});
}
private updateGraphOptions(): void {
const datas = [
{
nodes: this.collaborationNetwork.nodes.map((node) => {
const category = (() => {
if (node.category === 'institution') {
return 'institution';
}
if (node.category === 'work') {
return 'work';
}
if (node.raw_id === this.collaborationNetwork.raw_person_id) {
return 'author';
}
return 'collaborator';
})();
if (category === 'work') {
return {
id: node.raw_id + '',
name: node.display_name,
category,
};
}
return {
id: node.raw_id + '',
name: node.display_name,
category,
label: { show: true },
};
}),
links: this.collaborationNetwork.edges.map((edge) => {
return [edge.raw_source_id + '', edge.raw_target_id + ''];
}),
categories: [
{
name: 'author',
label: { show: true },
},
{
name: 'collaborator',
},
{
name: 'institution',
},
{
name: 'work',
},
],
emphasis: {
label: {
show: true,
},
},
},
];
this.authorDisplayName =
this.collaborationNetwork.nodes.find(
(node) => node.raw_id === this.collaborationNetwork.raw_person_id,
)?.display_name || 'Unknown Author';
this.options = {
series: datas.map((item) => {
return {
name: this.authorDisplayName,
type: 'graph',
layout: 'force',
symbolSize: 25,
animation: false,
data: item.nodes,
roam: true,
legend: [
{
data: item.categories.map((c) => c.name),
},
],
force: {
repulsion: 10000,
edgeLength: 100,
gravity: 0.1,
},
links: item.links.map((e) => {
return {
source: e[0] + '',
target: e[1] + '',
};
}),
categories: item.categories,
};
}),
};
}
navigateToPerson(raw_person_id: string) {
this.router.navigate(['/person', raw_person_id]);
}
onChartEvent(event: echarts.ECElementEvent, type: string) {
if (type !== 'chartClick') {
return;
}
const data = event.data as Collaborator;
const raw_person_id = data.id;
if (raw_person_id === this.collaborationNetwork.raw_person_id) {
return;
}
this.navigateToPerson(raw_person_id);
}
changeSearchTerm(event: Event) {
const filterValue = (event.target as HTMLInputElement).value;
this.searchTerm.set(filterValue);
}
trackBy = (index: number, el: Collaborator) => el.id;
onToggleChange(event: any) {
this.displayedTable.set(event.value);
const value = event.value;
if (!value) {
return;
}
this.filterForm.get('displayedTable')?.setValue(value);
}
}