added pie chart for authors and institutions

This commit is contained in:
Kazgatari
2026-07-13 12:14:42 +02:00
parent c2942906db
commit 0fd028c6cb
11 changed files with 328 additions and 17 deletions
@@ -0,0 +1,3 @@
<div class="pie-chart-holder">
<div echarts [options]="chartOptions()" class="pie-chart-container"></div>
</div>
@@ -0,0 +1,17 @@
.pie-chart-holder {
display: flex;
flex-direction: column;
background: white;
border: 1px solid #d7e4ff;
border-radius: 16px;
padding: 16px;
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.04);
box-sizing: border-box;
width: 100%;
height: 300px;
}
.pie-chart-container {
width: 100%;
height: 100%;
}
@@ -0,0 +1,155 @@
import {
Component,
input,
computed,
ChangeDetectionStrategy,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { EChartsOption } from 'echarts';
import { NgxEchartsDirective } from 'ngx-echarts';
import { TrendResponse } from '../../../api';
import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors';
@Component({
selector: 'app-uni-pie-chart',
templateUrl: './uni-pie-chart.component.html',
styleUrl: './uni-pie-chart.component.scss',
imports: [NgxEchartsDirective, CommonModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UniPieChartComponent {
readonly data = input<TrendResponse | null>(null);
readonly entityId = input<string | null>(null);
readonly entityName = input<string | null>(null);
readonly startYear = input<number>(2010);
readonly endYear = input<number>(2025);
getTopicColor(topic: string): string {
return topicHierarchyColor({ name: topic });
}
readonly chartOptions = computed<EChartsOption>(() => {
const trendData = this.data();
const entityIdVal = this.entityId();
if (!trendData || !entityIdVal) {
return {};
}
// Find the series for the selected entity (resilient matching)
const series = trendData.series.find((s) => {
const sId = s.entity_id;
const targetId = entityIdVal;
return (
sId === targetId ||
sId.toUpperCase() === targetId.toUpperCase() ||
sId.split('/').pop()?.toUpperCase() === targetId.toUpperCase() ||
targetId.split('/').pop()?.toUpperCase() === sId.toUpperCase()
);
});
if (!series || !series.topic_data) {
return {
title: {
text: 'Keine Daten vorhanden',
left: 'center',
top: 'center',
textStyle: { color: '#6b7280', fontSize: 13 },
},
};
}
const startYr = this.startYear();
const endYr = this.endYear();
const pieDataList: any[] = [];
Object.entries(series.topic_data).forEach(([topicName, yearlyItems]) => {
const filtered = (yearlyItems || []).filter(
(y) => y.year >= startYr && y.year <= endYr,
);
const totalRaw = filtered.reduce((sum, item) => sum + item.raw, 0);
if (totalRaw > 0) {
pieDataList.push({
value: totalRaw,
name: topicName,
itemStyle: {
color: this.getTopicColor(topicName),
},
});
}
});
if (pieDataList.length === 0) {
return {
title: {
text: `Keine Daten im Zeitraum ${startYr} - ${endYr}`,
left: 'center',
top: 'center',
textStyle: { color: '#6b7280', fontSize: 13 },
},
};
}
return {
title: {
text: `Verteilung: ${this.entityName()}`,
subtext: `Zeitraum: ${startYr} - ${endYr}`,
left: 'center',
top: 10,
textStyle: {
fontSize: 13,
fontWeight: 600,
color: '#1f2937',
},
subtextStyle: {
fontSize: 10,
color: '#6b7280',
},
},
tooltip: {
trigger: 'item',
formatter: '{b}: <strong>{c}</strong> ({d}%)',
},
legend: {
show: false, // Hide default legend to keep layout clean and readable in small containers
},
series: [
{
name: 'Themenverteilung',
type: 'pie',
radius: ['45%', '75%'],
avoidLabelOverlap: true,
top: 40,
bottom: 10,
itemStyle: {
borderRadius: 6,
borderColor: '#fff',
borderWidth: 2,
},
label: {
show: true,
position: 'outside',
fontSize: 9,
formatter: '{b}: {d}%',
color: '#374151',
},
labelLine: {
show: true,
length: 8,
length2: 5,
smooth: true,
},
emphasis: {
label: {
show: true,
fontSize: 10,
fontWeight: 'bold',
formatter: '{b}\n{c} ({d}%)',
},
},
data: pieDataList,
},
],
};
});
}
@@ -55,10 +55,12 @@
[locationSearchResults]="locationSearchResults()"
[getEntityColor]="boundGetEntityColor"
[isVisible]="boundIsVisible"
[selectedPieEntityId]="selectedPieEntity()?.id ?? null"
(addLocation)="addLocation($event)"
(removeLocation)="removeLocation($event.id, $event.type)"
(searchChange)="onLocationSearchChange($event)"
(toggleVisibility)="toggleVisibility($event)">
(toggleVisibility)="toggleVisibility($event)"
(selectPie)="onSelectPieEntity($event)">
</app-location-selector>
}
@@ -130,6 +132,23 @@
[endYear]="endYear()"></app-uni-trend-chart>
</div>
<!-- Pie Chart Card -->
@if (
selectedPieEntity() &&
selectedSearchType() !== 'news' &&
selectedSearchType() !== 'all'
) {
<div class="pie-chart-section" style="margin-bottom: 24px">
<app-uni-pie-chart
[data]="trendResponse()"
[entityId]="selectedPieEntity()!.id"
[entityName]="selectedPieEntity()!.name"
[startYear]="startYear()"
[endYear]="endYear()">
</app-uni-pie-chart>
</div>
}
<!-- Unified Institution Search, Compare, & Recommendations -->
@if (activeConfig().showInstitutionSelector) {
<app-institution-selector
@@ -143,10 +162,12 @@
[searchResults]="searchResults()"
[getEntityColor]="boundGetEntityColor"
[isVisible]="boundIsVisible"
[selectedPieEntityId]="selectedPieEntity()?.id ?? null"
(addInstitution)="addInstitution($event.id, $event.name)"
(removeInstitution)="removeInstitution($event)"
(searchChange)="onSearchChange($event)"
(toggleVisibility)="toggleVisibility($event)">
(toggleVisibility)="toggleVisibility($event)"
(selectPie)="onSelectPieEntity($event)">
</app-institution-selector>
}
@@ -161,10 +182,12 @@
[searchResults]="authorSearchResults()"
[getEntityColor]="boundGetEntityColor"
[isVisible]="boundIsVisible"
[selectedPieEntityId]="selectedPieEntity()?.id ?? null"
(addAuthor)="addAuthor($event.id, $event.name)"
(removeAuthor)="removeAuthor($event)"
(searchChange)="onAuthorSearchChange($event)"
(toggleVisibility)="toggleVisibility($event)">
(toggleVisibility)="toggleVisibility($event)"
(selectPie)="onSelectPieEntity($event)">
</app-author-selector>
}
</main>
@@ -22,6 +22,7 @@ import { MatIconModule } from '@angular/material/icon';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { UniTrendChartComponent } from '../uni-trend-chart/uni-trend-chart.component';
import { UniPieChartComponent } from '../uni-pie-chart/uni-pie-chart.component';
import {
TrendResponse,
TrendSeries,
@@ -108,6 +109,7 @@ export const TREND_CONFIGS: Record<string, TrendStrategyConfig> = {
CommonModule,
FormsModule,
UniTrendChartComponent,
UniPieChartComponent,
MatCheckboxModule,
MatButtonModule,
MatIconModule,
@@ -159,6 +161,9 @@ export class UniTrendParentComponent implements OnInit {
'normalized',
);
readonly activeChartTopic = signal<string>('');
readonly selectedPieEntity = signal<{ id: string; name: string } | null>(
null,
);
// Config mapper for active strategy UI representation
readonly activeConfig = computed(
@@ -255,6 +260,10 @@ export class UniTrendParentComponent implements OnInit {
return entityColors[idx % entityColors.length];
}
onSelectPieEntity(event: { id: string; name: string }) {
this.selectedPieEntity.set(event);
}
readonly boundGetEntityColor = (id: string) => this.getEntityColor(id);
readonly boundIsVisible = (id: string) => this.isVisible(id);
@@ -592,6 +601,43 @@ export class UniTrendParentComponent implements OnInit {
this.activeChartTopic.set(params['active_chart_topic']);
}
});
// Effect: automatically select/sync selectedPieEntity with active trendResponse series list
effect(() => {
const data = this.trendResponse();
const currentPie = untracked(() => this.selectedPieEntity());
if (!data || data.series.length === 0) {
this.selectedPieEntity.set(null);
return;
}
if (currentPie) {
const stillExists = data.series.some((s) => {
const sId = s.entity_id;
const targetId = currentPie.id;
return (
sId === targetId ||
sId.toUpperCase() === targetId.toUpperCase() ||
sId.split('/').pop()?.toUpperCase() === targetId.toUpperCase() ||
targetId.split('/').pop()?.toUpperCase() === sId.toUpperCase()
);
});
if (stillExists) {
return;
}
}
const firstSeries = data.series[0];
if (firstSeries) {
this.selectedPieEntity.set({
id: firstSeries.entity_id,
name: firstSeries.entity_name,
});
} else {
this.selectedPieEntity.set(null);
}
});
}
ngOnInit() {
@@ -997,9 +1043,13 @@ export class UniTrendParentComponent implements OnInit {
const current = this.comparisonAuthors();
const updated = [...current];
(res.results || []).forEach((author: any) => {
if (author.id && !updated.some((a) => a.id === author.id)) {
const shortId =
author.id && author.id.includes('/')
? author.id.split('/').pop() || author.id
: author.id;
if (shortId && !updated.some((a) => a.id === shortId)) {
updated.push({
id: author.id,
id: shortId,
name: author.display_name || 'Unbekannt',
});
}
@@ -1018,9 +1068,10 @@ export class UniTrendParentComponent implements OnInit {
}
addAuthor(id: string, name: string) {
const shortId = id.includes('/') ? id.split('/').pop() || id : id;
const current = this.comparisonAuthors();
if (!current.some((a) => a.id === id)) {
this.comparisonAuthors.set([...current, { id, name }]);
if (!current.some((a) => a.id === shortId)) {
this.comparisonAuthors.set([...current, { id: shortId, name }]);
}
}
@@ -45,8 +45,15 @@
' 8%, #ffffff)'
"
[style.color]="getEntityColor()(auth.id)">
<span class="item-name" [title]="auth.name">
<mat-icon>person</mat-icon> {{ auth.name }}
<span
class="item-name clickable-pie-target"
[class.active-pie]="selectedPieEntityId() === auth.id"
(click)="onSelectPie(auth.id, auth.name)"
[title]="'Klicken, um die Themenverteilung im Kreisdiagramm anzuzeigen'">
<mat-icon class="pie-icon-indicator">{{
selectedPieEntityId() === auth.id ? 'pie_chart' : 'person'
}}</mat-icon>
{{ auth.name }}
</span>
<div class="item-actions">
<button
@@ -23,11 +23,17 @@ export class AuthorSelectorComponent {
readonly activeChartTopic = input<string>('');
readonly getEntityColor = input<(id: string) => string>(() => '#9ca3af');
readonly isVisible = input<(id: string) => boolean>(() => true);
readonly selectedPieEntityId = input<string | null>(null);
readonly addAuthor = output<{ id: string; name: string }>();
readonly removeAuthor = output<string>();
readonly searchChange = output<string>();
readonly toggleVisibility = output<string>();
readonly selectPie = output<{ id: string; name: string }>();
onSelectPie(id: string, name: string): void {
this.selectPie.emit({ id, name });
}
onSearchInput(event: Event): void {
const val = (event.target as HTMLInputElement).value;
@@ -4,9 +4,25 @@
<div class="section-header">
<h3>Einrichtungen vergleichen</h3>
@if (primaryInstitutionName()) {
<div class="primary-inst-badge">
<div
class="primary-inst-badge clickable-pie-target"
[class.active-pie]="selectedPieEntityId() === primaryInstitution()"
(click)="
onSelectPie(
primaryInstitution() || '',
primaryInstitutionName() || ''
)
"
[title]="'Klicken, um die Themenverteilung im Kreisdiagramm anzuzeigen'">
<span class="label">Zugewiesen:</span>
<strong class="name">{{ primaryInstitutionName() }}</strong>
<strong class="name">
<mat-icon class="pie-icon-indicator">{{
selectedPieEntityId() === primaryInstitution()
? 'pie_chart'
: 'apartment'
}}</mat-icon>
{{ primaryInstitutionName() }}
</strong>
</div>
}
</div>
@@ -50,8 +66,15 @@
' 8%, #ffffff)'
"
[style.color]="getEntityColor()(inst.id)">
<span class="item-name" [title]="inst.name">
<mat-icon>apartment</mat-icon> {{ inst.name }}
<span
class="item-name clickable-pie-target"
[class.active-pie]="selectedPieEntityId() === inst.id"
(click)="onSelectPie(inst.id, inst.name)"
[title]="'Klicken, um die Themenverteilung im Kreisdiagramm anzuzeigen'">
<mat-icon class="pie-icon-indicator">{{
selectedPieEntityId() === inst.id ? 'pie_chart' : 'apartment'
}}</mat-icon>
{{ inst.name }}
</span>
<div class="item-actions">
<button
@@ -25,11 +25,17 @@ export class InstitutionSelectorComponent {
readonly searchResults = input<any[]>([]);
readonly getEntityColor = input<(id: string) => string>(() => '#9ca3af');
readonly isVisible = input<(id: string) => boolean>(() => true);
readonly selectedPieEntityId = input<string | null>(null);
readonly addInstitution = output<{ id: string; name: string }>();
readonly removeInstitution = output<string>();
readonly searchChange = output<string>();
readonly toggleVisibility = output<string>();
readonly selectPie = output<{ id: string; name: string }>();
onSelectPie(id: string, name: string): void {
this.selectPie.emit({ id, name });
}
onSearchInput(event: Event): void {
const val = (event.target as HTMLInputElement).value;
@@ -12,8 +12,15 @@
'color-mix(in srgb, ' + getEntityColor()(c.id) + ' 8%, #ffffff)'
"
[style.color]="getEntityColor()(c.id)">
<span class="item-name" [title]="c.name">
<mat-icon>flag</mat-icon> {{ c.name }}
<span
class="item-name clickable-pie-target"
[class.active-pie]="selectedPieEntityId() === c.id"
(click)="onSelectPie(c.id, c.name)"
[title]="'Klicken, um die Themenverteilung im Kreisdiagramm anzuzeigen'">
<mat-icon class="pie-icon-indicator">{{
selectedPieEntityId() === c.id ? 'pie_chart' : 'flag'
}}</mat-icon>
{{ c.name }}
</span>
<div class="item-actions">
<button
@@ -51,8 +58,15 @@
'color-mix(in srgb, ' + getEntityColor()(r.id) + ' 8%, #ffffff)'
"
[style.color]="getEntityColor()(r.id)">
<span class="item-name" [title]="r.name">
<mat-icon>public</mat-icon> {{ r.name }}
<span
class="item-name clickable-pie-target"
[class.active-pie]="selectedPieEntityId() === r.id"
(click)="onSelectPie(r.id, r.name)"
[title]="'Klicken, um die Themenverteilung im Kreisdiagramm anzuzeigen'">
<mat-icon class="pie-icon-indicator">{{
selectedPieEntityId() === r.id ? 'pie_chart' : 'public'
}}</mat-icon>
{{ r.name }}
</span>
<div class="item-actions">
<button
@@ -22,6 +22,7 @@ export class LocationSelectorComponent {
readonly locationSearchResults = input<AutocompleteLocationItem[]>([]);
readonly getEntityColor = input<(id: string) => string>(() => '#9ca3af');
readonly isVisible = input<(id: string) => boolean>(() => true);
readonly selectedPieEntityId = input<string | null>(null);
readonly addLocation = output<AutocompleteLocationItem>();
readonly removeLocation = output<{
@@ -30,6 +31,11 @@ export class LocationSelectorComponent {
}>();
readonly searchChange = output<string>();
readonly toggleVisibility = output<string>();
readonly selectPie = output<{ id: string; name: string }>();
onSelectPie(id: string, name: string): void {
this.selectPie.emit({ id, name });
}
onSearchInput(event: Event): void {
const val = (event.target as HTMLInputElement).value;