Files
smartguide/webapp/src/app/modules/person-network-graph/person-network-graph.component.ts
T

137 lines
3.6 KiB
TypeScript

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: [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,
},
},
};
}),
};
}
}